W2KSG: Asynchronous log access
Listing 12.11contains a script to asynchronously access the event logs. Rather than follow the WMI methods we can use the background jobs functionality in PowerShell version 2
Start-PSJob -Name job1 -Command "Get-EventLog -LogName application"
Receive-PSJob -Name job1 -Keep
Get-PSJob -Name job1 | Remove-PSJob
We create the job using Start-PSJob. Give it a name and the command we want to run. Get-PSJob can be used to view the status of jobs. Check the state for completed jobs.
Receive-PSJob pulls the data out of the job. Using the -keep parameter leaves the data available to be accessed again otherwise it is removed.
The job can be removed using Remove-PSJob as shown.
|Share this post :|