Output redirection fails when using Start-Process to launch CMD with START /B
This is my test.ps1
:
Start-Process -FilePath 'CMD.EXE' -NoNewWindow -ArgumentList '/C START /B PING.EXE google.com -t > logfile.txt 2>&1'
echo "Finished."
The test is an infinite ping to google, which is a good test for the conditions that I need to check:
- Open the process in a new detached thread (= see the 'Finished' printed out + see PING.EXE alive in the process list + CTRL+C not killing PING.exe) [OK]
- Collect the ping progress (that is: redirect standard input and standard error) to
logfile.txt
[FAILS] <= this solution does not seem to work on my test,logfile.txt
stays at 0 bytes
It is a convoluted solution, even though my final goal is relatively simple. What I finally want is to reproduce this linux cmd in powershell:
ASPNETCORE_ENVIRONMENT=Production ASPNETCORE_URLS=http://*:5000 dotnet run > ${LOGFILE_PATH}.txt 2>&1 &
The difficult part in Windows seems to be to "really detach the process". START /B
is the only windows functionality that I have found that really does that. But then I have the "output redirection" problem. And since I need it in a .ps1 file, I need to wrap it in Start-Process
.
You might be searching for one or more of the following Start-Process parameters:
-RedirectStandardOutput
Specifies a file. This cmdlet sends the output generated by the process to a file that you specify. Enter the path and filename. By default, the output is displayed in the console.
-NoNewWindow
Start the new process in the current console window. By default PowerShell opens a new window.