Let’s start with the problematic – currently in Config Manager there is no way to backup your Task Sequences and in a large environment, where multiple people are operating with the MEMCM Console (I still can’t get used to calling SCCM – MEMCM – Thanks Microsoft 🙁 )

This could be quite a problem. Moreover the MEMCM Audit log tells us who made a change to an existing task sequence or deleted it, but not what changes exactly were done or what steps were in the task sequences.

Therefore in my projects, I like to create a small script, with a schedule task, which exports daily the task sequences, in a specific folder and keeps the last X number of backups.

The Script can be also assigned with a scheduled task on the server, as described in this post:

ConfigMgr Scheduled Tasks with Service Account and PowerShell – ConfigRoar

This is the code I use

#Author: Nikolay Marinov - ConfigRoar.com


$date=Get-Date -Format ddMMyyyy

#define variables
$tsbackupshare="<Insert Path to Local Share>"
$lastbackupstokeep=10 #or change to other number if you want to 

#create folder
if(!(Test-Path filesystem::$tsbackupshare\$date))
    {
        New-Item -Path "Filesystem::$tsbackupshare" -ItemType Directory -Name $date
    }

#Import SCCM PowerShell Module
import-module (Join-Path $(Split-Path $env:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)  
cd ((Get-PSDrive -PSProvider CMSite)[0].Name + ':')

$tsnames=(Get-CMTaskSequence -Fast).Name

$path="$tsbackupshare\$date"

foreach($tsname in $tsnames)
    {
        $ts=get-cmtasksequence -Name $tsname -Fast | Export-CMTaskSequence -ExportFilePath "$tsbackupshare\$date\$tsname.zip"
    }

#cleanup folders
$folderstodelete=Get-ChildItem -Path $tsbackupshare | sort -Descending LastWriteTime | select -Skip $lastbackupstokeep

#set a safety mechanism to not delete more than 2 folders at a time

if($folderstodelete.Count -le 2)
    {
        foreach($foldertodelete in $folderstodelete)
            {
                Remove-Item -Path $tsbackupshare\$foldertodelete -Recurse -Force
                Write-Host "Removing Folder $tsbackupshare\$foldertodelete"
            }
    }
else
    {
        Write-Error "Too Many Folders to delete. Stopping execution"
    }

For those wondering, why this “safety” quota is there – I don’t like the “Remove-Item” cmdlet and being a bit paranoid for not deleting half the C:\Windows on a server, I always put small quotas on my code just for my own calmness. Normally the script should not have to remove more than one folder at a time, as when the number of backups to keep is being reached, the oldest backup will be deleted on every execution

Note: As the MEMCM PowerShell CMD-Let’s are pretty slow (let’s face it) a possible improvement for larger environments could be the substitute with WMI CMD-Lets

Logging can be added as well 🙂

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *