Pulling size and security from io.filesystemwatcher

Ask your PowerShell-related questions, including questions on cmdlet development!
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.
Locked
User avatar
juneb1022
Posts: 34
Joined: Mon Jun 10, 2013 10:14 am

Pulling size and security from io.filesystemwatcher

Post 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.

jvierra
Posts: 14578
Joined: Tue May 22, 2007 9:57 am
Answers: 1
Been upvoted: 1 time
Contact:

Re: Pulling size and security from io.filesystemwatcher

Post by jvierra »

It should be in SourceArgs.
$Event.SourceArgs.Filename

jvierra
Posts: 14578
Joined: Tue May 22, 2007 9:57 am
Answers: 1
Been upvoted: 1 time
Contact:

Re: Pulling size and security from io.filesystemwatcher

Post 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.  
  3.     $e= [System.Management.Automation.PSEventArgs]$event
  4.  
  5.     Write-Host
  6.  
  7.     Write-Host  ('ComputerName='+ $e.ComputerName)
  8.  
  9.     Write-Host  ('Time Generated=' + $e.TimeGenerated)
  10.  
  11.     Write-Host ('Message=' + $e.MessageData)
  12.  
  13.    
  14.  
  15.     $s=[System.IO.FileSystemEventArgs]($Event.SourceEventArgs)
  16.  
  17.     Write-Host ('Name=' + $s.Name) -fore green
  18.  
  19.     Write-Host ('FullPath=' + $s.FullPath) -fore green
  20.  
  21.     Write-Host ('change=' + $s.ChangeType) -fore green
  22.  
  23. }
  24.  
  25.  
  26.  
  27.  
  28.  
  29. $fsw = New-Object IO.FileSystemWatcher('c:\test2','*.*')
  30.  
  31. $fsw.EnableRaisingEvents = $true
  32.  
  33. $fsw.IncludeSubdirectories = $true
  34.  
  35. #$fsw.NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite, Security, Size'
  36.  
  37.  
  38.  
  39. Register-ObjectEvent $fsw -EventName Created -SourceIdentifier FinanceFileCreated -Action $action -MessageData 'Hello World!'

Locked