PowerShell for Windows Admins

Mar 27 2014   2:49PM GMT

Improving CIM/WMI method discovery

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I recently showed how to create a function that could be used to simplify the use of Get-CimClass.

In this version I’ve added some features:

- parameter validation

- namespace

- try-catch round getting the class information.

This turns the code into:

function Get-CimMethod {

[CmdletBinding()]

param (

[Parameter(Mandatory=$true)]

[Alias("Class")]

[ValidateNotNullOrEmpty()]

[string]$classname,

[ValidateNotNullOrEmpty()]

[string]$namespace = ‘root/cimv2′,

[Alias("Name")]

[ValidateNotNullOrEmpty()]

[string]$methodname

)

try

{

$class = Get-CimClass -Namespace $namespace -ClassName $classname -ErrorAction Stop

}

catch

{

Throw “Class: $classname NOT FOUND”

}

if ($methodname)

{

$class.CimClassMethods[$methodname].Parameters

}

else

{

$class.CimClassMethods

}

}

The module and function can be used like this:

Import-Module CimInvestigation -Force

Get-Command -Module CimInvestigation

Get-CimMethod -classname Win32_Process

Get-CimMethod -classname Win32_Process -methodname Create

Get-CimMethod -classname Win32_Process -methodname Create -namespace root/cimv2

 

 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: