Backing up all SCSM Management Packs

As with most things I tend to post, they may not be the most complex things in the world, but they are things that are in my routine.

The following is a simple script to export and back up all of the Management Packs in my Service Manager installation. Most of the code is a rip/replace from my On Demand Backup of All Registered DHCP Servers.

Import-Module smlets
Import-Module pscx 

#region User Changable Variables
$OutputFolderBase = 'C:\working\SCSM_MPBackups\'
$SCSM_ManagementServer = ''
$HistoricalCopiesToKeep = 50

#region General Variables & Output Object Locations
$now = Get-Date -DisplayHint DateTime -Format yyyy_MMM_dd-HH-mm-ss-tt
[String]$OutputFolder = "$OutputFolderBase$now\"
Write-Host -BackgroundColor Black -ForegroundColor Green $OutputFolder
$OutputFolder = New-Item -Path $OutputFolder -ItemType Directory

#region Backup All SM MPs
    Get-SCSMManagementPack -ComputerName $SCSM_ManagementServer | Export-SCSMManagementPack -TargetDirectory $OutputFolder

#region Clean Up

#Write out our zip file using the Write-Zip cmdlet provided from PSCX
Write-Zip -Path $OutputFolder -Level 9 -IncludeEmptyDirectories $OutputFolderBase\$

#Remove the uncompressed source files locally
Remove-Item $OutputFolder -Force -Recurse


#region Trim Historical Copies To Keep
    #Find number already saved
    $numbersaved = Get-ChildItem -Path $OutputFolderBase *zip
    #Store number of zip files to delete 
    $numbertotrim = $numbersaved.count - $HistoricalCopiesToKeep

    #Confirm number to delet is not 0 or lt 0
    if ($numbertotrim -le 0) {
        #Do Nothing.
        #No extra copies Exist.
        Write-Host Not deleting any copies. Current ZIP files saved is $numbersaved.Count
        } ELSE {
            #We have a valid $numbertotrim. Time to delete (if needed)
                Write-Host We may have extra copies. We are going to delete the following.
                $ZIPsToDelete = Get-ChildItem -Path $OutputFolderBase *zip | sort LastWriteTime | select -First $numbertotrim
                $ZIPsToDelete | Remove-Item -Force -ErrorAction SilentlyContinue -Verbose


One quick note about this one - the PSCX compression take me down to about 5-6Mb in size. As this is significantly larger than the previous DHCP backup posting, it may be worth while to filter out your target management packs. To do this, follow the example below. Hopefully you've historically done something that is 'standard' in your custom management packs that you can easily filter on. For instance, I've always put "Riston" somewhere in my management pack descriptions.

$ManagementPacks = Get-SCSMManagementPack -ComputerName $SCSM_ManagementServer;
$ManagementPacks | ? {$_.Description -like "*Riston*"} | Export-SCSMManagementPack -TargetDirectory $OutputFolder

Until Next Time!


Popular Posts