I was wondering if someone could help me with the below script. I would like to add the security and size from the System.IO.NotifyFilters, so that when the results are written to the host and to the csv file, I get the size of the file and the security.
$folder = '\\server\share'
$filter = '*.*'
$fsWatcher= New-Object IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubdirectories = $true;
NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite, Security, Size}
Register-ObjectEvent $fsWatcher Created -SourceIdentifier FinanceFileCreated -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
Write-Host "The file '$name' was $changeType at $timeStamp and size is $filesize" -foreground red
Out-File -FilePath D:\Powershell\FileSystemWatcher\outlog-finance.csv -Append -InputObject "The file '$name' was $changeType at $timeStamp and size is $filesize"}
Thanks.
Pulling size and security from io.filesystemwatcher
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: Pulling size and security from io.filesystemwatcher
Here is a better example of how the event data is delivered.
For the 'Created" event the notify filter is unused.
For the 'Created" event the notify filter is unused.
- $action={
- $e= [System.Management.Automation.PSEventArgs]$event
- Write-Host
- Write-Host ('ComputerName='+ $e.ComputerName)
- Write-Host ('Time Generated=' + $e.TimeGenerated)
- Write-Host ('Message=' + $e.MessageData)
- $s=[System.IO.FileSystemEventArgs]($Event.SourceEventArgs)
- Write-Host ('Name=' + $s.Name) -fore green
- Write-Host ('FullPath=' + $s.FullPath) -fore green
- Write-Host ('change=' + $s.ChangeType) -fore green
- }
- $fsw = New-Object IO.FileSystemWatcher('c:\test2','*.*')
- $fsw.EnableRaisingEvents = $true
- $fsw.IncludeSubdirectories = $true
- #$fsw.NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite, Security, Size'
- Register-ObjectEvent $fsw -EventName Created -SourceIdentifier FinanceFileCreated -Action $action -MessageData 'Hello World!'