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