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
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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

five × five =

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