首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >powershell脚本中的schtasks问题

powershell脚本中的schtasks问题
EN

Stack Overflow用户
提问于 2013-04-02 18:37:58
回答 1查看 4.1K关注 0票数 1

我对powershell脚本很陌生,而且我在windows服务器2003上忙于执行一些任务。我知道问题是逃跑,但无法解决。

代码语言:javascript
复制
[string]$a = "-command `"Powershell $b > $c`""
$d = "C:\powershell.exe";

其中$b是powershell脚本(C:\filename.ps1)的路径,$c是日志文件路径(C:\log\filename.log)

当我尝试用schtaks来安排任务时

代码语言:javascript
复制
schtasks /create /sc weekly /d $tsk.DofW /st $tsk.At /tn "$tsk.name" /tr "$d $($a)"

我收到一个错误,说无效的参数/选项- 'C:\filename.ps1‘

感谢你的任何帮助

编辑

如果我像您提到的那样运行它,没有任何错误,但是当我查看任务调度程序操作选项卡时,它在details C:\powershell.exe System.Collections.Hashtable.args而不是powershell.exe -command "Powershell C:\filename.ps1 > C:\log\filename.log“下面写着。如果我在schtasks中添加了像"$d $($a)“这样的额外括号,就会出现一个错误,说明无效的参数/选项">”。

EN

回答 1

Stack Overflow用户

发布于 2013-04-02 18:57:25

试试这个(未经测试)。评论意见如下:

代码语言:javascript
复制
$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已经在您的任务运行操作的末尾,您所需要的只是删除$a中的转义引号。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15771767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档