2014-01-28 77 views
0

我正在嘗試使用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的所以這些都不是選項

如果有人有任何見解,將不勝感激。

+0

什麼是你所厭惡的SCHTASK? –

+0

此外,請注意通過本文,如果它是任務中的啓動觸發器,則您傳遞的用戶必須位於服務器的管理員組中(遠程或本地)。你也可以運行標誌1而不是6來驗證語法而不創建任務,看看是否給出了任何指針。 http://msdn.microsoft.com/en-us/library/windows/desktop/aa382575(v=vs.85)。aspx –

+0

我試圖使用驗證標誌(1),並添加一個$ null來滿足7個參數,如下一篇文章中所述,並且如之前的評論中所述,並且它產生了相同的錯誤。我將GC的類型更改爲字符串而不是xml,並且這不會因驗證而失敗。然後,我將標誌更改爲6,現在我得到「異常調用」「RegisterTask」和「7」參數:「該參數是公司的直接參數。 (從HRESULT異常:0x80070057(E_INVALIDARG))「。問題是哪一個,我將繼續深入探討這一行動 – thxmike

回答

1

我能弄清楚這個問題。首先是RegisterTask中的第一個參數。我正在將其解釋爲Task調度程序中的文件夾。但是,這不是文件夾,而是任務名稱。其次,在一些註釋和驗證標誌的幫助下,我發現第二個參數需要是字符串類型而不是xml類型。最後,我不得不添加的零一七參數fullfill方法簽名:感謝您的幫助

這裏是工作的更新的代碼:

param(
    [string]$taskName = $(throw "-taskName is required"), #complete path for the scheduled task 
    [string]$xmlFilePath = $(throw "-xmlFilePath is required"), 
    [string]$server = "localhost", # Only works with Servers it can access. Use WinRM for cross domain request 
    [string]$taskFolderName = "\" 
) 
$value = $null; 
try { 
    $xmlContent = [string](Get-Content $xmlFilePath);  
    $taskScheduler = New-Object -ComObject Schedule.Service 
    $taskScheduler.Connect($server) 
    $taskFolder = $taskScheduler.GetFolder($taskFolderName); 
    $value = $taskFolder.RegisterTask($taskName, $xmlContent, 6, "<username>", "<password>", 1, $null); 
} 
catch { 

    $Exception = $_.Exception; 
    while ($Exception.Message -ne $null) 
     { 
      Write-Error $Exception.Message; 
      $Exception = $Exception.InnerException; 
     } 
    return; 
} 
1

如果你只需要輸入:

$taskScheduler = New-Object -ComObject Schedule.Service 
$taskScheduler.Connect("localhost") 
$taskFolder = $taskScheduler.GetFolder("\") 
$taskFolder.RegisterTask 

您會收到:

IRegisteredTask RegisterTask (string, string, int, Variant, Variant, _TASK_LOGON_TYPE, Variant) 

有7個參數,這MEAS,你錯過一個參數。如果您在Microsoft documentation看看調用看起來是這樣的:

HRESULT RegisterTask(
    [in]   BSTR path, 
    [in]   BSTR xmlText, 
    [in]   LONG flags, 
    [in]   VARIANT userId, 
    [in]   VARIANT password, 
    [in]   TASK_LOGON_TYPE logonType, 
    [in, optional] VARIANT sddl, 
    [out]   IRegisteredTask **ppTask 
); 

所以我會嘗試添加$ null作爲最後一個參數(安全描述符):

$taskFolder.RegisterTask($xmlFilePathl, $xmlContent, 6, "<user name>", "<password>", 1, $null) 
+0

我試圖在第七個參數中使用7個參數以及$ null,但是語句仍然失敗同樣的錯誤 – thxmike

+1

閱讀你的答案,這似乎是有幫助的。 – JPBlanc