2011-06-17 46 views
2

我試圖用powershell編寫Windows計劃的任務創建腳本,其中計劃任務調用包含空格的目錄中的powershell腳本。所以,我需要像powershell.exe -nonInteractive -command 「& 'C:\ TEMP \ Program Files文件\ a.ps1'」 A/TR參數創建Windows使用PowerShell計劃任務創建問題

這裏是什麼,我已經嘗試了樣品

# Create the script file the schedule task will call, note path contains a space 
'set-content c:\temp\program files\a.log "Just done @ $(get-date)"' > 'c:\temp\program files\a.ps1' 

$scriptFilePath = 'c:\temp\program files\a.ps1'; 
$userName = read-host 'user name'; # will need log on as batch rights 
$userPassword = read-host 'user password'; 

# The following looks good but schtasks args gets messed up so no creation 
$taskToRun = "c:\windows\system32\windowspowershell\v1.0\powershell.exe -noninteractive -command `"& '$scriptFilePath'`""; 
$taskToRun 
schtasks /create /tn 'ATest' /ru $userName /rp $userPassword /sc MINUTE /mo 1 /st '00:00' /f /tr $taskToRun; 

# Gets imported but mangles the tr so instead of 
$taskToRun = 'c:\windows\system32\windowspowershell\v1.0\powershell.exe -noninteractive -command \"& ''' + $scriptFilePath + '''\"'; 
$taskToRun 
schtasks /create /tn 'ATest' /ru $userName /rp $userPassword /sc MINUTE /mo 1 /st '00:00' /f /tr $taskToRun; 

如果有人知道如何正確逃生,我將不勝感激暗示提前

感謝

帕特

+0

Powershell不需要分號行終止符。只有當你希望在一行上有多個語句時才使用';'。 – vonPryz

+0

我知道他們不需要,只是一個習慣 – pmcgrath

+0

任何最終的解決方案,完整的源代碼? – Kiquenet

回答

1

我換-command選項 - 文件:

$taskToRun = "c:\windows\system32\windowspowershell\v1.0\powershell.exe -noninteractive -file ""$scriptFilePath""" 

此外,PowershellPack擁有的TaskScheduler模塊,這使任務調度更加容易:

http://archive.msdn.microsoft.com/PowerShellPack

[更新]感謝

完美的,只是需要逃生用單引號而非雙引號

$taskToRun = "c:\windows\system32\windowspowershell\v1.0\powershell.exe -noninteractive -file '$scriptFilePath'";