mishkin
mishkin

Reputation: 6212

kill remote job initiated by invoke-command

what is the easy way to kill remote job initiated by invoke-command with background processes?

I kick remote PS script to run legacy exe file and need a way to kill that process.

I was going to use stop-job first but as I am reading it works only for local processes.

Then I was going to use Remove-job -force command but if I understood right, it cannot kill running process until it completes (again my remote ps script will start another process to run exe file).

I am reading I can kill a process using wmi terminate command but I do not know how to get PID of remote process (I cannot use name because I can kill processes from other users)

what is the best way to achieve my goal?

my script looks like that:

invoke-command -computername server1,server2,server3..etc -script-block {my.exe} -asjob

loop to wait for all processes to complete and report progress

   check for CTRL-C press and kill all remote instances on {my.exe}

Upvotes: 0

Views: 2133

Answers (2)

mishkin
mishkin

Reputation: 6212

ok checked this morning from work and this works fine from a calling script:

get-job | remove-job -force 

I was confused by MSDN doc which says:

When you stop a background job, Windows PowerShell **completes** all tasks that are pending in that job queue and then ends the job

Upvotes: 1

Matt
Matt

Reputation: 1969

Check out this answer, you can add the start process command to your script block and return the pid or save it to a text file to be reference later.

PowerShell - get process ID of called application

Upvotes: 0

Related Questions