我是PowerShell脚本的新手,我在Windows Server 2003上遇到了schtasks。 我知道问题在于逃避但无法解决问题。
[string]$a = "-command `"Powershell $b > $c`""
$d = "C:\powershell.exe";
其中$ b是powershell脚本的路径(C:\ filename.ps1) $ c是日志文件路径(C:\ log \ filename.log)
当我尝试使用schtaks安排任务时
schtasks /create /sc weekly /d $tsk.DofW /st $tsk.At /tn "$tsk.name" /tr "$d $($a)"
我收到错误说错误的参数/选项 - 'C:\ filename.ps1'
任何帮助表示赞赏
修改
如果我这样做,就像你提到的那样运行没有任何错误,但当我看到 任务调度程序操作选项卡,它在详细信息C:\ powershell.exe System.Collections.Hashtable.args下说明 而不是powershell.exe - 命令“Powershell C:\ filename.ps1> C:\ log \ filename.log”。 如果我在schtasks中添加一个额外的括号,例如“$ d $($ a)”,我会收到一条错误,说明无效的参数/选项“>”
答案 0 :(得分:1)
试试这个(未经测试)。评论如下:
$b = "C:\filename.ps1"
$c = "C:\log\filename.log"
# Removed [string] - When value is quoted, it's a string, you don't need to cast it.
# Removed "PowerShell" in a PS console(which you open with your $d,
# you only need to specify the scriptpath to the file. You don't need "powershell" at the start.
$a = "-command $b > $c"
# Powershell executable is in the following location
$d = "c:\windows\System32\WindowsPowerShell\v1.0\powershell.exe"
schtasks /create /sc weekly /d $tsk.DofW /st $tsk.At /tn "$tsk.name" /tr "$d $a"
编辑问题在于,当您在$a
中转义引号时,其中一个引号在运行命令时结束了参数字符串。因此,命令的其余部分会考虑其他参数,powershell
无法找到要链接的参数。
当您使用字符串指定-command
参数时,需要将其添加到命令的末尾,因为-command
会将其后面的所有内容视为其值。由于-command
已经在您的taskrun操作结束时,您只需删除$a
中的转义引号。