2011-06-17 14 views
2

スケジュールタスクはpowershellでスケジュールされたタスクの作成をスクリプトしようとしています。スケジュールタスクはスペースを含むディレクトリにあるpowershellスクリプトを呼び出します。だから私はたpowershell.exe -noninteractive -command「& 『はC:\ tempに\プログラムファイル\ a.ps1』」のように/ trの引数で作成する必要があります。ここWindowsはpowershellでタスク作成の問題をスケジュールします

することは、私が試したもののサンプルです

私は-commandを交換したい
# 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

-fileのオプション:

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

はまた、PowershellPackだTaskSchedulerモジュールは、タスクのスケジューリングがはるかに容易になりますがあります。

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

ちょうど脱出するために必要な[UPDATE]おかげ

パーフェクト、二重引用符ではなく一重引用符付き

$taskToRun = "c:\windows\system32\windowspowershell\v1.0\powershell.exe -noninteractive -file '$scriptFilePath'"; 
関連する問題