Pulling size and security from io.filesystemwatcher

Ask your Windows 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 » Thu Apr 21, 2016 9:22 am

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.

User avatar
jvierra
Posts: 13798
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Pulling size and security from io.filesystemwatcher

Post by jvierra » Thu Apr 21, 2016 10:33 am

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

User avatar
jvierra
Posts: 13798
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Pulling size and security from io.filesystemwatcher

Post by jvierra » Thu Apr 21, 2016 1:01 pm

Here is a better example of how the event data is delivered.
For the 'Created" event the notify filter is unused.

Code: Select all

$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!'

Locked