Ever see the Geico commercial where the guy says it took three months to teach a guinea pig to say “Row”? “Such a simple word”!
Well, I’ve hunted around, tried all kinds of methods, and spent hours on what should have been a simple task! Finally, I think I’ve found the only reliable method for capturing PowerShell output when it is run as a Windows Scheduled Task.
The long and short of it is: Use a CMD or BAT program to call the PowerShell script. You may not have to use a CMD program but in some cases you will. A lot of it depends on the parameters being passed and if those parameters contain a “List” item or whether there are single quotes or double quotes and who knows what else. I tried many variations of supplying PowerShell parms in task scheduler. Some worked in some cases and not in others. The only consistent method which worked in every case was to use a CMD program.
With this method you can not only capture all the PowerShell output (stderr, error, verbose, etc.) to a file using redirection but you can also capture any return code and thereby have the Windows Task “know” the script succeeded or failed.
There are no changes necessary to your PowerShell script unless you want to capture an Exit code. What you do need to do is create a CMD program which calls your Posh using a format such as:
::Execute a Powershell script from a CMD
If (%1)==() GOTO Missing
Powershell.exe -NonInteractive -NoLogo -NoProfile -Command ""D:\Scripts\MyPosh.ps1"" -Server %1 -Warehouse ""%2"" 2>&1> ""D:\logs\PoshLog_%1.txt"";Return $LASTEXITCODE"
Echo "Parms are missing"
The key here is the –Command syntax. I have found this does not work properly when using –File. You must use –Command and enter the entire command (including script parms, redirection and the Return $LASTEXITCODE) within double quotes. You must also double double-quote any parms containing spaces. My example Command can be broken down as:
- The complete path and name of the PowerShell script to run
- Three parms I am supplying to the script as indicated by %1-%3. These parms are entered in the Windows Scheduled Task which calls this cmd script.
- The redirection of stderr to stdout and all redirected to a file using 2>&1> “C:\MyPath\log.txt”
- Command separator “;” the semicolon
- Capture the $LASTEXITCODE for the previous command (the Posh script) and throw it out to Windows Scheduler
The Task Scheduler would look something like this The arguments portions is like this:
LCFSQLT04 "W:\SQL Backups" "'DOTProperty','Property'"
It has three arguments of which the last is a “list”
Emitting an Error
In order to capture an error from your Powershell script, you have to emit that error by using an EXIT statement. A RETURN statement will not work.