繁体   English   中英

PowerShell计划任务RegisterTask方法

[英]PowerShell Scheduled Task RegisterTask Method

我试图使用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; 
}

在本地或远程运行此操作会产生相同的结果。 错误如下:C:\\ temp \\ CreateScheduledTaskFromXML.ps1:使用“6”参数调用“RegisterTask”的异常:“(1,2)::”在行:1 char:33 +。\\ CreateScheduledTaskFromXML。 ps1 <<<< -server DEVBDAPP12 -xmlFilePath“C:\\ Temp \\ collectors \\ adcomputer \\ Discovery.Ser vices.ActiveDirectory.Computer.Collector.xml”+ CategoryInfo:NotSpecified :( :) [Write-Error],WriteErrorException + FullyQualifiedErrorId :Microsoft.PowerShell.Commands.WriteErrorException,CreateScheduledTaskFromXML.ps1

这是什么意思“异常调用”RegisterTask“with”6“参数:”(1,2)::“”registertask方法出现故障,但错误没有意义。 此用法基于以下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)

我能够弄清楚这个问题。 首先是RegisterTask中的第一个参数。 我将其解释为Task调度程序中的文件夹。 但是,这不是文件夹而是任务名称。 其次,在一些注释和验证标志的帮助下,我发现第二个参数需要是字符串类型而不是xml类型。 最后,我必须添加一个null的第7个参数来填充方法签名:感谢您的所有帮助

以下是有效的更新代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM