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:
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:
Scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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!" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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" | |
} |
Hi, I’ve just tried your scripts (awesome idea by the way), the backup part seems to work great, but whenever I try to restore, it gives me the following error for each setting/task
The property ‘xx’ cannot be found on this object. Verify that the property exists and can be set.
Do you know why that would be?
I’ve looked at your jason files and the data seems to have been backed up properly.
Thks in advance and don’t hesitate if you have any questions.
Steph
Hi Steph, which line of the script returns an error? Can you post the full error?
Which ConfigMgr version are you using?
Hello it’s the command ConvertFrom-Json that gives the following error :
ConvertFrom-Json : Invalid object passed in, ‘:’ or ‘}’ expected. (1): {
At line:34 char:62
+ $JSON = Get-Content -Path $File -ErrorAction Stop | ConvertFrom-Json -E …
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
Never mind, I just inserted : |out-string| and it works.
Original command line(35) : $JSON = Get-Content -Path $File -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
New command line (working) : $JSON = Get-Content -Path $File | Out-String | ConvertFrom-Json -ErrorAction Stop
Thank you.
Good to know, thanks. I updated the script.
Hmm, when I try this on our Primary Site Server, it finds no Maintenance Tasks and sure enough, using WMI Explorer, that class is indeed empty, despite us having all the regular set of rules.
Excellent script my friend. I tested the script on CAS and PRI. Works perfectly.