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,
- the redirection must be included into the Base64-Encoded string
- if the script is quite long, you may hit the CMD/TaskScheduler CLI command-line string limitation
hope it helps.