Dunner1991
Dunner1991

Reputation: 39

Task Scheduler executes script but does not output to log file

I am running into the following issue :

I have a task scheduler job running across a number of Azure VMs, it is scheduled to run at ~11pm every wednesday.

From what I can see the script executes the required change, however it does not generate the log output file

enter image description here

the argument I use is the following:

-F C:\Users\Admin\Desktop\scripts\automatexml_weu.ps1 -Verbose > C:\Users\Admin\Desktop\scripts\xml_script_logs\xml_script_output_$(Get-Date -format "yyyyMMdd").log

But the log file is never generated.

If I run the above command outside of Task Scheduler it works with no issues

Anyone have any ideas where I am going wrong?

Thanks in advance

Upvotes: 3

Views: 3115

Answers (2)

Peyre
Peyre

Reputation: 573

A bit late but it may help others. Redirecting powershell output in task scheduler is a bit tricky.

If you pass the script using the -file parameter, the redirection is always handled by the task scheduler CLI which has absolutely no idea of what $(Get-Date -format "yyyyMMdd") means and tries (unsuccessfully) to interpret it as a filename. More, as $(Get-Date -format "yyyyMMdd") contains space chars, the CLI splits it into multiples parameters adding more mess to the command line. If you do want to use -file, you have to rebuild the date from %date%

On the other hand, if you replace -file by -command and quote > with ^ to hide it from the task scheduler CLI, the redirection is handled by powershell which understands $(Get-Date -format "yyyyMMdd"). Be aware the the task scheduler CLI interprets " in the same way that CMD does so it will remove them. In that very case, it's not a problem as the -f of Get-Date is waiting for a [String] argument but if for instance, you use $((get-date).tostring("yyyyMMdd")), you will get an error. So just replace " by ' (eg. $((get-date).tostring('yyyyMMdd')))

To summarize, your parameters should read

-command C:\Users\Admin\Desktop\scripts\automatexml_weu.ps1 -Verbose ^> C:\Users\Admin\Desktop\scripts\xml_script_logs\xml_script_output_$(Get-Date -format 'yyyyMMdd').log

One last point, if you only redirect stream 1, you'll only get what your script sent to the output stream and will never get the content of the error/ warning/ verbose/ streams. If you are interested in them (I suppose you are as you have -verbose in you command), juste replace ^> by ^*^> in the command parameters.

-command C:\Users\Admin\Desktop\scripts\automatexml_weu.ps1 -Verbose ^*^> C:\Users\Admin\Desktop\scripts\xml_script_logs\xml_script_output_$(Get-Date -format 'yyyyMMdd').log

Edit

how to use -EncodedCommand instead of -Command

say you have the following script to execute

$ErrorActionPreference = 'STOP'
Set-StrictMode -Version Latest

$params = @{
    ForegroundColor = 'Red'
    BackgroundColor = 'Yellow'
}

Write-Host 'Hello, World!' @params

and for any reason, you don't want or can not store it in a file (say D:\hello-world.ps1) and use

-command D:\hello-world.ps1 ^*^> D:\hello-world.log 

in the TaskScheduler, you can Base64-encode it,

[System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("&{$(Get-Content -Raw 'D:\hello-world.ps1')} *> D:\hello-world.log"))

and use the resulting Base64 string as an EncodedCommand in the TaskScheduler

-EncodedCommand JgB7ACQARQBy...8AZwA=

please note that,

  1. the redirection must be included into the Base64-Encoded string
  2. if the script is quite long, you may hit the CMD/TaskScheduler CLI command-line string limitation

hope it helps.

Upvotes: 1

Otter
Otter

Reputation: 1141

Edit: it appears there is a workaround for this, take a look @Peyre answer


Unfortunately it doesnt seem possible to do it this way through Task Scheduler.

"The scheduled task cannot redirect that way. You will have to redirect to a file inside of the PowerShell script. It is PowerShell that supports that redirection." - technet

Upvotes: 1

Related Questions