Search code examples
windowspowershellkill-processinvoke-command

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}

Solution

  • 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