WMI Object Identifiers and Keys

Recently one of MVPs, Darren Mar-Elia (Group Policy Guru from SDMSoftware [which as a set of FREE PowerShell GP cmdlets HERE])  was working with our WMI type accelerators and got the following error:


$ld = ‘\\sdmlaptop1\root\cimv2:Win32_LogicalDisk.Caption=”C:”‘


$disk = [WMI] $ld


Cannot convert value “\\sdmlaptop1\root\cimv2:Win32_LogicalDisk.Caption=“C:”” to type “System.Management.ManagementObject”. Error: “Invalid object path “


At line:1 char:17


+ $disk = [WMI] $ld <<<<


This is a great opportunity to discuss WMI Key properties.  WMI is an awesome piece of infrastructure.  It has a rich object metamodel which includes metadata on properties.  What does that mean?  Think of it as Reflection+++.   It means that you can ask WMI a rich set of questions about the object/type you have.  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. 


[NOTE: These demos are all using the V2 CTP syntax.  If you want to do the same thing on V1, just put the term “psbase.” in front of “properties” .  e.g. instead of $c.Properties  you’ll type $c.psbase.Properties – jps]


PS>$c=[wmiclass]”win32_logicaldisk”
PS>$c.Properties[“caption”]


Name       : caption
Value      :
Type       : String
IsLocal    : False
IsArray    : False
Origin     : CIM_ManagedSystemElement
Qualifiers : {CIMTYPE, MaxLen, read}


PS>$c.Properties[“deviceid”]


Name       : deviceid
Value      :
Type       : String
IsLocal    : False
IsArray    : False
Origin     : CIM_LogicalDevice
Qualifiers : {CIM_Key, CIMTYPE, key, MappingStrings…}


Here is a function I wrote to make this a little easier:


function Get-WmiKey
{
  $class = [wmiclass]$args[0]
  $class.Properties |
      Select @{Name=”PName”;Expression={$_.name}} -Expand Qualifiers |
      Where {$_.Name -eq “key”} |
      foreach {$_.Pname}
}


PS>Get-WmiKey Win32_LogicalDisk
DeviceID
PS>Get-WmiKey Win32_Process
Handle
PS>Get-WmiKey Win32_Service
Name
PS>


So if we believe what this is telling us, it means that the Darren’s original string should have used the “DeviceID” property instead of the “Caption”.  Let’s see if that works.


PS>$ld = ‘\\jpslap11\root\cimv2:Win32_LogicalDisk.Caption=”C:”‘
PS>[wmi]$ld
Cannot convert value “\\jpslap11\root\cimv2:Win32_LogicalDisk.Caption=”C:”” to type “System.Management.ManagementObject
“. Error: “Invalid object path ”
At line:1 char:9
+ [wmi]$ld <<<<
PS>$ld = ‘\\jpslap11\root\cimv2:Win32_LogicalDisk.DeviceID=”C:”‘
PS>[wmi]$ld


DeviceID     : C:
DriveType    : 3
ProviderName :
FreeSpace    : 18366902272
Size         : 85446352896
VolumeName   : PowerShell Rocks!


As I mentioned, WMI is an awesome technology.  Key properties are just the beginning of it.  Check out the other WMI property qualifiers HERE and start experimenting!


In closing let me say that throughout the history of PowerShell, a small subset of people have looked at it and said, “WMI is dead”.  I must tell you that I’ve never understood what they were talking about.  I LOVE WMI.  PowerShell is a direct outgrowth of the work I did on the WMI command line.  If you’ve played with our CTP release, you’ll see that we are investing very heavily in supporting WMI and making WMI a REALLY GOOD CHOICE for surfacing your management information (you’ll see that even more in the next drop of the CTP [ what you’ll be able to do with WMI will BLOW YOUR SOCKS OFF – and we’ll be doing even more!]).


Cheers!


Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx