在远程会话中启动后台任务,当会话被删除时,这些后台任务不会被终止

发布于 2024-12-23 07:52:38 字数 289 浏览 2 评论 0原文

我一直在使用 PsExec -d 在远程 powershell 会话中启动控制台应用程序,因为我希望这些应用程序在我执行某些任务时在后台运行。问题是,即使我使用 Remove-PSSession 终止远程 powershell 会话,我也希望后台应用程序继续运行。当前发生的情况是,一旦远程 powershell 会话被终止,所有在 PsExec -d 帮助下启动的进程也会被终止。我猜这与进程树以及 Windows 如何管理此类事物的生命周期有关。

有谁知道如何启动远程后台进程并让该进程即使在远程会话被终止后仍然存在?

I have been using PsExec -d to launch console applications in a remote powershell session because I want these apps to run in the background while I perform some task. The problem is that I want the background applications to continue running even if I kill the remote powershell session with Remove-PSSession. What happens currently is once the remote powershell session is killed so are all the processes that were started with the help of PsExec -d. I'm guessing it has something to do with process trees and how windows manages the lifetime of such things.

Does anyone have any idea how I can launch a remote background process and have that process persist even after the remote session is killed?

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

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

发布评论

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

评论(1

颜漓半夏 2024-12-30 07:52:38

首先解释一下它为什么如此工作。也许其他人可以用它来带来另一种解决方案。

我使用基于 WMI 的解决方案编辑了我的答案。

当您进入远程会话时:

PS C:\Users\JPB> enter-PSSession -ComputerName 192.168.183.100 -Credential $cred
[192.168.183.100]: PS C:\Users\jpb\Documents>

您在服务器上创建一个名为 wsmprovhost.exe 的进程,如

下所示“在此处输入图像描述”

当您仅在此远程会话中启动进程时:

[192.168.183.100]: PS C:\Users\jpb\Documents> Start-Process calc.exe

下所示

新进程是 wsmprovhost.exe 的子进程,如在此处输入图像描述

如果停止远程会话,wsmprovhost.exe 就会消失,子进程也会消失。

解释是 wsmprovhost.exe 和由此启动的所有进程属于同一个作业。

在此处输入图像描述

默认情况下,一方面此作业不支持 JOB_OBJECT_LIMIT_BREAKAWAY_OK 限制标志不允许我们使用 CREATE_BREAKAWAY_FROM_JOB 标志启动进程,但另一方面,此作业支持JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE 限制标志,导致与作业关联的所有进程在作业的最后一个句柄关闭时终止。

也许存在一种解决方案来配置 WinRM 以支持支持 JOB_OBJECT_LIMIT_BREAKAWAY_OK 的作业。


编辑:

所以阅读 Microsoft 文档< /a>,我找到了一种记录在案的技术方法,供您通过 WinRM 启动程序,但在另一项工作中。默认情况下,由与作业关联的进程使用 CreateProcess 创建的进程与该作业关联;但是,使用Win32_Process.Create创建的进程与作业无关

因此,如果在远程会话中您使用 WMI 创建一个进程,如下所示:

PS C:\silogix> $ps = New-PSSession -ComputerName 192.168.183.100 -Credential $cred
PS C:\silogix> Enter-PSSession -Session $ps
[192.168.183.100]: PS C:\Users\jpb\Documents> Invoke-WmiMethod -path win32_process -name create -argumentlist "calc.exe"



__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 2
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
ProcessId        : 1236
ReturnValue      : 0

[192.168.183.100]: PS C:\Users\jpb\Documents> exit
PS C:\silogix> Remove-PSSession $ps

如果您停止远程会话,wsmprovhost.exe 就会消失,但新进程仍保留在服务器上,如下所示:

在此处输入图像描述

使用 WMI 启动的进程不属于任何作业。用法语我会说“Ce qu'il Fallait démontrer”

Here is first an explanation of why it works so. Perhaps someone else can use it to bring a another solution.

I edited my answer with solution based on WMI.

When you enter a remote session :

PS C:\Users\JPB> enter-PSSession -ComputerName 192.168.183.100 -Credential $cred
[192.168.183.100]: PS C:\Users\jpb\Documents>

You create on the server a process called wsmprovhost.exe as shown here under

enter image description here

When you simply start a process in this remote session :

[192.168.183.100]: PS C:\Users\jpb\Documents> Start-Process calc.exe

The new process is a child of wsmprovhost.exe as shown here under

enter image description here

If you stop the remote session wsmprovhost.exe disappeared and so the child process.

The explanation is that wsmprovhost.exe and all the processes started by this one belongs to the same job.

enter image description here

By default, on one hand this job DOES NOT supports JOB_OBJECT_LIMIT_BREAKAWAY_OK limit flag that does not allow us to start a process with CREATE_BREAKAWAY_FROM_JOB flag, on the other hand this job supports JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE limit flag that causes all processes associated with the job to terminate when the last handle to the job is closed.

It perhaps exists a solution to configure WinRM to support jobs which supports JOB_OBJECT_LIMIT_BREAKAWAY_OK.


Edited :

So reading Microsoft documentation, I found a documented technical way for you to start a program through WinRM but in an onother job. By default, processes created using CreateProcess by a process associated with a job are associated with the job; however, processes created using Win32_Process.Create are not associated with the job.

So if in you remote session you create a process with WMI like this :

PS C:\silogix> $ps = New-PSSession -ComputerName 192.168.183.100 -Credential $cred
PS C:\silogix> Enter-PSSession -Session $ps
[192.168.183.100]: PS C:\Users\jpb\Documents> Invoke-WmiMethod -path win32_process -name create -argumentlist "calc.exe"



__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 2
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
ProcessId        : 1236
ReturnValue      : 0

[192.168.183.100]: PS C:\Users\jpb\Documents> exit
PS C:\silogix> Remove-PSSession $ps

If you stop the remote session wsmprovhost.exe disappeared, but the new process stay on the server as show here under :

enter image description here

The processes started with WMI does not belongs to any Job. In french I would say "Ce qu'il fallait démontrer"

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