2013-04-02 36 views
0

我是新来的PowerShell脚本,我在一个Windows Server 2003上与schtasks坚持。 我知道问题是转义,但无法解决它。与powershell脚本中的schtasks问题

[string]$a = "-command `"Powershell $b > $c`"" 
$d = "C:\powershell.exe"; 

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

当我尝试使用schtaks

schtasks /create /sc weekly /d $tsk.DofW /st $tsk.At /tn "$tsk.name" /tr "$d $($a)" 

我得到一个错误说无效的参数/选项计划任务 - 'C:\ filename.ps1'

任何帮助表示赞赏

编辑

如果我这样做,你怎么提到它运行没有任何错误,但是当我看 任务调度操作选项卡下,它的细节Ç说:\ powershell.exe System.Collections.Hashtable。 args 而不是powershell.exe - 命令“Powershell C:\ filename.ps1> C:\ log \ filename.log”。 如果我添加一个额外的支架,如“$ d $($ a)”在schtasks中,我得到一个错误,说无效参数/选项“>”

+1

如果您要在CMD中输入命令,它会是什么样子? '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” ' –

回答

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中的转义引号。

+0

更新了问题 – javaMan

+0

更新了答案。 –