Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Backs up ConfigMgr Site Maintenance Task settings from WMI to json files
## 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!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment