Task Scheduler does not properly redirect sysout/syserr from a Powershell script to a log file?
(Cross posted from a Powershell forum. Is there a separate forum for Task Scheduler? All the other Task Scheduler questions seemed to be here...)
http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/32952fdf-b3b8-44f9-a3c4-62bd802b1f39
Why do I have to create a separate, one line batch file wrapper just to get sysout/syserr redirection to work from Task Scheduler? Why can't Task Scheduler just...not...suck?
I'm dumbfounded how one command works fine in a cmd window, but the exact same command fails in Task Scheduler? Is there anyone from M$ lurking here who knows the details of Task Scheduler - does it just launch a cmd process, then run the configured
command?
Anyway, based on other posts in these groups (clearly others have this issue as well), I've changed my test script to:
x.ps1:
$DebugPreference = 2
$VerbosePreference = 2
$WarningPreference = 2
hostname.exe
Write-Host "host"
Write-Output "output"
Write-Error "error"
Write-Verbose "verbose"
Write-Warning "warning"
Write-Debug "debug"
From a cmd window:
del .\x_command.log
powershell -command ".\x.ps1" > ".\x_command.log"
works as expected, here is the output:
MYMACHINENAME
host
output
E:\SAS\Config\Utilities\x.ps1 : error
At line:1 char:8
+ .\x.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,x.ps1
VERBOSE: verbose
WARNING: warning
DEBUG: debug
del .\x_file.log
powershell -file ".\x.ps1" > ".\x_file.log"
works as expected, here is the output:
MYMACHINENAME
host
output
E:\SAS\Config\Utilities\x.ps1 : error
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,x.ps1
VERBOSE: verbose
WARNING: warning
DEBUG: debug
As you can see, the outputs are slightly different between the -command and -file invocation parameters. Why...who knows? Poor quality control and testing in Remond?
Using Task Scheduler: I ran the task under my login userid credentials to replicate the environment of the cmd window.
(manually delete .\x_command.log)
powershell -command ".\x.ps1" > ".\x_command.log"
Does not work, but at least it created a file, here is the output:
MYMACHINENAME
output
<end of file>
(manually delete .\x_file.log)
powershell -file ".\x.ps1" > ".\x_file.log"
Does not work, does not create a file
I've spent hours just trying to get Task Scheduler to do simple redirection to a file!!!
Next on the agenda is seeing if something like cmd /c "powershell -file .\x.ps1 > .\x.log" will work, without having to create the separate batch file wrapper. But what a convoluted mess just to get Task Scheduler to work in a way that makes sense.
(I admit this is tangentially related to Powershell, I'll also cross post this to a Task Scheduler group if there is one...)
August 22nd, 2012 9:26pm
Please stick the power shell forums as this forum is strictly used for discussing the Windows Server related issues. I hope your problem will be resolved soon on the power shell forumhttp://www.arabitpro.com
Free Windows Admin Tool Kit Click here and download it now
August 29th, 2012 3:03am
Please stick the power shell forums as this forum is strictly used for discussing the Windows Server related issues. I hope your problem will be resolved soon on the power shell forum
http://www.arabitpro.com
It's a Task Scheduler issue, manifested when calling a Powershell script
September 7th, 2012 8:37pm