Search code examples
powershellpowershell-remoting

remote process getting killed mysteriously


I am running a remote process using PowerShell and it seems to get killed without trace after a few minutes.

The command I'm using follows :

$command = {Start-Process -NoNewWindow -Wait -FilePath $filepath -ArgumentList $args}
Invoke-Command -Session $MyPSSession -ScriptBlock $command

While remote-desktopping the machine, I look at the Task Manager: the process is in the list and takes as much memory and CPU resources as usual (i.e. as if I executed the same command directly on the machine instead of using Invoke-Command), but after 3 to 6 minutes the process stops without any trace.

Is there some way to prevent this behavior? My process is supposed to run a long amount of time (more than 10 minutes) on average and it gets killed anyways.

Edit 1 : Alright so I debugged that issue with procmon and it seems like just before dying the process launches an Exit code 3. I looked it up and it's "System could not find path specified". It is mysterious yet again because running the process directly on the machine would not trigger such exit code.

...Then would there be a way to know which path the process was looking for?

Edit 2 : There is indeed a way to know which path the process failed to access, but in my case there are thousands (!!!) of events logged by procmon that had the result "NAME NOT FOUND" on operation "RegOpenKey" and such. Also, it seems like my remote machine has a hard time with the CreateFileMapping function, which always return "FILE LOCKED WITH ONLY READERS", no matter which dll it's trying to "map"...

Edit 3 : I found out that entering the session created beforehand and then run the process changed the behavior, but the process still gets killed some time after being started, this time with an Exit code of '-1073741819' ... which is "Access Denied". Will investigate further.

Edit 4 : On the remote machine, the PSversion was 2.0. On my workstation, the PSversion was 5.0. I installed Service Pack 1, .NET Framework 4.5.2 and WMF 5.0 (contains powerShell 5.0). Until today the process worked fine and did not die mysteriously, but now it does spit an Exit code 3 after a few seconds. I tried to run another app on my remote machine : I got it to finish and output its results even if the process took longer than those few seconds. "Then it must be the app!" I told myself... nope! when I run the same app with the same parameters from the VM to my workstation, it works just fine. "It must be the remote machine then!" ... yeah, about that. How would I be able to identify the differences between those two machines and fix the remote one so I can run my app on it from my workstation?


Solution

  • On the remote machine, the PSversion was 2.0. On my workstation, the PSversion was 5.0. I installed Service Pack 1, .NET Framework 4.5.2 and WMF 4.0 (includes PowerShell 4.0) on the remote machine, and then I started my process remotely from my workstation and it is more healthy than ever.

    Conclusion : Using PowerShell 2.0 for remoting is not a good solution for long-term processes and will surely cause problems later on, especially if the receiving machine is running an old version of PS.

    Lesson of the week : Update your stuff, guys.