PowerShell – Archive all Task Sequences to XML with Index

We’ve amassed a very large number of task sequences since migrating to Configuration Manager 2012 and it got me thinking about ways to archive off older sequences so that we can clean house.  So I came up with this script.

The script will first collect all of the task sequences in your site.  Next it will enumerate through them, write the sequence to an XML named after the task sequence ID and finally create a CSV index of the TSID, task sequence name, the last modified date of the sequence and when the sequence was backed up.

Import-Module (Join-Path $(Split-Path $env:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1) 
$SiteCode = "CM1" 
$BackupFolder = 'D:\TS_Backups' 
$IndexFile = "$BackupFolder\_Index.csv" 
$Date = Get-Date -Format yyyy-MM-dd 
$StringDate = $Date.ToString() 
Set-Location $SiteCode`:\ 
IF (!(Test-Path $IndexFile))
 {
 #Prep the Index
 Add-Content -Value "TSID,Name,Last Modified Date,Backup Date" -Path $IndexFile
 }
 Write-Host "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" -ForegroundColor White -BackgroundColor Black 
Write-Host "Pulling all of the Task Sequences..." -ForegroundColor Cyan 
$TSList = Get-CMTaskSequence 
Write-Host "Done!" -ForegroundColor Green 
Write-Host " " 
Write-Host "$($TSList.Count) sequences" -ForegroundColor Cyan 
Write-Host "Exporting task sequences to XML..." -ForegroundColor DarkCyan 
FOREACH ($TS in $TSList)
 {
 Set-Content -Path “$BackupFolder\$($TS.PackageId).xml” -Value $TS.Sequence
 $LastModDate = Get-date($TS.LastRefreshTime) -Format yyyy-MM-dd
 Add-Content -Value "$($TS.PackageId),$($TS.Name),$LastModDate,$StringDate" -Path $IndexFile
 } 
Write-Host "Done!" -ForegroundColor Green 
Write-Host "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" -ForegroundColor White -BackgroundColor Black

To modify the script to work in your site you just need to modify line 3 for your site code and line 4 for your backup location.

Import-Module (Join-Path $(Split-Path $env:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1) 
$SiteCode = "CM1" 
$BackupFolder = 'D:\TS_Backups' 
$IndexFile = "$BackupFolder\_Index.csv"

Sample CSV Index:

TSID,Name,Last Modified Date,Backup Date
CM10000D,Nomad Build Server - Build v 2.0,2014-08-27,2015-06-03
CM10000F,Nomad Build Server - PreCache v 2.1,2015-05-29,2015-06-03
CM10001E,DEV2012 - Windows 7 Migration PreFlight v 2.0,2014-07-10,2015-06-03
CM100020,DevCoreImage Utilities 2.0,2014-09-02,2015-06-03
CM100021,RETIRED-Nomad Build Server - Cache Monitor v 2.0_1,2015-03-30,2015-06-03
Screen Shot 2015-06-03 at 2.33.13 PM

Mike Marable

I am the OSD lead and a senior engineer for the Configuration Manager client group at Michigan Medicine (formerly the University of Michigan Health System). We manage 40,000 systems throughout the health system and medical school with Configuration Manager. I have been doing OS deployments for nearly 25 years, 14 of which have been with Michigan Medicine. I’ve lead the engineering efforts of moving our OS Deployment from a custom solution to Configuration Manager, our Windows XP to Windows 7 and Windows 7 to Windows 10 efforts. My passion over the last 2+ years has been Windows as a Service.

Add comment

9 + nineteen =

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

%d bloggers like this: