Question : Remotely killing processes on a remote server

I have a script that will query a remote server looking for a specific process, and I have it returning a list of the PIDs and I want to send a kill command to each of the threads but I'm having trouble getting it to work.

When it gets to this tline:

            ([WMI]"\\$srv\root\cimv2:Win32_Process.Handle=$id".Terminate())

It errors out saying there is no Terminate method, but when I run the same command from the cmdline - it works.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
$servers =
          'server1',
          'server2',
          'server3'
	
foreach ($srv in $servers)
{
# Stop the services
# Step 1 - stop the service
if (-not ([appdomain]::CurrentDomain.getassemblies() |? {$_.ManifestModule -like "system.serviceprocess"})) {[void][System.Reflection.Assembly]::LoadWithPartialName('system.serviceprocess')}
$getSRV = [System.ServiceProcess.ServiceController]::GetServices($srv)
(new-Object System.ServiceProcess.ServiceController($service,$srv)).Stop()
(new-Object System.ServiceProcess.ServiceController($service,$srv)).WaitForStatus('Stopped',(new-timespan -seconds 30))
Write-Host $service " stopped on " $srv
 
# Step 2 - Kill remaining threads
$killList = $null
$killList = @()
$process = [System.Diagnostics.Process]
$EASProc = $Null
$EASProc = $process::GetProcesses($srv) | where {$_.ProcessName -like "eassrvr"}
if (!$EASProc){Write-Host "SKIPPING " $srv}
if ($EASProc)
	{
	foreach ($proc in $EASProc)
		{
		$killList += $proc.ID
		}
	if($killList)
		{
		foreach ($id in $killList)
		{
	([WMI]"\\$srv\root\cimv2:Win32_Process.Handle=$id".Terminate())
		Write-Host "Need to kill..." $id
		}
	}
}
}
Open in New Window Select All

Answer : Remotely killing processes on a remote server

I believe the problem there is that $processes is not what you expect it is. It is probably returning an array.

You can try this
$processes | %{$_.Terminate()}
Random Solutions  
 
programming4us programming4us