I have two requirements:
1. Call a PS script from a batch script but PS Window could be suppressed or appear and disappear
2. The Batch Script needs to evaluate the Exit code from PS before terminating.
3. Like to avoid running an VB script or edit the registry
The above seems to be simple enough but i cannot get it to work for the life of me..!!!
Here is what i tried:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPath%""' -Verb RunAs}";
Now the above meets requirement 1 BUT the Batch Script does not wait for the PS to finish and does not capture the Exit code.
In the PS Script i am doing this - forcing it to end with 1 just as a test:
#exit $LASTEXITCODE
exit 1
and in the Batch script i have this soon after the call to the PS Script:
if errorlevel 1 goto FAIL
GOTO SUCCESS
The problem is that the Batch script terminates even before the PS Window closes and the errorlevel is ALWAYS 0...so a SUCCESS generated everytime even though i forced a 1 in PS...!!!
I have tried variations of switches like Bypass and -WindowStyle Hidden in the call to PS but nothing appears to work - in fact using these switches actually keeps the PS Window open or worse - closes the calling window.
Here is what i tried for the Open Window issue:
The below keeps the PS window open BUT closes the CALLING batch window.
PowerShell -WindowStyle Hidden -NoProfile -ExecutionPolicy -Command "& '%PowerShellScriptPath%'";
The below keeps both windows open BUT the return code is always 0 in the Batch script.
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'"