A PowerShell Script Server. Basically, it monitors a folder and when files appear in it, it takes some action on them.
File Name : superwatcher.ps1
Author : MOBZystems
Date : Jul-15-2017
A PowerShell Script Server. Basically, it monitors a folder and when files appear in it, it takes some action on them, in this case: execute them. I’ve written servers like that before, but always in C#, using the System.IO.FileSystemWatcher class. Once again, it’s not rocket science: the main challenge is in error handling and logging. The heavy lifting is done by the FileSystemWatcher, which lives up to its name: it watches a (single) folder, with or without it subfolders, and raises event when files are Created, Changed, Deleted or Renamed. (Those are also the names of the events). Each event gets an argument specifying which file was afffected; the Renamed event also receives the previous name of the file. So after setting up the FileSystemWatcher, all there’s left to do is to implement the event handlers.
Start-FileSystemWatcher.ps1 - File System Watcher in Powershell. Brought to you by MOBZystems, Home of Tools - https://www.mobzystems.com/code/using-a-filesystemwatcher-from-powershell/
superwatcher [-Path] <String> [[-Filter] <String>] [-Recurse]
[-SkipHiddenFolder] [-CreatedAction <ScriptBlock>] [-DeletedAction <ScriptBlock>] [-ChangedAction <ScriptBlock>]
[-RenamedAction <ScriptBlock>] [-KeyboardTimeout <Int32>] [<CommonParameters>]
The path to monitor
attr |
value |
---|---|
Required? |
true |
Position? |
1 |
Default value |
|
Accept pipeline input? |
false |
Accept wildcard characters? |
false |
Monitor these files (a wildcard)
attr |
value |
---|---|
Required? |
false |
Position? |
2 |
Default value |
. |
Accept pipeline input? |
false |
Accept wildcard characters? |
false |
Monitor subdirectories?
attr |
value |
---|---|
Required? |
false |
Position? |
named |
Default value |
False |
Accept pipeline input? |
false |
Accept wildcard characters? |
false |
Execute ths block on Created
attr |
value |
---|---|
Required? |
false |
Position? |
named |
Default value |
|
Accept pipeline input? |
false |
Accept wildcard characters? |
false |
Execute ths block on Deleted
attr |
value |
---|---|
Required? |
false |
Position? |
named |
Default value |
|
Accept pipeline input? |
false |
Accept wildcard characters? |
false |
Execute ths block on Changed
attr |
value |
---|---|
Required? |
false |
Position? |
named |
Default value |
|
Accept pipeline input? |
false |
Accept wildcard characters? |
false |
Execute ths block on Renamed
attr |
value |
---|---|
Required? |
false |
Position? |
named |
Default value |
|
Accept pipeline input? |
false |
Accept wildcard characters? |
false |
Check for ESC every … seconds
attr |
value |
---|---|
Required? |
false |
Position? |
named |
Default value |
-1 |
Accept pipeline input? |
false |
Accept wildcard characters? |
false |
This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer, PipelineVariable, and OutVariable. For more information, see about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
System.String[]
Events Script Blocks
superwatcher.ps1 C:\test\ -Recurse -CreatedAction {
superwatcher.ps1 C:\test\ -Recurse -DeletedAction {