0

I am trying to register a task in windows task scheduler by reading a XML file as string and able to do it successfully, additionally what i want to do is, to replace the {plname} (which is present in the XML) with the task name.

<Exec>
  <Command>notepad.exe</Command>
   <Arguments>-Command "&amp;{ &amp; "./pl.ps1 {plname} run" }"</Arguments>  
<WorkingDirectory>C:\Windows\System32\Tasks\Power</WorkingDirectory>
</Exec>

When i run my PS script (excluding the Register-ScheduledTask cmdlet) I can see the {plname} being replaced. font is small but you can see {plname} is replaced by openNotepad

Now when I run the "Register-ScheduledTask" cmdlet it registers the task successfully but in the GUI of Task Scheduler it is not showing the replacement done just before. {plname} was replaced earlier with openNotepad, but has reverted back to its previous state.

Hope i have made myself clear. Here is my script, Just in case.

    $taskName = "openNotepad"
    $taskTemplate = Get-Content (join-path "C:\Users\ACER\Desktop\POWERSHELL" "task-scheduler-template.xml" ) | Out-String
    $taskTemplate -replace "{plname}", $taskName

    Register-ScheduledTask -Xml $taskTemplate -TaskPath "\Power\" -TaskName $taskName
Ricky
  • 35
  • 1
  • 1
  • 7

1 Answers1

1

You forgot to update the $taskTempate variable with the replaced value.

$taskTemplate = $taskTemplate -replace "{plname}", $taskName
Kirill Pashkov
  • 3,118
  • 1
  • 15
  • 20