我正在尝试从 PowerShell 脚本将任务添加到任务计划程序,该脚本将运行带有参数的 PowerShell 脚本。
文件路径中的空格与整个命令周围的必要引号相冲突,并且 SCHTASKS 将 ' 转换为 ",因此我无法正确封装。
$command = "PowerShell \`"& 'C:\ProgramFiles (x86)\MyDir\MyScript.ps1' $myStringParam $myBooleanParam\'""
Write-Host $command # This outputs: PowerShell \"& 'C:\Program Files (x86)\MyDir\MyScript.ps1' Cat 0\"
SCHTASKS /Create /TN "MyTask" /TR "$command" /SC DAILY /ST 01:30:00 /RL Highest /EC ScriptEvents /RU SYSTEM
但任务计划程序将操作显示为:
PowerShell "& "C:\Program Files (x86)\MyDir\MyScript.ps1" Cat 0"
" 和 " 相互抵消,因为这里 ' 总是切换到 ",因此任务失败。
通过使用 \" 作为内部引号解决了这个问题。必须在 PowerShell 脚本中将 ' 与 \\\`" 交换
$command = "PowerShell \`"& \\\`"C:\ProgramFiles (x86)\MyDir\MyScript.ps1\\\`" $myStringParam $myBooleanParam\'""
所以任务计划程序显示
PowerShell "& \"C:\Program Files (x86)\MyDir\MyScript.ps1\" Cat 0"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)