我正在嘗試使用Schedule.Service Com對象使用powershell v2.0創建計劃任務。 我已經創建了ps1文件,但是當我們執行它時,我們會遇到一個錯誤。 下面是代碼:PowerShell計劃任務RegisterTask方法
param(
[string]$xmlFilePath = $(throw "-xmlFilePath is required"),
[string]$server = "localhost",
[string]$taskFolderName = "\"
)
try {
$xmlContent = [xml] (Get-Content $xmlFilePath);
$taskScheduler = New-Object -ComObject Schedule.Service
$taskScheduler.Connect($server)
$taskFolder = $taskScheduler.GetFolder($taskFolderName);
$taskFolder.RegisterTask($xmlFilePathl, $xmlContent, 6, "<user name>", "<password>", 1);
}
catch {
$Exception = $_.Exception;
while ($Exception.Message -ne $null)
{
Write-Error $Exception.Message;
$Exception = $Exception.InnerException;
}
return;
}
運行過程中出現該本地或遠程給出相同的結果。 錯誤如下: C:\ temp \ CreateScheduledTaskFromXML.ps1:調用具有「6」參數的「RegisterTask」的異常:「(1,2)::」 在行:1 char:33 + \ CreateScheduledTaskFromXML.ps1 < < < < -server DEVBDAPP12 -xmlFilePath 「C:\ TEMP \收藏家\ adcomputer \ Discovery.Ser vices.ActiveDirectory.Computer.Collector.xml」 + CategoryInfo:NotSpecified:(:) [直寫錯誤],WriteErrorException + FullyQualifiedErrorId:Microsoft.PowerShell.Commands.WriteErrorException,CreateScheduledTaskFromXML.ps1
這是什麼意思「Exception calling」RegisterTask「with 「6」參數:「(1,2)::」「 失敗發生在registertask方法上,但錯誤沒有意義。 這種使用是基於以下MSDN文章 http://msdn.microsoft.com/en-us/library/windows/desktop/aa382575(v=vs.85).aspx
作爲一個側面上沒有,我們無法更新這臺機器的PowerShell 3.0或使用動力單元在這個時候,想避免Schtask.exe的所以這些都不是選項
如果有人有任何見解,將不勝感激。
什麼是你所厭惡的SCHTASK? –
此外,請注意通過本文,如果它是任務中的啓動觸發器,則您傳遞的用戶必須位於服務器的管理員組中(遠程或本地)。你也可以運行標誌1而不是6來驗證語法而不創建任務,看看是否給出了任何指針。 http://msdn.microsoft.com/en-us/library/windows/desktop/aa382575(v=vs.85)。aspx –
我試圖使用驗證標誌(1),並添加一個$ null來滿足7個參數,如下一篇文章中所述,並且如之前的評論中所述,並且它產生了相同的錯誤。我將GC的類型更改爲字符串而不是xml,並且這不會因驗證而失敗。然後,我將標誌更改爲6,現在我得到「異常調用」「RegisterTask」和「7」參數:「該參數是公司的直接參數。 (從HRESULT異常:0x80070057(E_INVALIDARG))「。問題是哪一個,我將繼續深入探討這一行動 – thxmike