In a PowerShell script that is itself a console application, I need to execute another console application with an arbitrary name, parameters, no stream redirection and get the correct return code.
This is my current code:
$execFile = "C:\Path\To\Some.exe"
$params = "arg1 arg2 /opt3 /opt4"
# Wait until the started process has finished
Invoke-Expression ($execFile + " " + $params + " | Out-Host")
if (-not $?)
{
# Show error message
}
It can start Win32 applications with the correct parameters (spaces delimit parameters, not everything goes into argv[1]). But the return code seems to be lost, and it also redirects console streams. That just doesn't work.
Another code is also wrong:
& $execFile $params
if (-not $?)
{
# Show error message
}
While this gets the return code and waits for the process to finish, it puts all parameters into argv[1] and the executed process can't work with that garbage input.
Is there another solution in PowerShell?