{"id":326,"date":"2006-11-24T06:00:00","date_gmt":"2006-11-24T14:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/11\/24\/powershells-wmi\/"},"modified":"2006-11-24T06:00:00","modified_gmt":"2006-11-24T14:00:00","slug":"powershells-wmi","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/11\/24\/powershells-wmi\/","title":{"rendered":"PowerShell&#8217;s WMI"},"content":{"rendered":"<p><P>I know there&#8217;s a lot of confusion, with some folks, around WMI in PowerShell.<\/P><P>For example, this is the first type of WMI usage in PoSH that most folks saw:<\/P><P><STRONG>$wmi = Get-WMIObject Win32_OperatingSystem<BR>$wmi.ServicePackMajorVersion<\/STRONG><\/P><P>Cool enough. Of course, in this example, you&#8217;re only ever going to get one instance of a WMI class back; with other classes, you might be multiple instances, and so you&#8217;d write a little <STRONG>foreach<\/STRONG> loop, perhaps, to spin throught hem all. But then RC2 came out with an all-new way of working with WMI: The WMI type adapter or type accelerator. Now you could do something like this:<\/P><P><STRONG>[WMI]&#8217;\\\\Server2\\root\\cimv2:Win32_Process.Handle=&#8221;0&#8243;&#8216;<\/STRONG><\/P><P>This is a kinda strange-looking syntax if you&#8217;re coming from the VBScript world, and it essentially means, &#8220;Go to Server2. Connect to the root\\cimv2 WMI namespace, and get the instance of the Win32_Process class where the Handle property is 0.&#8221; Using this particular syntax requires you to specify a WMI &#8220;query&#8221; that returns exactly one instance, not a collection. I&#8217;m not a big fan of this syntax, but only because&#8230; well, I&#8217;m lazy. I know my VBScript syntax, and this ain&#8217;t it. I&#8217;ll tell you though, I can think of a few times in VBScript where I could have really used a WMI syntax this concise. However, in VBScript I might have executed a query like this:<\/P><P><STRONG>SELECT * FROM Win32_Process WHERE Handle = &#8216;0&#8217;<\/STRONG><\/P><P>And you know what? I can use that in PowerShell, too:<\/P><P><STRONG>$process = Get-WMIObject -query &#8220;SELECT * FROM Win32_Process WHERE Handle = &#8216;0&#8217;&#8221;<\/STRONG><\/P><P>Of course, in PowerShell it&#8217;s easier to run that agains a remote computer:<\/P><P><STRONG>$process = Get-WMIObject -query &#8220;SELECT * FROM Win32_Process WHERE Handle = &#8216;0&#8217;&#8221; -computer Server2<\/STRONG><\/P><P>A benefit of this technique &#8211; using Get-WMIObject, that is &#8211; is that the query will always return a collection. In this example, the collection would only have one instance it it, but that&#8217;s okay: The fact that it&#8217;s a collection means you&#8217;ll deal with it the same no matter how many instances it contains.<\/P><P>So does PowerShell <EM>need<\/EM> these different ways of accessing WMI? Well, it doesn&#8217;t hurt. You don&#8217;t have to <EM>learn<\/EM> them all; learn whichever one you like. But if you were to ask the guys writing Exchange Server 2007, or System Center Operations Manager 2007, I bet they love having such flexible access to WMI. <\/P><P>And you know&#8230; I can see the allure. Over time I&#8217;ll memorize this syntax the same way I memorized my current VBScript-centric syntax, and I&#8217;ll come to wonder how I lived without it.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p><P>I know there&#8217;s a lot of confusion, with some folks, around WMI in PowerShell.<\/P><P>For example, this is the first type of WMI usage in PoSH that most folks saw:<\/P><P><STRONG>$wmi = Get-WMIObject Win32_OperatingSystem<BR>$wmi.ServicePackMajorVersion<\/STRONG><\/P><P>Cool enough. Of course, in this example, you&#8217;re only ever going to get one instance of a WMI class back; with other classes, you might &#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,25],"tags":[],"class_list":["post-326","post","type-post","status-publish","format-standard","hentry","category-general","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=326"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/326\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}