Greetings,
I’m trying to figure out how to write a script that starts a process on the local computer, but in a separate process tree. I need the process’s standard out and error to still stream to the console, and I need the script to wait for and exit with the process’s exit code. I need the script to not wait for or terminate any descendants the process properly backgrounds. It would be nice if the process inherited the script’s working directory and environment, but I could specify the required working directory and the few required environment variables as arguments if need be.
I’ve tried Start-Process. That seems like the closest thing to what I need, particularly when used with Wait-Process instead of -Wait (as the former doesn’t wait on descendant processes). The only problem I’m having with Start-Process is that I don’t see a way to specify that the process not be a descendant of my script’s process tree. I don’t have control over the system that calls my script, and it does seem to “-Wait” on all my script’s descendants before proceeding. That’s what I need to overcome.
I’ve tried Start-Job, but that also seems to start the process as a descendant.
It does appear that Invoke-Process would start a separate process tree, but it seems to require elevated privileges (that I don’t have and wouldn’t be able to obtain), even when running a local command:
> Invoke-Command -ComputerName . -ScriptBlock { echo "Testing" }
[localhost] Connecting to remote server localhost failed with the following error message : Access is denied. For more
information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (localhost:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken
I’ve also looked at Invoke-CimMethod and Win32_Process.Create, as suggested in a StackOverflow post. This seems like it might be my best bet, but how do I redirect the process’s standard out and error back to the script, and exit the script with the process’s exit code?
My context is a Bamboo continuous integration server PowerShell script task that invokes my script, which in turn invokes the Gradle build tool via its Gradle Wrapper .bat script. How exactly Bamboo invokes script tasks is a black box, but it seems to “-Wait” instead of “Wait-Process”. The Gradle Wrapper typically backgrounds a daemon process that outlives a single build. Even when I use Wait-Process in my script, and return an exit code as specified in Bamboo’s knowledge base article on this issue, my jobs still hang until the Gradle Daemon exits after its 3-hour idle timeout. They suggest an alternative workaround of disabling the Gradle Daemon, but that’s not how Gradle is primarily designed to be used, and I keep running into reliability issues with that approach. It also has negative performance implications.
If I can start the Gradle Wrapper from my script as a separate process tree, so Bamboo doesn’t wait on the Gradle Daemon; still log the Gradle Wrapper’s output, so Bamboo will include it in the logs for the build job; and exit my script with the Wrapper’s exit code, so Bamboo knows whether the build succeeded or failed; I believe that’ll solve my problem.