Migrating to CIM–part 1

I have created many WMI posts on this blog over the years. With PowerShell v3 a new API has been introduced and new cmdlets are available for working with WMI. The new functionality offers many improvements and advantages over using the WMI cmdlets so I thought that a series of articles showing the evolution of a piece of WMI based PowerShell might be useful.

Much of our work with WMI is based on retrieving data e.g.

Get-WmiObject -Class Win32_ComputerSystem

In PowerShell v3 this could become

Get-CimInstance -ClassName Win32_ComputerSystem


OK that seems simple but what about using the WMI type accelerators

$HKLM = 2147483650            
$key = "SYSTEM\CurrentControlSet\Services"            
$reg = [wmiclass]'\\.\root\default:StdRegprov'            
$subkeys = $reg.EnumKey($HKLM, $key)            

These don’t exist in the CIM cmdlets.  What we do have is Invoke-CimMethod so we can do this

[uint32]$HKLM = 2147483650            
$key = "SYSTEM\CurrentControlSet\Services"            
Invoke-CimMethod -Namespace "root\default" -ClassName StdRegProv -MethodName EnumKey  -Arguments @{hDefKey=$HKLM; sSubKeyName =$key} |            
select -ExpandProperty sNames

There are a few important points we need to note:

  • The value to define the hive has to be an unsigned integer
  • we have to supply the correct argument name
  • The arguments are provided in a hash table
  • ClassName is used instead of class
  • The results are a collection that we have to expand

So how do we find this information – see part 2

This entry was posted in PowerShell and WMI, PowerShell V3. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s