0

I have this code:

param(
[string]$target_server_name=$ENV:ComputerName,
[string]$task_path,
#[string]$task_user,
#[string]$task_pass,
[string]$drop_folder,
[string]$dest_folder
)

    $creds = Get-Credential
    $task_user = $creds.UserName
    $task_pass = $creds.GetNetworkCredential().Password

    robocopy $drop_folder $dest_folder /MIR
    $service = new-object -ComObject ("Schedule.Service")
    $service.Connect($target_server_name)
    $rootFolder = $service.GetFolder("\")
    Get-Item $task_path | % {
       $task_name = $_.Name.Replace('.xml', '')
       $task_xml = Get-Content $_.FullName
       $task = $service.NewTask(0)
       $task.XmlText = $task_xml
       $folder.RegisterTaskDefinition($task_name, $task, 6, $task_user, $task_pass, 1, $null)
    }

And I am trying to import some task that I exported from the Windows Task Scheduler. But I am having this error: enter image description here

Can someone please tell me what I am doing wrong, Thanks.

Yanet Francisco
  • 147
  • 4
  • 19
  • You can use the debugger in the ISE to step through your code one line at a time. Also, when the debugger is active, you can float your mouse pointer over a variable to see what it contains. This should make it easier to determine where things are going wrong. – Bill_Stewart Nov 01 '17 at 18:34

2 Answers2

1

Try replace your $task.XmlText = $task_xml with $task.XmlText = $task_xml -join "`n".

Andrei Odegov
  • 2,925
  • 2
  • 15
  • 21
0

I also found this way and I think it looks better,

$taskDefinition.XmlText = $taskxml.OuterXml

Take a look on this: enter link description here

Yanet Francisco
  • 147
  • 4
  • 19