We use this as a backup for our task sequences that we run periodically, but it
should be a good starting point. The script isn’t the cleanest but it works. It
runs when it calls the Export-TaskSequences function with the –filter
parameter.
Good idea for a blog post, I might throw this out there this week.
if ([IntPtr]::size -ne 4) {
write-error "This script must be ran with 32-bit PowerShell."
} else {
$CMModule = 'C:\Program Files (x86)\Microsoft Configuration
Manager\AdminConsole\bin\ConfigurationManager.psd1'
if ($CMModule) {
$date = Get-Date -UFormat %m%d%Y_%H%M%S
$tsbackup = "\\server\osd\TaskSequences\ConfigMgr2012\Backup_$date\"
[environment]::CurrentDirectory = Get-Location -PSProvider FileSystem
Copy-Item .\tsDocumentorv2.xsl -Destination $tsbackup
New-Item $tsbackup -ItemType directory
function Export-TaskSequences {
param (
$filter
)
Import-Module $CMModule
Set-Location CAS:
$tasksequences = Get-CMTaskSequence -Name $filter
foreach ($ts in $tasksequences) {
$tsname = $ts.Name
write-host "`nTask Sequence:"$tsname
#$tspathtxt = $tsbackup + "$tsname.txt"
$tspathxml = $tsbackup + "$tsname.xml"
#write-host "File Path:"$tspathtxt
write-host "File Path:"$tspathxml
Set-Location "c:\"
#$ts.Sequence | Out-File $tspathtxt -Force
Write-Output '<?xml-stylesheet type="text/xsl"
href="tsDocumentorv2.xsl"?>' | Out-File $tspathxml
$ts.Sequence | Out-File $tspathxml -Append
}
}
Export-TaskSequences -filter "1E*"
Export-TaskSequences -filter "Windows*"
} else {
write-error "Please install the Configuration Manager 2012 console
first!"
}
}
Daniel Ratliff
From: [email protected] [mailto:[email protected]] On
Behalf Of Atkinson, Matt
Sent: Friday, June 20, 2014 7:11 PM
To: [email protected]
Subject: RE: [mssms] [OT?]Automatically track edits to task sequences
Excellent, I’ll be glad to figure out where I went wrong.
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Daniel Ratliff
Sent: Friday, June 20, 2014 2:47 PM
To: '[email protected]'; '[email protected]'
Subject: RE: [mssms] [OT?]Automatically track edits to task sequences
I actually have this working at work. Will reply Monday if no one does earlier.
-----Original Message-----
From: Atkinson, Matt
[[email protected]<mailto:[email protected]>]
Sent: Friday, June 20, 2014 05:34 PM Eastern Standard Time
To: [email protected]<mailto:[email protected]>
Subject: [mssms] [OT?]Automatically track edits to task sequences
Hi All,
Hopefully this isn’t too far off topic, but I’m trying to hack together a
method to automatically capture a copy of our task sequences whenever they are
modified for easy documentation and version control. Having some trouble and
figured someone out there might be able to tell me what I’m doing wrong or
provide a better solution all together.
So far I’ve been trying to combine the automagic task sequence documentor
stylesheet from here:
http://blogs.technet.com/b/deploymentguys/archive/2009/02/20/update-to-the-tasksequence-documentor.aspx?PageIndex=1#pi36647=2
and the WMI event subscription functions to kick off a script Powershell script
whenever the TS gets modified. I used the PowerEvents PS module by Trevor
Sullivan (http://powerevents.codeplex.com/) to build the WMI filter/consumer
and it kicks off the following command line:
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -executionpolicy
unrestricted d:\Scripts\ExportPowershellTS.ps1 %TargetInstance.PackageID%
ExportPowershellTS.ps1 looks like this:
## Set a parameter for the package ID
param(
[string]$TSPkgID
)
## Import the SCCM Powershell Module
import-module"D:\Program Files\Microsoft Configuration
Manager\AdminConsole\bin\configurationmanager.psd1"
## Change location to the SCCM site
set-locationXXX:
## Gather the timestamp
$Timestamp= (Get-Date -Format
s).Replace("/","-").Replace(":",".").Replace("T"," ")
## Gather the task sequence name property
$TSName= (Get-CMTaskSequence -TaskSequencePackageID "$TSPkgID" | select
-ExpandProperty Name)
## Gather the XML for the actual task sequence steps
$TS= Get-CMTaskSequence -TaskSequencePackageID "$TSPkgID" | select
-ExpandProperty Sequence
## Insert stylesheet to use on the top line of the task sequnce file
Write-Output'<?xml-stylesheet type="text/xsl" href="tsDocumentorv2.xsl"?>' |
Out-File "D:\temp\$TSName$Timestamp.xml"
## Append task sequence Steps
Write-Output$TS | Out-File "D:\temp\$TSName$Timestamp.xml" -Append
So far it’s half working, seems to be getting tripped up by the command line
with the %TargetInstance.PackageID% variable. The variable isn’t getting a
value, so it’s just creating a file with the style sheet info on the top line
and the timestamp in the file name. It isn’t able to gather the task sequence
name or steps without that value.
Eventually I’m planning to have the file and stylesheet zipped up and uploaded
to a TeamSite page for people to track the TS changes that were made.
________________________________
This message is intended for the sole use of the addressee, and may contain
information that is privileged, confidential and exempt from disclosure under
applicable law. If you are not the addressee you are hereby notified that you
may not use, copy, disclose, or distribute to anyone the message or any
information contained in the message. If you have received this message in
error, please immediately advise the sender by reply email and delete this
message.
The information transmitted is intended only for the person or entity to which
it is addressed
and may contain CONFIDENTIAL material. If you receive this material/information
in error,
please contact the sender and delete or destroy the material/information.
________________________________
This message is intended for the sole use of the addressee, and may contain
information that is privileged, confidential and exempt from disclosure under
applicable law. If you are not the addressee you are hereby notified that you
may not use, copy, disclose, or distribute to anyone the message or any
information contained in the message. If you have received this message in
error, please immediately advise the sender by reply email and delete this
message.
The information transmitted is intended only for the person or entity to which
it is addressed
and may contain CONFIDENTIAL material. If you receive this
material/information in error,
please contact the sender and delete or destroy the material/information.