Improving CIM/WMI method discovery

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

This entry was posted in CIM, PowerShell and WMI, PowerShell V3, PowerShell v4. 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