PowerShell计划任务寄存器方法



我正在尝试使用powershell v2.0使用Schedule.service com对象创建计划的任务。我已经创建了PS1文件,但是当我们执行避开我的情况下,我们会遇到错误。这是代码:

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; 
}

runnig在本地或远程给出相同的结果。错误如下:C: temp createScheduledTaskFromxml.ps1:异常调用" registertask",用" 6"参数(s):"(1,2)::"在线:1 char:33 。 createScheduledTaskfromxml.ps1&lt;&lt;&lt;&lt;&lt;-Server devbdapp12 -xmlFilePath" C: temp collectors adcomputer adcomputer discovery.ser vices.activerectory.computer.collector.xml" categoryInfo:未指定::) [write-error],writeerrorexception flutlqualifyErrid:microsoft.powershell.commands.writeerrorexception,createsCheduledTaskFromxml.ps1

这是什么意思是"异常调用" registertask"," 6"参数:"(1,2)::""故障发生在寄存器任务方法上,但错误没有意义。此用途基于以下MSDN文章http://msdn.microsoft.com/en-us/library/windows/desktop/aa382575(v = vs.85).aspx

作为一方,我们无法将此机器更新为PowerShell 3.0或目前使用PowerPack,并且希望避免使用schtask.exe,因此这些不是选项

如果有人有任何见解,将不胜感激。

如果您只输入:

$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个参数,这是您错过一个论点的措施。如果您查看Microsoft文档,则呼叫看起来像这样:

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)

我能够找出这个问题。首先是寄存器任务中的第一个参数。我将其解释为任务调度程序中的文件夹。但是,这不是文件夹,而是任务名称。其次,在某些注释和验证标志的帮助下,我发现第二个参数需要是字符串类型,而不是XML类型。最后,我不得不添加第七个关于null的论点,以完整填写方法签名:感谢您的所有帮助

这是有效的更新代码:

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; 
}

最新更新