Share via


Return LASTEXITCODE to Scheduled Tasks from PowerShell script

Question

Wednesday, December 1, 2010 1:38 PM

I'm using Scheduled Tasks to run a powershell script that will invoke a win32 app. If the win32 app exits with an error, I would like that error to be passed up to the task scheduler so that it reflects in the Last Result column of the Scheduled Tasks window. Using exit $LASTEXITCODE is simply showing 0x1 in the Scheduled Tasks window, when the lastexitcode is anything other than zero.

For example, I have the following ps script and batch file:

Test.ps1
iex c:\scripts\Test.bat
exit $LASTEXITCODE

Test.bat
exit /b 12345

If i run Test.ps1 as a scheduled task, the Last Result displayed is 0x1. Am I going about this the wrong way? How can I pass the error code to the task scheduler?

All replies (2)

Thursday, December 2, 2010 3:37 AM âś…Answered

Have a look here: http://dmitrysotnikov.wordpress.com/2008/05/29/monitor-web-site-availability/


Thursday, December 2, 2010 1:11 PM | 1 vote

Thanks for the reference, Marco! Using the command switch when scheduling the PS script did the trick.

**Before
**C:\WINDOWS\system32\WINDOW~2\v1.0\powershell.exe -NoProfile -NonInteractive  "c:\scripts\TestingEmailNotices.ps1"

**After
**C:\WINDOWS\system32\WINDOW~2\v1.0\powershell.exe -NoProfile -NonInteractive -command ". c:\scripts\TestingEmailNotices.ps1; exit $LASTEXITCODE"