:Powershell Register-ClusteredScheduledTask from XML

1

I'd like to take my tasks on a non-clustered server, and import them as clustered tasks on a new cluster that has been built and tested.

Here's what I have tried:

cls
$xmlFilename="i:\Scripts\ImportScheduledTasks\CleaupMessageLog.xml" 
$xmlContents = get-content $xmlFilename
Register-ClusteredScheduledTask -TaskName "TestXMLTask" -TaskType AnyNode -Xml $xmlContents

The XML imports okay into the Windows Task Scheduler, but then it is not a "Clustered" task.

Here is the error I get when I run the above code:

Register-ClusteredScheduledTask : Cannot process argument transformation on parameter 'Xml'. Cannot 
convert value to type System.String.
At C:\Users\nwalters\Documents\TestWebSampleCode4FromXML.ps1:4 char:80
+ Register-ClusteredScheduledTask -TaskName "TestXMLTask" -TaskType AnyNode -Xml $ ...
+                                                                                ~
    + CategoryInfo          : InvalidData: (:) [Register-ClusteredScheduledTask], ParameterBindingArgumen 
   tTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Register-ClusteredScheduledTask

Thinking that it wants an XML Doc, I also tried this:

cls
$xmlFilename="i:\Scripts\ImportScheduledTasks\CleaupMessageLogUTF8.xml" 
$xmlContents = get-content $xmlFilename
$xmlDoc = [xml] (get-content $xmlFilename)
$test = $xmlDoc.OuterXml
#Write-Host $test
Register-ClusteredScheduledTask -TaskName "TestXMLTask" -TaskType AnyNode -Xml $xmlDoc

I open the file, changed UTF16 to UTF8 and resaved it, shut the different filename from the first example.

Error:

Register-ClusteredScheduledTask : The task XML is malformed.
(1,2)::ERROR: incorrect document syntax
At C:\Users\nwalters\Documents\TestWebSampleCode4FromXML.ps1:7 char:1
+ Register-ClusteredScheduledTask -TaskName "TestXMLTask" -TaskType AnyNode -Xml $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (PS_ClusteredScheduledTask:Root/Microsoft/...edScheduledTask) 
    [Register-ClusteredScheduledTask], CimException
    + FullyQualifiedErrorId : HRESULT 0x8004131a,Register-ClusteredScheduledTask

If the XML is really malformed, then how was I able to load it and get to the .OuterXml?

powershell
scheduled-task
asked on Server Fault Aug 25, 2014 by NealWalters • edited Aug 25, 2014 by NealWalters

1 Answer

3

The -Xml parameter expects the xml input as a string.

Get-Content, by default, returns an array of strings (one for each line), which is why the first error is thrown.

To get around this, use the -Raw parameter when calling Get-Content to have it return a single string:

$xmlContents = Get-Content $xmlFilename -Raw

Prior to PowerShell 3.0, you can achieve the same thing by piping the string array to Out-String:

$xmlContents = Get-Content $xmlFilename | Out-String
answered on Server Fault Aug 26, 2014 by Mathias R. Jessen

User contributions licensed under CC BY-SA 3.0