kayasax
9/20/2013 - 7:36 AM

Import & export of scheduled tasks using scheduler service

Import & export of scheduled tasks using scheduler service

# create task from its xml definition
$sch = New-Object -ComObject("Schedule.Service")
$sch.connect("computername")

$root=$sch.GetFolder("\")
$root.CreateFolder("subfolder")
$folder =$sch.GetFolder("\subfolder") 
  	
#import .xml
Get-childItem -path $task_path -Filter *.xml | %{
	$task_name = $_.Name.Replace('.xml', '')
	$task_xml = Get-Content $_.FullName
	$task = $sch.NewTask($null)
	$task.XmlText = $task_xml
	$folder.RegisterTaskDefinition($task_name, $task, 6, $cred.UserName, $cred.GetNetworkCredential().password, 1, $null)
}

#Export
$folder.getTasks(0) | % {
    $path="c:\temp\tasks\$($_.name).xml"
    $x=New-Item -ItemType file -Path $path
    Set-Content -Path $path -Value $_.xml
}