Ok, I have the following code which retrieves a file from my main machine makes an adjustment and then creates a Scheduled Task on my test machine. The program is run from the test machine. This works just as it should.
$SRCXMLPath = "\\Dellxps8920\BEKDocs-DellXPS8920\TaskSchedulerExports\Tasks"
$DestXMLPath = "G:\BEKDocs\TaskSchedulerExports\Standard"
$XMLFile = "Backup Report.xml"
$GCArgs = @{Path =
$(Join-Path -Path $SRCXMLPath -ChildPath $XMLFile)}
[xml]$xml = Get-Content @GCArgs
$xml.Task.RegistrationInfo.Author #Has no effect on running!
$xml.Task.Principals.Principal.UserId
<# ----------------------------------------------------
| Note: You can use either the text description of |
| the account or the Registry designation. |
----------------------------------------------------
#>
$xml.Task.Principals.Principal.UserId =
'DellXPS8700\Bruce'
# "S-1-5-21-1944453205-1635450679-2182824496-1001"
$xml.save()
& schtasks.exe /create /tn "Backup Report" /xml "$(Join-Path -Path $DestXMLPath -ChildPath $XMLFile)"
However, I'm trying to convert it to Full PowerShell via Register-ScheduledTask cmdlet to eliminate the call to schtasks.exe. Using the following test code I consistently get the displayed error.
[xml]$Task = Get-Content "G:\BEKDocs\TaskSchedulerExports\Standard\Backup Report.xml"
$RTArgs = @{TaskName = "Backup Report"
TaskPath = "\"
XML = $Task
Force = $True}
Register-ScheduledTask @RTArgs
Error Message:
Register-ScheduledTask @RTArgs
Register-ScheduledTask : The task XML is malformed.
(1,2)::ERROR: incorrect document syntax
At line:7 char:1
Register-ScheduledTask @RTArgs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (PS_ScheduledTask:Root/Microsoft/.
..S_ScheduledTask) [Register-ScheduledTask], CimException
FullyQualifiedErrorId : HRESULT 0x8004131a,Register-ScheduledTask
Anyone have any Ideas?
Please Note: the .xml file works perfectly with the code at the top of the post or if it is imported manually inside TaskSheceuler.
CodePudding user response:
You're reading the XML file as an XML object, but the cmdlet expects a string object.
Register-ScheduledTask
[-Force]
[[-Password] <String>]
[[-User] <String>]
[-TaskName] <String>
[[-TaskPath] <String>]
[-Xml] <String>
[-CimSession <CimSession[]>]
[-ThrottleLimit <Int32>]
[-AsJob]
[<CommonParameters>]
Change the line to read the file in as just one string and you should be all set.
$Task = Get-Content "G:\BEKDocs\TaskSchedulerExports\Standard\Backup Report.xml" -raw