21 August 2019

Extract SCCM WIM from ISO and Configure for WDS via PowerShell

One issue, more like a time-waster, I have had as we are migrating to SCCM from MDT is getting the boot image on the WDS server. SCCM is not configured to manage WDS because we use it for other things, so we manually upload the WIM. SCCM creates an ISO that needs to be mounted to extract the WIM for WDS. Thanks to this blog post, it is explained. I wanted the steps automated, so I wrote the following PowerShell script that will perform all of the steps needed to extract and configure the WIM.

To use this, populate the full file name in the $File variable. The script will do the rest. It is documented throughout the script so that it is easy to follow. You can download the script from my GitHub site.

           Extract and Configure SCCM WIM Boot Image  
           This script will extract the WIM file from the ISO boot image generated by SCCM. It then mounts, configures, and unmounts the WIM file so it is ready to push up to WDS.  
      .PARAMETER File  
           Filename of the ISO, which includes the full path.  
           Created with:    SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.142  
           Created on:      8/21/2019 12:59 PM  
           Created by:      Mick Pletcher  
           Filename:        SCCMBootImage.ps1  
      [string]$File = 'C:\WinPE\SCCM.iso'  
 Import-Module Dism  
 #Extract the name of the directory the $File resides in  
 $Directory = $File.substring(0, $File.LastIndexOf('\'))  
 #Mount and assign drive letter  
 $Drive = ((Mount-DiskImage -ImagePath $File) | Get-Volume).DriveLetter  
 #Delete old Mount directory  
 Remove-Item -Path ($Directory + '\Mount') -Recurse -Force -ErrorAction SilentlyContinue  
 #Create Mount folder to copy WIM contents to  
 New-Item -Path ($Directory + '\Mount') -ItemType Directory -Force  
 #Delete old WIM file  
 Remove-Item -Path ($Directory + '\boot.wim') -ErrorAction SilentlyContinue -Force  
 #Copy boot.WIM file to $Directory  
 Copy-Item -Path ($Drive + ':\sources\boot.wim') -Destination $Directory -Force  
 #Turn off read only  
 Set-ItemProperty -Path ($Directory + '\boot.wim') -Name IsReadOnly -Value $false  
 #Mount the WIM file  
 Mount-WindowsImage -ImagePath ($Directory + '\boot.wim') -Index 1 -Path ($Directory + '\Mount')  
 #Copy data folder to mounted image  
 Copy-Item -Path ($Drive + ':\SMS\data') -Destination ($Directory + '\Mount\sms') -Recurse -Force  
 #Unmount Windows Image  
 Dismount-WindowsImage -Path ($Directory + '\Mount') -Save  
 #Dismount disk image  
 Dismount-DiskImage -ImagePath $File  
 #Delete the old wim if it exists  
 Remove-Item -Path ((Get-ChildItem -Path ($Directory + '\' + ((Get-ChildItem -path $File -ErrorAction SilentlyContinue).Basename) + '.wim')).FullName) -Force -ErrorAction SilentlyContinue  
 #Rename boot.wim to match the basename of the ISO  
 Rename-Item -Path ($Directory + '\boot.wim') -NewName ((Get-ChildItem -Path $File).BaseName + '.wim') -Force  
 #Delete the Mount folder  
 Remove-Item -Path ($Directory + '\Mount') -Recurse -Force -ErrorAction SilentlyContinue  


Post a Comment