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.
This topic is 7 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
juneb1022
Posts: 34
Last visit: Fri Sep 30, 2016 8:08 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Pulling size and security from io.filesystemwatcher

Post by jvierra »

It should be in SourceArgs.
$Event.SourceArgs.Filename
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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.     $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!'
This topic is 7 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked