{"id":3601,"date":"2009-08-30T18:49:46","date_gmt":"2009-08-30T18:49:46","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/08\/30\/exploring-wmi-with-powershell-v2\/"},"modified":"2019-02-18T13:12:27","modified_gmt":"2019-02-18T20:12:27","slug":"exploring-wmi-with-powershell-v2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/exploring-wmi-with-powershell-v2\/","title":{"rendered":"Exploring WMI with PowerShell V2"},"content":{"rendered":"<p>I was messing around with my son\u2019s computer today trying to figure out why one of his programs wouldn\u2019t work.&#160; I was going to use ProcMon to monitor the processes but wasn\u2019t sure what the processes where.&#160; So first I had to figure out what processes were getting started when I ran his program.&#160; I guessed that there was a WMI event for this but as always with WMI \u2013 WHAT IS THE NAME OF THE EVENT?<\/p>\n<p>In the past, this is where things stopped.&#160; WMI was awesome but god help you if you didn\u2019t know exactly what you wanted.&#160; We made this a ton easier in PowerShell V2 so I decided to give it a try.&#160; First thing I did was to LIST all the WMIOBJECTS that had the term PROCESS in the name.&#160; I did that with this:<\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #0000ff\">PS&gt; Get-WMIObject<\/span> <span style=\"color: #000080\">-NameSpace<\/span> <span style=\"color: #8a2be2\">root<\/span> <span style=\"color: #000080\">-Recurse<\/span> <span style=\"color: #000080\">-List<\/span>  <span style=\"color: #8a2be2\">*Process*<\/span>             <\/pre>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>That gave me too many results to wade through so I needed to filter it down some more.&#160; Every WMI object as a property called __Derivation which is a list of strings showing the class hierarchy for that object.&#160; The point of that is that all WMI Event classes will contain the string \u201c__Event\u201d in this property.&#160; So then I did this:<\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #0000ff\"><\/span><\/pre>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #0000ff\">PS&gt; Get-WMIObject<\/span> <span style=\"color: #000080\">-NameSpace<\/span> <span style=\"color: #8a2be2\">root<\/span> <span style=\"color: #000080\">-Recurse<\/span> <span style=\"color: #000080\">-List<\/span>  <span style=\"color: #8a2be2\">*Process*<\/span> <span style=\"color: #a9a9a9\">|<\/span><span style=\"color: #0000ff\">Where<\/span> <span style=\"color: #000000\">{<\/span><span style=\"color: #ff4500\">$_<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Derivation<\/span> <span style=\"color: #a9a9a9\">-contains<\/span> <span style=\"color: #8b0000\">&quot;__Event&quot;<\/span><span style=\"color: #000000\">}<\/span>            <\/pre>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">&#160;&#160; NameSpace: ROOT\\CIMV2 <\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">Name&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Methods&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Properties&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#8212;-&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;-&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;&#8212;-&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>Win32_ProcessTrace&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {ParentProcessID, ProcessID, ProcessName, SECURITY&#8230;\n    <br \/>Win32_ProcessStartTrace&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {ParentProcessID, ProcessID, ProcessName, SECURITY&#8230;\n    <br \/>Win32_ProcessStopTrace&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {ExitStatus, ParentProcessID, ProcessID, ProcessNa&#8230; <\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">&#160;&#160; NameSpace: ROOT\\WMI <\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">Name&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Methods&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Properties&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#8212;-&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;-&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;&#8212;-&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>ProcessorCStateEvent&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {Active, InstanceName, SECURITY_DESCRIPTOR, TIME_C&#8230;\n    <br \/>ProcessorPerfStateEvent&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {Active, HighestState, InstanceName, SECURITY_DESC&#8230;\n    <br \/>ProcessorThrottleStateEvent&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {Active, HighestState, InstanceName, SECURITY_DESC&#8230;\n    <br \/>PortCls_IrpProcessing&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {SECURITY_DESCRIPTOR, TIME_CREATED}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>That made it pretty easy to figure out that I wanted to look at Win32_ProcessStartTrace.&#160; Just to make sure I got the class using the \u2013Amended parameter.&#160; This adds documentation to the returned object in the QUALIFIERS property.&#160; This information is normally not provided because it can be expensive to retrieve so WMI only provides it if\/when you ask for it.<\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #ff4500\">$Class<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #0000ff\">Get-WmiObject<\/span> <span style=\"color: #000080\">-List<\/span> <span style=\"color: #8a2be2\">Win32_ProcessStartTrace<\/span> <span style=\"color: #000080\">-Amended<\/span>\n<span style=\"color: #ff4500\">$Class<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Qualifiers<\/span> <span style=\"color: #a9a9a9\">|<\/span><span style=\"color: #0000ff\">ft<\/span> <span style=\"color: #8a2be2\">Name<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8a2be2\">Value<\/span> <span style=\"color: #000080\">-auto<\/span>            <\/pre>\n<p>This returned:<\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">Name&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Value\n    <br \/>&#8212;-&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8211;\n    <br \/>abstract&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; True\n    <br \/>Description The ProcessStartTrace event class indicates a new process has started.\n    <br \/>Locale&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 1033<\/font><\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>With that I was ready to go.&#160; I used the new Register-WMIEvent class to solve the problem.&#160; This takes a WMI query and can optionally take an ACTION.&#160; I decided to format the event and write the results to the host whenever a process was started.&#160; This looks a little chewy but is actually pretty straight forward. <\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #ff4500\">$Query<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">'SELECT * FROM Win32_ProcessStartTrace'<\/span>\n<span style=\"color: #ff4500\">$action<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #000000\">{<\/span>\n    <span style=\"color: #ff4500\">$e<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$Event<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">SourceEventArgs<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">NewEvent<\/span>\n    <span style=\"color: #ff4500\">$fmt<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">'ProcessStarted: (ID={0,5}, Parent={1,5}, Time={2,20}, Name=&quot;{3}&quot;)'<\/span>\n    <span style=\"color: #ff4500\">$msg<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$fmt<\/span> <span style=\"color: #a9a9a9\">-f<\/span> <span style=\"color: #ff4500\">$e<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">ProcessId<\/span><span style=\"color: #a9a9a9\">,<\/span> <span style=\"color: #ff4500\">$e<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">ParentProcessId<\/span><span style=\"color: #a9a9a9\">,<\/span> <span style=\"color: #ff4500\">$event<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">TimeGenerated<\/span><span style=\"color: #a9a9a9\">,<\/span> <span style=\"color: #ff4500\">$e<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">ProcessName<\/span>\n    <span style=\"color: #0000ff\">Write-host<\/span> <span style=\"color: #000080\">-ForegroundColor<\/span> <span style=\"color: #8a2be2\">Red<\/span> <span style=\"color: #ff4500\">$msg<\/span>\n<span style=\"color: #000000\">}<\/span>\n<span style=\"color: #0000ff\">Register-WmiEvent<\/span> <span style=\"color: #000080\">-Query<\/span> <span style=\"color: #ff4500\">$Query<\/span> <span style=\"color: #000080\">-SourceIdentifier<\/span> <span style=\"color: #8a2be2\">ProcessStart<\/span> <span style=\"color: #000080\">-Action<\/span> <span style=\"color: #ff4500\">$Action<\/span>             <\/pre>\n<p>&#160;<\/p>\n<p>This then worked a treat.&#160; Every time a process was created, I got output like this on my host:<\/p>\n<p><font color=\"#ff0000\" size=\"2\" face=\"Courier New\">ProcessStarted: (ID= 8740, Parent= 1140, Time=8\/30\/2009 3:41:54 PM, Name=&quot;Magnify.exe&quot;)\n    <br \/>ProcessStarted: (ID=13748, Parent= 1068, Time=8\/30\/2009 3:41:54 PM, Name=&quot;Utilman.exe&quot;)\n    <br \/>ProcessStarted: (ID=11964, Parent=13748, Time=8\/30\/2009 3:41:54 PM, Name=&quot;Magnify.exe&quot;)\n    <br \/>ProcessStarted: (ID= 9872, Parent= 1068, Time=8\/30\/2009 3:41:58 PM, Name=&quot;Utilman.exe&quot;)<\/font><\/p>\n<p>This worked a treat.&#160; When I was done, I just unregistered from the event.<\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #0000ff\">Unregister-Event<\/span> <span style=\"color: #000080\">-SourceIdentifier<\/span> <span style=\"color: #8a2be2\">ProcessStart<\/span>            <\/pre>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>I love to tell the story from years ago about a customer that told me they had a love\/hate relationship with WMI.&#160; <br \/>When I asked them why they loved&#160; it, they said, \u201c<em>Everything you could ever want to know is available in WMI<\/em>\u201d.\n  <br \/>When I asked them why they hated it, they said, \u201c<em>We can\u2019t FIND IT<\/em>!\u201d<\/p>\n<p>&#160;<\/p>\n<p>I strongly encourage you to spend some time learning and using the new WMI cmdlets we put into PowerShell V2, they will pay you back over and over again.<\/p>\n<p>&#160;<\/p>\n<p>Enjoy! <\/p>\n<p>Jeffrey Snover [MSFT]\n  <br \/>Distinguished Engineer\n  <br \/>Visit the Windows PowerShell Team blog at:&#160;&#160;&#160; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a>\n  <br \/>Visit the Windows PowerShell ScriptCenter at:&#160; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was messing around with my son\u2019s computer today trying to figure out why one of his programs wouldn\u2019t work.&#160; I was going to use ProcMon to monitor the processes but wasn\u2019t sure what the processes where.&#160; So first I had to figure out what processes were getting started when I ran his program.&#160; I [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3601","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>I was messing around with my son\u2019s computer today trying to figure out why one of his programs wouldn\u2019t work.&#160; I was going to use ProcMon to monitor the processes but wasn\u2019t sure what the processes where.&#160; So first I had to figure out what processes were getting started when I ran his program.&#160; I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=3601"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3601\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=3601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=3601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=3601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}