2013-04-02 35 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

更新了答案。 –