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;
如果有人知道如何正確逃生,我將不勝感激暗示提前
感謝
帕特
Powershell不需要分號行終止符。只有當你希望在一行上有多個語句時才使用';'。 – vonPryz
我知道他們不需要,只是一個習慣 – pmcgrath
任何最終的解決方案,完整的源代碼? – Kiquenet