February 2019
Intermediate to advanced
626 pages
15h 51m
English
The Action parameter of Register-ObjectEvent allows a script block to be automatically triggered when an event is raised.
The script block can use a reserved variable, $event, which is equivalent to the output from Get-Event. In the following example, the event subscriber includes an action, which creates a log message. The log messages are written to file in a different folder; if they were written to the same folder, a loop would be created:
New-Item C:\Audit -ItemType Directory$watcher = [System.IO.FileSystemWatcher]::new('C:\Data')$params = @{ InputObject = $watcher EventName = 'Changed' Action = { $event.SourceEventArgs | Export-Csv C:\Audit\DataActivity.log -Append }}Register-ObjectEvent ...Read now
Unlock full access