在远程会话中启动后台任务,当会话被删除时不会被终止 [英] Launching background tasks in a remote session that don't get killed when the session is removed

查看:37
本文介绍了在远程会话中启动后台任务,当会话被删除时不会被终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 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?

推荐答案

首先解释一下它为什么会这样.也许其他人可以用它带来另一种解决方案.

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

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

I edited my answer with solution based on 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 的进程,如下所示

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

当你在这个远程会话中简单地启动一个进程时:

When you simply start a process in this remote session :

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

新进程是 wsmprovhost.exe 的子进程,如下所示

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

如果你停止远程会话 wsmprovhost.exe 就会消失,所以子进程.

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

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

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

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

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.

可能存在配置 WinRM 以支持支持 JOB_OBJECT_LIMIT_BREAKAWAY_OK 的作业的解决方案.

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

所以阅读 微软文档,我找到了一种记录在案的技术方法,可以让您通过 WinRM 启动程序,但在其他工作中.默认情况下,与作业关联的进程使用 CreateProcess 创建的进程与作业关联;但是,使用 Win32_Process.Create 创建的进程与作业无关.

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.

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

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

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

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

使用 WMI 启动的进程不属于任何 Job.用法语我会说Ce qu'il fallait démontrer"

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

这篇关于在远程会话中启动后台任务,当会话被删除时不会被终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆