PowerShell for Windows Admins

Mar 23 2014   4:51AM GMT

Discovering CIM/WMI methods and parameters

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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

 

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: