I'm reading around about how to get the error from a powershell script and passing it around to the batch file calling it. The bacth file is just a Jenkins build step (Execute Win batch) and it's as simple as that:
powershell -ExecutionPolicy Unrestricted -File C:\Users\hicciuser\invoke.ps1
echo %ERRORLEVEL%
exit %ERRORLEVEL%
The powershell script is remarkably simple:
Invoke-WebRequest -Uri "https://random.url" -TimeoutSec 2
Exit $LASTEXITCODE
Turns out that, if the web request fails (times out in this case), then the %ERRORLEVEL% still seems to be 0.
If, in the script, I do Exit 1 then the %ERRORLEVEL% is set up correctly, so it seems that the Invoke-WebRequest command is not setting the right exit code on failure. Is there a simpler way to do this that I am missing?
()?