Discovering CIM/WMI methods and parameters

As you’ve probably gathered I spend a lot of time working with, and investigating, CIM (WMI) classes. CIM and WMI will be treated as synonymous for these articles.

If you want to discover the methods available on a CIM class you can do this:

$class = Get-CimClass -ClassName Win32_Process
$class.CimClassMethods

If you want to drill down into an individual method you then have to do this:

$class.CimClassMethods["Create"].Parameters

We end up with a two stage process. Firstly discover the available a methods and then discover the parameters on the method.

 

In a busy session this can turn into a lot of typing. You can short-circuit some that if you using the Get-CimMethodParameter from the CIM Utilities modules that can be downloaded from http://gallery.technet.microsoft.com/scriptcenter/CimUtilities-56d9dd99

To my mind it only supplies an answer to the last part of the problem. There is a hidden method – Get-CimMethod – that does the first step but as its not published it doesn’t help us.

I decided that its time to re-write the module to work the way I want.  This provides an opportunity to go over the functionality of Get-CimClass, Advanced functions and modules.

To define my problem:

I want a cmdlet that:

  • provides a list of methods on a WMI class
  • provides the parameters of a specific method
  • the standard output of Get-CimClass is acceptable
  • work with any namespace
  • work with remote machines
  • not concerned about pipeline input at this stage (may add later)
  • not concerned about outputting parameters as a hash table ( though may add later)

I’ll start with a simple function to perform the first two requirements.

function Get-CimMethod {
[CmdletBinding()]
param (
[string]$classname,

[string]$methodname

)

if ($methodname)
{
  $class = Get-CimClass -ClassName $classname
  $class.CimClassMethods[$methodname].Parameters
}
else
{
  Get-CimClass -ClassName $classname | select -ExpandProperty CimClassMethods
}

}

I decided to save this as a psm1 file (module file) and called the module CimInvestigation.

In PowerShell 3 & 4 the module autoloads so I can do this:

Get-CimMethod -classname Win32_NetworkAdapter

Get-CimMethod -classname Win32_NetworkAdapter -methodname setpowerstate

Next time I’ll turn this into more of an advanced function with some validation and error checking

Leave a Reply