I am very new to Powershell scripting but have attempted to modify a script that I found here to import some XML Scheduled Tasks using Powershell in Windows 2012 R2.
I have been successful in importing the Scheduled Tasks into the root [Task Scheduler Library] using this script.
The problem seems to be that the Schedule Tasks need to be imported into a subfolder under Task Scheduler Library, let's say "SubTasks"
$task_path = "C:\Users\me\Desktop\ST Testing\exported ST's\scheduledTask.xml"
$task_user = "usr"
$task_pass = "pwd"
$schedule = new-object -com("Schedule.Service")
$schedule.Connect("server") # servername
#$folder = $schedule.GetFolder("\") <===This works fine
$folder = $schedule.GetFolder("\SubTasks") #<===This does not work
Write-Host $folder
Get-Item $task_path | % {
$task_name = $_.Name.Replace('.xml', '')
$task_xml = Get-Content $_.FullName
$task = $schedule.NewTask($null)
$task.XmlText = $task_xml
$folder.RegisterTaskDefinition($task_name, $task, 6, $task_user, $task_pass, 1, $null)
}
When I run the above Powershell Script I receive this error:
Exception calling "RegisterTaskDefinition" with "7" argument(s): "The specified path is invalid. (Exception from HRESULT: 0x800700A1)" At C:\Users\me\Desktop\ST Testing\ImportSTs.ps1:22 char:5 + $folder.RegisterTaskDefinition($task_name, $task, 6, $task_user, $task_pass, ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation
Thanks in advance.
Register-ScheduledTask
supports using XML to define the task:
Register-ScheduledTask `
-User $task_user `
-Password $task_pass `
-TaskName ([IO.Path]::GetFileNameWithoutExtension($task_path)) `
-TaskPath '\SubTasks' `
-Xml (Get-Content $task_path -Raw)
Note that you need to fetch the actual XML content. The -Raw
flag prevents Get-Content
from returning a String[]
, which also makes Register-ScheduledTask
unhappy.
Also note that Register-ScheduledTask
has a -TaskPath
argument, allowing you specify a subfolder to put the task in if desired.
I'm not fond of using the grave mark for line continuation, so I prefer splatting to spread things out over multiple lines:
$taskArgs = @{
User='usr';
Password='pwd';
TaskName=([IO.Path]::GetFileNameWithoutExtension($task_path));
TaskPath='\SubTasks';
Xml=(Get-Content $task_path -Raw)
}
Register-ScheduledTask @taskArgs
if you're on powershell 3 (win2k12 - so you are) and above, there is a whole module for task scheduling.
see: gcm -Module PSScheduledJob
however, it does not seem to come with an easy way to import tasks from xml.
there are modules for that, this particular module deserializes the xml file and tests for all settings, seems more cumbersome than schtasks solution listed below.
for powershell 2 (win2k8) I found it easier to use schtasks
- full ref here:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx
for example (scheduling a task from an xmlfile, the task will run under a particular user creds:
schtasks /Create /XML $xmlfile /RU $creds.UserName /RP $creds.GetNetworkCredential().Password /TN "My Task Name"
I got my xml file by manually creating the task and exporting to xml (I removed credential and info nodes from xml)
Was able to get this to work for our needs.
$task_user="username"
$task_pass="password"
$schedule=new-object -ComObject ("Schedule.Service")
$schedule.Connect("server")
$folder=$schedule.GetFolder("\tasks")
$path="\\server\c$\temp\Tasks\"
Get-ChildItem $path -Filter "*.xml"| foreach {
$task_conf=Get-Item -Path $_.FullName
$taskname=$task_conf.Name
$task_xml=$task_conf.FullName
$task=$schedule.NewTask(0)
$task.XmlText=(Get-Content $task_xml).Replace('Task version="1.1" xmlns','Task version="1.2" xmlns')
$folder.RegisterTaskDefinition($taskname,$task,6,$task_user,$task_pass,1,$null)
}
User contributions licensed under CC BY-SA 3.0