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.
I edited my answer with solution based on WMI.
When you enter a remote session :
You create on the server a process called
wsmprovhost.exe
as shown here underWhen you simply start a process in this remote session :
The new process is a child of
wsmprovhost.exe
as shown here underIf 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.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 withCREATE_BREAKAWAY_FROM_JOB
flag, on the other hand this job supportsJOB_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 :
If you stop the remote session wsmprovhost.exe disappeared, but the new process stay on the server as show here under :
The processes started with WMI does not belongs to any Job. In french I would say "Ce qu'il fallait démontrer"