| Richard's profileRichard Siddaway's BlogPhotosBlogLists | Help |
|
November 07 PowerShell WMI eventsIn my previous post ( http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!2598.entry or http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!2598.entry ) I started to look at WMI events in PowerShell v2. The win32_process class was used but all that showed us was that a process had started. We need a bit more information. A bit of digging brought up the Win32_ProcessStartTrace class that seems to do what we want. Register-WMIEvent allows us to specify the class we want to use rather than a query – however if we try that we don’t get anything returned - oops. Looking through the help for Register-WMIEvent shows that we have the possibility of performing an action when the event occurs. The action scriptblock can use a number of variables including $Event, $EventSubscriber, $Sender, $SourceEventArgs, and $SourceArgs automatic variables. Wanting to understand these variables I tried dumping it though get-member. PS> Register-WmiEvent -Query "Select * FROM Win32_ProcessStartTrace" -Action {$Event | gm} Id Name State HasMoreData Location Command The subscription runs as a PowerShell job. Using the opening of Notepad to trigger the event we can see that data is returned. PS> Get-Job Id Name State HasMoreData Location Command And see that we have a few properties to play with. ComputerName may come in useful if we are dealing with remote machines. PS> Receive-Job -Id 2 TypeName: System.Management.Automation.PSEventArgs Name MemberType Definition The properties look similar to those we saw in the last post. Lets dig into SourceEventArgs PS> Register-WmiEvent -Query "Select * FROM Win32_ProcessStartTrace" -Action {$Event.SourceEventArgs | gm} Id Name State HasMoreData Location Command PS> Get-Job Id Name State HasMoreData Location Command PS> Receive-Job -Id 3 TypeName: System.Management.EventArrivedEventArgs Name MemberType Definition
Only thing here that look interesting is NewEvent PS> Register-WmiEvent -Query "Select * FROM Win32_ProcessStartTrace" -Action {$Event.SourceEventArgs.NewEvent | gm} Id Name State HasMoreData Location Command PS> Receive-Job -Id 4 TypeName: System.Management.ManagementBaseObject#\Win32_ProcessStartTrace Name MemberType Definition
Now we have got to the information we need. So how can we use this. Up to now we have just allowed the job to run and then picked the data from the job. One option is to write the data to the prompt as shown in this example http://blogs.msdn.com/powershell/archive/2009/08/30/exploring-wmi-with-powershell-v2.aspx. A lot of this digging was because I didn’t understand how this was put together. PowerShell really is the best way to discover how to use PowerShell!! This gets us to this script which is modified from the PowerShell Team blog
Turns out the ComputerName parameter doesn’t work but a comment on the blog shows how Jeffrey Hicks solved the problem. What we get now is a listing at our PowerShell prompt when a new process starts. We can keep working and the data comes through when the prompt is idle. Next we will look at closing a process and recording the data in a log TrackbacksThe trackback URL for this entry is: http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!2599.trak Weblogs that reference this entry
|
|
|