Question

I am attempting to use powershell v2.0 to create a scheduled task using the Schedule.Service Com Object. I have created ps1 file but we get an error when we execute it that is eluding me. Here is the code:

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 this locally or remotely gives the same result. The error is as follows: C:\temp\CreateScheduledTaskFromXML.ps1 : Exception calling "RegisterTask" with "6" argument(s): "(1,2)::" At line: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

What does this mean "Exception calling "RegisterTask" with "6" argument(s): "(1,2)::"" The failure is occuring on the registertask method but the error does not make sense. This use is based on the following MSDN article http://msdn.microsoft.com/en-us/library/windows/desktop/aa382575(v=vs.85).aspx

As a side not, We cannot update this machine to powershell 3.0 or use the powerpack at this time and would like to avoid schtask.exe so these are not options

If anyone has any insight it would be greatly appreciated.

Was it helpful?

Solution 2

I was able to figure out this problem. First was the first argument in the RegisterTask. I was interpreting this as the folder in the Task scheduler. However, this is not the folder but the task name. Second, with the help from some of the comments and the validation flag, I found that the second argument needs to be a string type and not xml type. Finally, I had to add a 7th argument of null to fullfill the method signature: Thanks for all your help

Here is the updated code that works:

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

OTHER TIPS

If you just type :

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

You receive :

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

There are 7 arguments, this meas that you miss one argument. If you have a look at Microsoft documentation the call looks like this :

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

So I would try to add $null as the last argument (security descriptor) :

$taskFolder.RegisterTask($xmlFilePathl, $xmlContent, 6, "<user name>", "<password>", 1, $null)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top