ConfigMgr OSD: Dynamically Named WIM Captures with PowerShell

ConfigMgr OSD: Dynamically Named WIM Captures with PowerShell

In the context of the Operating System Deployment (OSD) feature in Microsoft System Center Configuration Manager (ConfigMgr), it is common that customers will perform a “build & capture” of their target operating system, and then deploy new computers using that reference (aka. “gold” or “base”) WIM image. This process is typically automated through a build & capture task sequence. The last step of a build & capture task sequence is typically the Capture Operating System task sequence item.

Capture Operating System
Capture Operating System

Within the configuration of the Capture Operating System task sequence step, most users of ConfigMgr will simply specify a static path to the destination of the resulting WIM image. This static naming can cause conflicts if the task sequence is executed multiple times, without first renaming the target file. Additionally, the same scenario can occur if the build & capture task sequence is executed on multiple, distinct systems simultaneously.

To solve for this, we can use a Windows PowerShell script that sets the OSDCaptureDestination task sequence variable to a dynamic value. For example, you can insert the current date-time into the target WIM file path, to ensure that the resulting file name is unique. I have written a parameterized PowerShell script that allows you to specify the folder path, as well as a prefix to add to the file. That script is included below.

<#

.Author
Trevor Sullivan 

.Synopsis
Dynamically sets the OSD capture location, to avoid naming and file locking conflicts.

.Date
2014-05-21 

.Parameter Prefix
The -Prefix parameter specifies the file name prefix that will be used to generate the dynamic file name. 

.Parameter Path 
The -Path parameter specifies the UNC folder path that will be used to generate the dynamic destination file path for the WIM image.
#>

# Define a parameter block, with default parameter values.
param (
    [Parameter(Mandatory = $false)]
    [string] $Path = '//sccm01/d$/Source/Operating System Images/Captures'
    ,
    [Parameter(Mandatory = $false)]
    [ValidatePattern('[a-zA-Z0-9.-s]')]
    [string] $Prefix = 'Windows 8.1 Enterprise 64-bit'
)

# Generate the dynamic file path and log it
$FilePath = '{0}/{1}.{2}.wim' -f $Path, $Prefix, (Get-Date -Format 'yyyy-MM-dd hh-mm-ss');
$LogText = 'Capture file path is: {0}' -f $FilePath;
Set-Content -Path $env:WinDir/Temp/OSDCapture.log -Value $LogText;
Write-Host -Object $LogText;

# Set the OSDCaptureDestination task sequence variable
$TSEnv = New-Object -ComObject Microsoft.SMS.TSEnvironment;
$TSEnv.Value('OSDCaptureDestination') = $FilePath;
Run PowerShell Script Task Sequence Item
Run PowerShell Script Task Sequence Item

To implement this script, simply follow these steps:

  1. Create a generic “OSD Scripts” Configuration Manager package
  2. Add the script to the package
  3. Distribute the package to your Distribution Points
  4. Add a Run PowerShell Script task sequence item to your build & capture task sequence
  5. Configure the task sequence item to call the PowerShell script

Once you have implemented this PowerShell script, you will have dynamically named WIM files in your capture directory, and will not have to worry about naming conflicts, or file locking issues!