Page 1 of 1

Pulling size and security from io.filesystemwatcher

Posted: Thu Apr 21, 2016 9:22 am
by juneb1022
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.

Re: Pulling size and security from io.filesystemwatcher

Posted: Thu Apr 21, 2016 10:33 am
by jvierra
It should be in SourceArgs.
$Event.SourceArgs.Filename

Re: Pulling size and security from io.filesystemwatcher

Posted: Thu Apr 21, 2016 1:01 pm
by jvierra
Here is a better example of how the event data is delivered.
For the 'Created" event the notify filter is unused.
  1. $action={
  2.     $e= [System.Management.Automation.PSEventArgs]$event
  3.     Write-Host
  4.     Write-Host  ('ComputerName='+ $e.ComputerName)
  5.     Write-Host  ('Time Generated=' + $e.TimeGenerated)
  6.     Write-Host ('Message=' + $e.MessageData)
  7.    
  8.     $s=[System.IO.FileSystemEventArgs]($Event.SourceEventArgs)
  9.     Write-Host ('Name=' + $s.Name) -fore green
  10.     Write-Host ('FullPath=' + $s.FullPath) -fore green
  11.     Write-Host ('change=' + $s.ChangeType) -fore green
  12. }
  13.  
  14.  
  15. $fsw = New-Object IO.FileSystemWatcher('c:\test2','*.*')
  16. $fsw.EnableRaisingEvents = $true
  17. $fsw.IncludeSubdirectories = $true
  18. #$fsw.NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite, Security, Size'
  19.  
  20. Register-ObjectEvent $fsw -EventName Created -SourceIdentifier FinanceFileCreated -Action $action -MessageData 'Hello World!'