{"id":6361,"date":"2008-04-15T09:30:00","date_gmt":"2008-04-15T09:30:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2008\/04\/15\/wmi-object-identifiers-and-keys\/"},"modified":"2019-02-18T13:16:02","modified_gmt":"2019-02-18T20:16:02","slug":"wmi-object-identifiers-and-keys","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/wmi-object-identifiers-and-keys\/","title":{"rendered":"WMI Object Identifiers and Keys"},"content":{"rendered":"<p>Recently one of MVPs, <a href=\"http:\/\/www.windowsitpro.com\/Authors\/Index.cfm?AuthorID=546\">Darren Mar-Elia<\/a> (Group Policy Guru from <a href=\"http:\/\/www.sdmsoftware.com\/\">SDMSoftware<\/a> [which as a set of FREE PowerShell GP cmdlets <a href=\"http:\/\/www.sdmsoftware.com\/freeware.php\">HERE<\/a>])&nbsp; was working with our WMI type accelerators and got the following error:<\/p>\n<p><font face=\"Courier New\" size=\"2\">$ld = &#8216;\\\\sdmlaptop1\\root\\cimv2:Win32_LogicalDisk.Caption=&#8221;C:&#8221;&#8216;<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">$disk = [WMI] $ld<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">Cannot convert value &#8220;<\/font><a href=\"Win32_LogicalDisk.Caption=\"><font face=\"Courier New\" size=\"2\">\\\\sdmlaptop1\\root\\cimv2:Win32_LogicalDisk.Caption=<\/font><\/a><font face=\"Courier New\" size=\"2\">&#8220;C:&#8221;&#8221; to type &#8220;System.Management.ManagementObject&#8221;. Error: &#8220;Invalid object path &#8220;<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">At line:1 char:17<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">+ $disk = [WMI] $ld &lt;&lt;&lt;&lt;<\/font><\/p>\n<p>This is a great opportunity to discuss <strong>WMI Key properties<\/strong>.&nbsp; WMI is an awesome piece of infrastructure.&nbsp; It has a rich object metamodel which includes metadata on properties.&nbsp; What does that mean?&nbsp; Think of it as Reflection+++.&nbsp;&nbsp; It means that you can ask WMI a rich set of questions about the object\/type you have.&nbsp; In this example, you can ask WMI which of the properties of a class can be used as a KEY, in other words as an object identifier.&nbsp; <\/p>\n<p>[<strong>NOTE<\/strong>: These demos are all using the V2 CTP syntax.&nbsp; If you want to do the same thing on V1, just put the term &#8220;psbase.&#8221; in front of &#8220;properties&#8221;&nbsp;.&nbsp; e.g. instead of $c.Properties&nbsp; you&#8217;ll type $c.psbase.Properties &#8211; jps]<\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt;$c=[wmiclass]&#8221;win32_logicaldisk&#8221; <br \/>PS&gt;$c.Properties[&#8220;caption&#8221;] <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : caption <br \/>Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <br \/>Type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : String <br \/>IsLocal&nbsp;&nbsp;&nbsp; : False <br \/>IsArray&nbsp;&nbsp;&nbsp; : False <br \/>Origin&nbsp;&nbsp;&nbsp;&nbsp; : CIM_ManagedSystemElement <br \/>Qualifiers : {CIMTYPE, MaxLen, read} <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt;$c.Properties[&#8220;deviceid&#8221;] <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : deviceid <br \/>Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <br \/>Type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : String <br \/>IsLocal&nbsp;&nbsp;&nbsp; : False <br \/>IsArray&nbsp;&nbsp;&nbsp; : False <br \/>Origin&nbsp;&nbsp;&nbsp;&nbsp; : CIM_LogicalDevice <br \/>Qualifiers : {CIM_Key, CIMTYPE, <strong><font color=\"#ff0000\">key<\/font><\/strong>, MappingStrings&#8230;}<\/font><\/p>\n<p>Here is a function I wrote to make this a little easier:<\/p>\n<p>function Get-WmiKey <br \/>{ <br \/>&nbsp; $class = [wmiclass]$args[0] <br \/>&nbsp; $class.Properties | <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Select @{Name=&#8221;PName&#8221;;Expression={$_.name}} -Expand Qualifiers | <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Where {$_.Name -eq &#8220;key&#8221;} | <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach {$_.Pname} <br \/>}<\/p>\n<p>PS&gt;Get-WmiKey Win32_LogicalDisk <br \/>DeviceID <br \/>PS&gt;Get-WmiKey Win32_Process <br \/>Handle <br \/>PS&gt;Get-WmiKey Win32_Service <br \/>Name <br \/>PS&gt;<\/p>\n<p>So if we believe what this is telling us, it means that the Darren&#8217;s original string should have used the &#8220;DeviceID&#8221; property instead of the &#8220;Caption&#8221;.&nbsp; Let&#8217;s see if that works.<\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt;$ld = &#8216;\\\\jpslap11\\root\\cimv2:Win32_LogicalDisk.Caption=&#8221;C:&#8221;&#8216; <br \/>PS&gt;[wmi]$ld <br \/><font color=\"#ff0000\">Cannot convert value &#8220;\\\\jpslap11\\root\\cimv2:Win32_LogicalDisk.Caption=&#8221;C:&#8221;&#8221; to type &#8220;System.Management.ManagementObject <br \/>&#8220;. Error: &#8220;Invalid object path &#8221; <br \/>At line:1 char:9 <br \/>+ [wmi]$ld &lt;&lt;&lt;&lt; <br \/><\/font>PS&gt;$ld = &#8216;\\\\jpslap11\\root\\cimv2:Win32_LogicalDisk.DeviceID=&#8221;C:&#8221;&#8216; <br \/>PS&gt;[wmi]$ld <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">DeviceID&nbsp;&nbsp;&nbsp;&nbsp; : C: <br \/>DriveType&nbsp;&nbsp;&nbsp; : 3 <br \/>ProviderName : <br \/>FreeSpace&nbsp;&nbsp;&nbsp; : 18366902272 <br \/>Size&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 85446352896 <br \/>VolumeName&nbsp;&nbsp; : PowerShell Rocks!<\/font><\/p>\n<p>As I mentioned, WMI is an awesome technology.&nbsp; Key properties are just the beginning of it.&nbsp; Check out the other WMI property qualifiers <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms799861.aspx\">HERE<\/a> and start experimenting!<\/p>\n<p>In closing let me say that throughout the history of PowerShell, a small subset of people have looked at it and said, &#8220;WMI is dead&#8221;.&nbsp; I must tell you that I&#8217;ve never understood what they were talking about.&nbsp; I LOVE WMI.&nbsp; PowerShell is a direct outgrowth of the work I did on the WMI command line.&nbsp; If you&#8217;ve played with our CTP release, you&#8217;ll see that we are investing very heavily in supporting WMI and making WMI a REALLY GOOD CHOICE for surfacing your management information (you&#8217;ll see that even more in the next drop of the CTP [ what you&#8217;ll be able to do with WMI will BLOW YOUR SOCKS OFF &#8211; and we&#8217;ll be doing even more!]).<\/p>\n<p>Cheers!<\/p>\n<p>Jeffrey Snover [MSFT] <br \/>Windows Management Partner Architect <br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a> <br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <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>Recently one of MVPs, Darren Mar-Elia (Group Policy Guru from SDMSoftware [which as a set of FREE PowerShell GP cmdlets HERE])&nbsp; was working with our WMI type accelerators and got the following error: $ld = &#8216;\\\\sdmlaptop1\\root\\cimv2:Win32_LogicalDisk.Caption=&#8221;C:&#8221;&#8216; $disk = [WMI] $ld Cannot convert value &#8220;\\\\sdmlaptop1\\root\\cimv2:Win32_LogicalDisk.Caption=&#8220;C:&#8221;&#8221; to type &#8220;System.Management.ManagementObject&#8221;. Error: &#8220;Invalid object path &#8220; At line:1 char:17 [&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":[26],"class_list":["post-6361","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Recently one of MVPs, Darren Mar-Elia (Group Policy Guru from SDMSoftware [which as a set of FREE PowerShell GP cmdlets HERE])&nbsp; was working with our WMI type accelerators and got the following error: $ld = &#8216;\\\\sdmlaptop1\\root\\cimv2:Win32_LogicalDisk.Caption=&#8221;C:&#8221;&#8216; $disk = [WMI] $ld Cannot convert value &#8220;\\\\sdmlaptop1\\root\\cimv2:Win32_LogicalDisk.Caption=&#8220;C:&#8221;&#8221; to type &#8220;System.Management.ManagementObject&#8221;. Error: &#8220;Invalid object path &#8220; At line:1 char:17 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6361","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=6361"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6361\/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=6361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=6361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=6361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}