Backup and Restore ConfigMgr Site Maintenance Task Settings with PowerShell

According to the official Microsoft documentation, when upgrading to System Center Configuration Manager (current branch), it is recommended to disable any site maintenance tasks that could run during the upgrade (as they can cause the upgrade to fail), and to make a record of the task schedules for any disabled tasks:

Before you disable a task, record the schedule of the task so you can restore its configuration after the site upgrade completes.

Since I don’t want to manually note down the schedules for my maintenance tasks, I wrote a couple of PowerShell scripts to backup and restore the site maintenance task settings for me.

The backup script will export the settings for each site maintenance task and save them to file in json format.

The restore script will then read each backup file, compare the current settings in WMI with the backed-up settings for each maintenance task, and update anything that doesn’t match. The script will tell you if anything has changed and what the old and new values are.

If you run the backup before you disable the tasks, the restore script will re-enable them again.

Both scripts should be run elevated on your primary site server.

Backup-CMSiteMaintenanceTaskSettings

To backup, simply pass the sitecode and the backup directory to the script:


Backup-CMSiteMaintenanceTaskSettings -SiteCode ABC -BackupDirectory G:\Backup\SiteMaintenanceTasks

You will then see a json file for each site maintenance task in the backup directory:

jsonfiles

Restore-CMSiteMaintenanceTaskSettings

To restore, pass the same parameters to the script:


Restore-CMSiteMaintenanceTaskSettings -SiteCode ABC -BackupDirectory G:\Backup\SiteMaintenanceTasks

The script will read each file then compare the settings with the current task settings in WMI. If there are any differences, these will be updated and reported:

restore

Scripts

## Saves ConfigMgr Site Maintenance Task settings from WMI to json files ##
## Should be run on the Primary Site Server ##
## Should be run as administrator ##
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory=$True)]
[ValidateNotNullorEmpty()]
[string]$SiteCode,
[Parameter(Position=1,Mandatory=$True)]
[ValidateNotNullorEmpty()]
[string]$BackupDirectory
)
Try
{
# Load maintenance tasks from WMI
$MTs = Get-WmiObject Namespace ROOT\SMS\site_$SiteCode Class SMS_SCI_SQLTask ErrorAction Stop
}
Catch
{
$_
Return
}
If ($MTs)
{
# Iterate through each task, convert to hash table, output to json format, save to file
$MTs | foreach {
# Convert to hash table
$Values = @{}
$_.Properties | Foreach {
$Values.Add($_.Name, $_.Value)
}
Try
{
# Convert to json format, save to file
$Values | ConvertTo-Json ErrorAction Stop | Out-File "$BackupDirectory\$($_.TaskName).json" ErrorAction Stop Force
}
Catch
{
$_
}
}
}
Else
{
Write-Warning "No maintenance tasks found!"
}

## Restores ConfigMgr Site Maintenance Task settings from json files (backed up using the 'Backup-CMSiteMaintenanceTaskSettings' script ##
## Should be run on the Primary Site Server ##
## Should be run as administrator ##
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory=$True)]
[ValidateNotNullorEmpty()]
[string]$SiteCode,
[Parameter(Position=1,Mandatory=$True)]
[ValidateNotNullorEmpty()]
[string]$BackupDirectory
)
Try
{
# Get the list of backed-up json files
$Files = Get-ChildItem $BackupDirectory File Filter "*.json" ErrorAction Stop | Select ExpandProperty FullName
}
Catch
{
$_
Return
}
# Flag for no changes
$NoChanges = $True
# Iterate through each file (maintenance task)
Foreach ($File in $Files)
{
Try
{
# Convert the file content from JSON format
$JSON = Get-Content Path $File ErrorAction Stop | Out-String | ConvertFrom-Json ErrorAction Stop
}
Catch
{
$_
Continue
}
# Convert the JSON psobject to a hash table
$SavedSettings = @{}
$Names = $JSON | Get-Member MemberType properties | Select-Object ExpandProperty name
$Names | ForEach-Object {
$SavedSettings.Add($_,$JSON.$_)
}
# Get the corresponding maintenance task from WMI
Try
{
$MT = Get-WmiObject Namespace ROOT\SMS\site_$SiteCode Class SMS_SCI_SQLTask Filter "TaskName='$($SavedSettings.TaskName)'" ErrorAction Stop
}
Catch
{
$_
Continue
}
# Iterate through each backed-up setting
$SavedSettings.GetEnumerator() | foreach {
$SavedSetting = $_
# Get the current setting value from WMI
$CurrentSetting = $MT.Properties | Where {$_.Name -eq $SavedSetting.Name}
# If the backed-up setting does not match the current setting
If ($SavedSetting.Value -ne $CurrentSetting.Value)
{
# Output the setting values we will restore
Write-host "Updating Task '$($MT.TaskName)':" ForegroundColor Yellow
Write-Host " Setting: $($SavedSetting.Name)"
Write-host " Current Value: $($CurrentSetting.Value)"
Write-host " Restored Value: $($SavedSetting.Value)"
# Flag that something was restored
$NoChanges = $False
# Set the new value in WMI
$MT.$($SavedSetting.Name) = $SavedSetting.Value
Try
{
[void]$MT.Put()
}
Catch
{
$_
}
}
}
}
# If no changes were detected
If ($NoChanges)
{
Write-Host "No differences were detected between the backed-up task settings and the current task settings"
}