Categories

I’m afraid you can’t do that anymore

In PowerShell 1.0 you could do this:

notepad
$proc = Get-WmiObject -Class Win32_Process -Filter "Name='notepad.exe'"
$proc.Terminate()

To access the methods of the WMI class you had to get a variable representing the instance and call the method. This technique still works in PowerShell 4.0

When the CIM cmdlets were introduced in PowerShell 3.0 people assumed that they worked the same:

£> notepad
£> $proc = Get-CimInstance -ClassName Win32_Process -Filter "Name='notepad.exe'"
£> $proc.Terminate()
Method invocation failed because [Microsoft.Management.Infrastructure.CimInstance] does not contain a method named
'Terminate'.
At line:1 char:1
+ $proc.Terminate()
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Terminate:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

The CIM cmdlets retrun inert objects – NO METHODS – so you can’t do that.

The correct way to use the CIM cmdlets is:

Get-CimInstance -ClassName Win32_Process -Filter "Name='notepad.exe'" | Invoke-CimMethod -MethodName Terminate

Leave a Reply