Skip to content

Instantly share code, notes, and snippets.

@thegooddoctorgonzo
Last active September 28, 2023 14:54
Show Gist options
  • Save thegooddoctorgonzo/132bdae1bd164abbdbae6b66ad591b67 to your computer and use it in GitHub Desktop.
Save thegooddoctorgonzo/132bdae1bd164abbdbae6b66ad591b67 to your computer and use it in GitHub Desktop.
Migrates scheduled tasks created by specified users and scripts from old server to new server. Also replaces all references to old server in ps1 files with new server name and disables those tasks on retired server.
# PowerShell function: Move scheduled tasks from old server to new server
# Author: thegooddoctorgonzo 20180424
Function Migrate-ScheduledTasks {
<#
.SYNOPSIS
Migrates scheduled tasks created by specified users and scripts from old server to new server. Also replaces all references to old server in ps1 files with new server name
.DESCRIPTION
Migrates scheduled tasks created by specified users and scripts from old server to new server. Also replaces all references to old server in ps1 files with new server name
.EXAMPLE
Move-ScheduledTasks -OldServer Serv1 -NewServer Serv2 -Authors domain\user -MoveScripts
.PARAMETER OldServer
Name of source server
.PARAMETER NewServer
Name of destination server
.PARAMETER Authors
Author of Scheduled Tasks to be moved. Accepts multiple inputs seperated by comma
.PARAMETER MoveScripts
Switch to move scripts in c:\Scripts to same folder on new server
.OUTPUT
None
.NOTES
Written by Steve Landry 20180424.
#>
[cmdletbinding()]
Param (
#Name of source server
[Parameter(Mandatory=$true)]
[string]$OldServer,
#Name of destination server
[Parameter(Mandatory=$true)]
[string]$NewServer ,
#Author of Scheduled Tasks to be moved. Accepts multiple inputs seperated by comma
[Parameter(Mandatory=$true)]
[string[]]$Authors ,
#Switch to move scripts in c:\Scripts to same folder on new server
[Parameter()]
[switch]$MoveScripts ,
#Location of folder on OldServer containing scripts
[Parameter()]
[string]$Repository ,
#Switch to leave tasks enabled on old, disabled on new, will still transfer task XMLs and scripts if MoveScripts switch enabled
[Parameter()]
[switch]$StageOnly
)
Process {
#create folder for exported tasks XML
if(!(Test-Path -Path ("\\" + $OldServer + "\c$\ExportedTasks")))
{
Invoke-Command -ComputerName $OldServer -ScriptBlock {New-Item -Path c:\ExportedTasks -ItemType Directory -Verbose}
}
#export scheduled task XML
Invoke-Command -ComputerName $OldServer -ScriptBlock {$Auth = $args;Get-ScheduledTask -TaskPath '\' | Where-Object -FilterScript {$_.State -ne 'Disabled' -and $Auth -contains $_.Author} | foreach{ Export-ScheduledTask -TaskName $_.TaskName -TaskPath $_.TaskPath | Out-File -FilePath (Join-Path "C:\ExportedTasks\" "$($_.TaskName).xml") -Force} } -ArgumentList $Authors -Verbose
#copy XML folder to new server and rename
Copy-Item -Path (Join-Path "\\" "$($OldServer.ToString())\c$\ExportedTasks") -Destination (Join-Path "\\" "$($NewServer.ToString())\c$\") -Recurse -Verbose
Rename-Item -Path (Join-Path "\\" "$($NewServer.ToString())\c$\ExportedTasks") -NewName ImportedTasks -Verbose
#movescripts switch present - copy scripts old to new
if($MoveScripts)
{
Copy-Item -Path ("\\" + $OldServer + "\" + $Repository.Replace(':','$')) -Destination ("\\" + $NewServer + "\C$\" ) -Force -Recurse -Verbose
}
#collect individual .ps1 files
$files = Get-ChildItem ("\\" + $NewServer + "\" + $Repository.Replace(':','$')) -Include "*.ps*" -Recurse
#and replace references to old server with new server
foreach($file in $files)
{
(Get-Content -Path $file.FullName) -replace $oldSErver.ToString(), $NewServer.ToString() | Set-Content $file.FullName -Verbose
}
#import tasks to new server
Invoke-Command -ComputerName $NewServer -ScriptBlock {$XMLs = Get-ChildItem -Path "C:\ImportedTasks\"; foreach($XML in $XMLs){Register-ScheduledTask -Xml (Get-Content $XML.FullName | Out-String) -TaskName $XML.Name -User "DOMAIN\USER" -Password "P@ssw0rd" -Force}} -Verbose
if(!($StageOnly))
{
#disable tasks on old server
Invoke-Command -ComputerName $OldServer -ScriptBlock {$Auth = $args;Get-ScheduledTask -TaskPath '\' | Where-Object -FilterScript {$_.State -ne 'Disabled' -and $Auth -contains $_.Author} | Disable-ScheduledTask } -ArgumentList $Authors -Verbose
}
else
{
Invoke-Command -ComputerName $NewServer -ScriptBlock {$Auth = $args;Get-ScheduledTask -TaskPath '\' | Where-Object -FilterScript {$_.State -ne 'Disabled' -and $Auth -contains $_.Author} | Disable-ScheduledTask } -ArgumentList $Authors -Verbose
}
}
}
@thegooddoctorgonzo
Copy link
Author

Added functionality:
Parameter -Repository: Designate location of folder containing scripts on OldServer
Parameter -StageOnly: Switch. Will copy scripts and task XMLs if designated, but will leave tasks on OldServer enabled and disable tasks on NewServer

Fixes:
$files variable was pointed to incorrect directory, now points to $Repository

@thegooddoctorgonzo
Copy link
Author

Fixes: Fixed Copy-Item destination path

@thegooddoctorgonzo
Copy link
Author

Fixes: Fixed Get-ChildItem line to collect all/only .ps files

@thegooddoctorgonzo
Copy link
Author

Fixes: Fixed Get-Content - substituted .Replace with -replace to ignore letter case

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