It just monitor a folder for new files with Powershell.
1
2#My function
3function Do-MonitorNewFile-TODO($Event_name, $Event_FullPath, $Event_changeType, $Event_TimeGenerated)
4{
5 Write-Host "The file '$Event_name' was $Event_changeType at $Event_TimeGenerated" -ForegroundColor red
6}
7
8function Do-MonitorNewFile($Sourcepath, $MonitorName)
9{
10 # Remove previous event
11 Unregister-Event -SourceIdentifier "$MonitorName" -ErrorAction SilentlyContinue
12 Remove-Event -SourceIdentifier "$MonitorName" -ErrorAction SilentlyContinue
13
14 # Watcher
15 $watcher = New-Object System.IO.FileSystemWatcher
16 $watcher.IncludeSubdirectories = 1
17 $watcher.Path = $Sourcepath
18 $watcher.EnableRaisingEvents = 1
19
20 #action
21 $action = {
22 $Event_name = $Event.SourceEventArgs.Name
23 $Event_FullPath = $Event.SourceEventArgs.FullPath
24 $Event_changeType = $Event.SourceEventArgs.ChangeType
25 $Event_TimeGenerated = $Event.TimeGenerated
26
27 # Call my function
28 Do-MonitorNewFile-TODO "$Event_name" "$Event_FullPath" "$Event_changeType" "$Event_TimeGenerated"
29 }
30
31 $oEvent = Register-ObjectEvent $watcher Created -Action $action -SourceIdentifier "$MonitorName"
32
33}
34
35# Call - monitor a folder
36Do-MonitorNewFile "C:\TEMP\" "MyMonitorName"
Comments