My friends asked me simple question: How to stop (terminate) processes on a remote server?
Of course you can use:
Invoke-Command -ComputerName RemoteServer
or
Invoke-Command -Session $existingSessionToRemoteServer
But in this case my friends had established CIM session in their script. I wrote a few simple examples how to stop process using WMI and CIM and if you add -ComputerName (for Get-WmiObject) or -CimSession (for Get-CimInstance) then you can do it on a remote server.
WMI on local computer
(Get-WmiObject Win32_Process -Filter "Name = 'calc.exe'").Terminate()
CIM on remote computer
$processItems = Get-CimInstance ` -ClassName Win32_Process ` -Filter "Name = 'calc.exe'" ` -CimSession 'MyRemoteServer.ad.contoso.com' if ($processItems ) { # This will not work # $a.Terminate() foreach ($processItem in $processItems) { Write-Verbose -Message ('{0}: Terminate process: {1} ({2})' -f $processItem.Name, $processItem.ProcessId) -Verbose $result = Invoke-CimMethod -InputObject $processItem -MethodName Terminate if ($result.ReturnValue -ne 0) { Write-Error -Message ('Error durring trial to terminate process; ReturnValue: {0}' -f $result.ReturnValue) } } } else { Write-Verbose -Message 'Nothing to terminate' -Verbose }