终止由调用命令启动的远程作业

发布于 2024-12-01 04:51:50 字数 597 浏览 7 评论 0原文

终止由调用命令与后台进程启动的远程作业的简单方法是什么?

我踢远程 PS 脚本来运行旧版 exe 文件,并且需要一种方法来终止该进程。

我本来打算首先使用 stop-job,但正如我正在阅读的那样,它仅适用于本地进程。

然后我打算使用 Remove-job -force 命令,但如果我理解正确的话,它无法终止正在运行的进程,直到它完成(我的远程 ps 脚本将再次启动另一个进程来运行 exe 文件)。

我正在读我可以使用 wmi终止命令杀死一个进程,但我不知道如何获取远程进程的PID(我不能使用名称,因为我可以杀死其他用户的进程)

实现我的目标的最佳方法是什么?

我的脚本看起来像这样:

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}

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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺孤伤于心 2024-12-08 04:51:50

好的,今天早上下班检查了一下,这在调用脚本中工作得很好:

get-job | remove-job -force 

我对 MSDN 文档感到困惑,它说:

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

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
凯凯我们等你回来 2024-12-08 04:51:50

看看这个答案,您可以将启动进程命令添加到脚本块中并返回pid或将其保存到文本文件以供稍后参考。

PowerShell - 获取被调用应用程序的进程 ID

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文