I am new to powershell scripting and i am stuck up with schtasks on a windows server 2003. I know the problem is with escaping but am not able to solve it.
[string]$a = "-command `"Powershell $b > $c`""
$d = "C:\powershell.exe";
where $b is a path to a powershell script(C:\filename.ps1) $c is the log file path (C:\log\filename.log)
When i try to schedule a task using schtaks
schtasks /create /sc weekly /d $tsk.DofW /st $tsk.At /tn "$tsk.name" /tr "$d $($a)"
i get an error saying Invalid argument/option - 'C:\filename.ps1'
Any help appreciated
EDIT
If i do it like how you mention it runs without any error but when i look at the task scheduler Actions tab it says under details C:\powershell.exe System.Collections.Hashtable.args instead of powershell.exe -command "Powershell C:\filename.ps1 > C:\log\filename.log". If i add an extra bracket like "$d $($a)" in schtasks i get an error saying Invalid argument/option ">"
schtasks /Create /sc WEEKLY /d MON /st 13:13 /tn FOO /tr 'C:\PowerShell.exe -command "& C:\filename.ps1 | Out-File C:\log\filename.log"'- E.V.I.L.