Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aev-mambro2/6a350d5944bf904fe943ca02cd8712d5 to your computer and use it in GitHub Desktop.
Save aev-mambro2/6a350d5944bf904fe943ca02cd8712d5 to your computer and use it in GitHub Desktop.
import-scheduled-tasks-from-local-folder-structure-to-remote-server.ps1
$targetServer = "netbui-server-name";
$inputFolder = "C:\Users\Me\Documents\scheduled-task-exports\the-date\$($targetServer)";
$tasksNamespace = "\my\Scheduled\Tasks\Path";
$taskRunnerUserName = "WHO?";
$taskRunnerPassword = "how?";
$c = New-CimSession -ComputerName $targetServer;
Get-ChildItem -Recurse -Path (Join-Path $inputFolder $tasksNamespace) -Filter "*.xml" | foreach {
$newTaskName = $_.BaseName;
$newTaskPath = $_.DirectoryName.Replace($inputFolder,"");
Write-Output "File: '$($newTaskName)' in '$($newTaskPath)' (from '$($_.FullName)')";
Register-ScheduledTask `
-TaskName $_.BaseName `
-TaskPath $newTaskPath `
-Xml (Get-Content $_.FullName -Raw) `
-User $taskRunnerUserName `
-Password $taskRunnerPassword `
-CimSession $c
#Force to overwrite existing tasks
#-Force
;
}
@aev-mambro2
Copy link
Author

aev-mambro2 commented Jul 19, 2021

Use this Powershell script to import scheduled tasks (that you will have exported before) from a local folder structure to a remote Microsoft Windows server in your network. Use this together with the export script to migrate tasks from one server to another: https://gist.github.com/aev-mambro2/29bc3092b1af001dbd3d271d73172340

The script assumes that the folder structure for the files to import aligns with the wanted scheduled tasks path structure on the remote server. The script will create a task path structure in the remote server, that mimics the file system's folder structure found at input folder, given the existence of a scheduled task XML file.

Supply the name of the remote server. It must exist in your network and you must have the privilege to create scheduled tasks on it.
Supply the folder in which the XML files live, that need to get imported. Generate these for instance by exporting scheduled tasks.
Supply the root of the task scheduler path at which the new tasks should get imported. This is added to the input folder to look up local files, and supplied as the (new) root for import. If such a root did not yet exist on the remote server, it will get created.
Supply the name of the user of the account that will be running the tasks. You probably want to have this be a network account. Make sure this account has the required user privileges to create, change, and start scheduled tasks.
Supply the password for that same account.

If you're looking to overwrite existing tasks, be sure to remove the # sign on line 20. That forces the command to write the task whether it is new or exists already. With that # in place, the line is turned into a comment, and will be ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment