Description
As part of a an open-source project, I am writing a script that will allow to host Jenkins agent as a service.
Jenkins is running on Java, so I had to call Java.exe.
What I Tried
For this, I need to make sure that when the script is being terminated, the java process is being terminated as well.
I found that using CMD /c or Start-Process is starting a new process, and because of this, stopping the PowerShell script does not stop the java.exe as well.
The only thing that worked was the call operator, &.
However, as you see below it treats the infomration returned from JAVA as an exception. IT IS FALSE-POSITIVE.
What I tried and Need
I need to make sure the actual information is being sent to output as usual, not hidden.
This rules out $ErrorActionPreference = 'SilentlyContinue'
Also tried 2>&1 and *>&1 but same result
Any ideas will be great.
The Code
& "$Java\java.exe" -jar "$JenkinsPath\agent.jar" -url "$JenkinsURL/" -secret $Secret -name $env:computername -workDir "$JenkinsPath" *>>"$JenkinsPath\agent.log"
The Error
java.exe : Aug 04, 2024 5:14:14 PM org.jenkinsci.remoting.engine.WorkDirManager initializeWorkDir
At line:1 char:1
+ & "$Java\java.exe" -jar "$JenkinsPath\agent.jar" -url "$JenkinsURL/" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Aug 04, 2024 5:...itializeWorkDir:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
INFO: Using D:\Jenkins\remoting as a remoting work directory
Aug 04, 2024 5:14:14 PM org.jenkinsci.remoting.engine.WorkDirManager setupLogging
INFO: Both error and output logs will be printed to D:\Jenkins\remoting
Aug 04, 2024 5:14:14 PM hudson.remoting.Launcher createEngine
Thanks in advance.


jave.exeviacmd /Cisn't necessary here, but with respect to terminating the PowerShell process running the script there should be no difference. By contrast, aStart-Process-launched process is an independent process (not a child process of the caller) that by default runs asynchronously and in a new window. Only in unusual situations isStart-Processcalled for; direct invocation (via&or just by name / path, if it isn't quoted and contains no variables) is almost always the better solution.