I've successfully created a FileSystemWatcher (C# object). I run the code below in a powershell session.
# Filters
$filter = "somefile.txt"
$flagfolder = "C:\path\to\some\folder"
# Instantiate Watcher
$Watcher = New-Object IO.FileSystemWatcher $flagfolder, $filter -Property @{
IncludeSubdirectories = $false
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
# EVENT: $filter is created
$onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier FileCreated -Action {
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp"
Write-Host $path
someglobalfunction $param
}
After running the code above, if I do a Get-Job it reports the FileWatcher:
Id Name PSJobTypeName State HasMoreData Location
-- ---- ------------- ----- ----------- --------
74 FileCreated NotStarted False
But, if I open a new powershell session and do a Get-Job it reports nothing....
I need this to fire whenever anybody or anything creates the file $pathfolder\somefile.txt... but currently it only works when the session that defines $watcher creates the file.
$flaga value, but never use$flag.