none
Import schedule task in windows server 2016 RRS feed

  • Question

  • I have around 200 .xml task which I copied from old windows 2008 server that I need to import with same name and it also use service account and password.  I tried below script and its not working in 2016. do yo have any working script for windows 2016?

    $task_path = "D:\Schedule task\*.xml"
    $task_user = "domain\serviceaccount"
    $task_pass = "password"

    $sch = New-Object -ComObject("Schedule.Service")
    $sch.connect("localhost")
    $folder = $sch.GetFolder("\")

    Get-Item $task_path | %{
     $task_name = $_.Name.Replace('.xml', '')
     $task_xml = Get-Content $_.FullName

     $task = $sch.NewTask($null)
     
     $task.XmlText = $task_xml

     $folder.RegisterTaskDefinition($task_name, $task, 6, $task_user, $task_pass, 1, $null)
     
    }


    • Edited by TechBigB Friday, November 15, 2019 9:05 PM
    Friday, November 15, 2019 9:04 PM

Answers

  • $task_path = "c:\temp\test\*.xml"
    $task_user = "user"
    $task_pass = "pass"
    Get-Item $task_path | %{
      $task_xml = Get-Content $_.FullName -Raw
      Register-ScheduledTask -TaskName $_.BaseName -Xml $task_xml -Password $task_pass -User $task_user -Force
    }

    Saturday, November 16, 2019 12:46 AM

All replies

  • To import XML task files use schtasks.

    SCHTASKS /?


    \_(ツ)_/

    Friday, November 15, 2019 9:15 PM
    Moderator
  • $task_path = "c:\temp\test\*.xml"
    $task_user = "user"
    $task_pass = "pass"
    Get-Item $task_path | %{
      $task_xml = Get-Content $_.FullName -Raw
      Register-ScheduledTask -TaskName $_.BaseName -Xml $task_xml -Password $task_pass -User $task_user -Force
    }

    Saturday, November 16, 2019 12:46 AM
  • Register-ScheduedTask does not have an XML parameter in 5.1 or earlier on W2012 or earlier.  It does exist on W2016 and later,


    \_(ツ)_/


    • Edited by jrvModerator Saturday, November 16, 2019 2:18 AM
    Saturday, November 16, 2019 2:17 AM
    Moderator
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, November 18, 2019 5:57 AM
    Moderator
  • Thanks for the help! This worked.
    Monday, November 18, 2019 9:11 PM