3

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

It works with "powershell -command" (the default), not "powershell -file"
1

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.

4 Comments

How to redirect if -Command is single-quoted? Because … -Command '…' ^*^> C:\Foo.log prints an PS error: ParserError: Unexpected token '^*^>' in expression or statement. - my command: pwsh.exe -ExecutionPolicy Bypass -NoProfile -Command '$ErrorActionPreference = ''SilentlyContinue''; $params = @{ Api = ''https://example.net/api/''; … left out for brevity…; Write-Output $?; Invoke-RestMethod -Authentication Bearer -Method POST -Token $params.Token -Uri ($params.Api + ''apiMethod') | Out-Null' ^*^> C:\Foo.log
@Yoda first, I did all my testing with powershell 5.1, so powershell.exe, not pwsh.exe. so I'm not sure if what I say apply to your case. in your command line, you use single-quoted string and cmd/ the task scheduler CLI have absolutly no idea of what a single quoted string is. did you try using a regular double-quoted string? if you still encounter quoting issues with double-quoted string, you can try -EncodedCommand instead of -Command. it often helps a lot.
Thanks for the tremendous help, but using double quotes, Task Scheduler seems to simply strip them off: ParserError: … someUrlPath/$($params.SomeId)/foo/bar`).data.value }; (Inv … while it should be /bar`").data.value. Neither using backticks, nor a \, nor another ", nor any combination of them in front of a " helped - it simply always removes any ". Can you help me out one more time, please?
@Yoda I edited my answer to include instructions on how to use -EncodedCommand instead of -Command. hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.