Invoke-WmiMethod

The Invoke-WmiMethod cmdlet was new with PowerShell 2. 

In PowerShell v1 we would do something like this

(gwmi win32_Process -Filter “Name=’Notepad.exe'”).Terminate()

With the cmdlet we can do this

gwmi win32_Process -Filter “Name=’Notepad.exe'” |
Invoke-WmiMethod -Name Terminate

This works OK when the method doesn’t need any arguments. When arguments are necessary you may find that using the method directly on the object works BUT it doesn’t if you use Invike-WmiMethod with the –Arguments  parameter.

One possibility is that the cmdlet needs the parameters in a different order.

If you test the arguments needed by a method like this

(gwmi win32_volume -Filter “DriveLetter = ‘c:'” ).Chkdsk.OverLoadDefinitions

You will get the order shown in the documentation

if you use

(gwmi win32_volume -Filter “DriveLetter = ‘c:'” ).GetMethodParameters(“Chkdsk”)

You may find you get different results for the order of the parameters. Use the order given by this technique with the Invoke-WmiMethod

Advertisements
This entry was posted in PowerShell and WMI. 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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s