Quantcast
Channel: WMI – PowerShell for Windows Admins
Viewing all articles
Browse latest Browse all 140

I’m afraid you can’t do that anymore

$
0
0

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

 


Viewing all articles
Browse latest Browse all 140

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>