Patch Current WIM Files
Patch Current WIM Files, When we started offering up patch current WIM files last year, we were trying to eliminate the time needed for patching during a deployment. In beginning this process, I quickly wrote out a script to do what I wanted, and then put it away until the next month. Each month it became a little more refined and easier to use. Now I can kick off a couple VMs, run the script and a few hours later (depending on your anti-virus) have patch current WIMs the day after Patch Tuesday.
I have read many blogs on the subject and while they may not have had an all-in-one answer for me, I have taken pieces from everyone for what I was trying to do. If I am using something you wrote, thank you. I’ll have some acknowledgements at the end of the article.
Due to having to use a proxy, I cannot automate the gathering of the updates, but if you can, you can easily add that to this process.
First, we have a network share that keeps everything. Different directories for OEM files, monthly patches, Upgrade packages, and reference WIMs.
Once 1803 came out, I saw that I needed to make this more dynamic for future releases. To accommodate this I have a couple of questions based on which version you are editing, such as 1709, 1809, etc. We also maintain different languages, so you would enter en-us or another language if needed.
#######################
Param(
[Parameter(Position=0, HelpMessage=”Enable pause for troubleshooting. Default is False”)] [ValidateSet(‘True’,’False’)] [string]$EnablePause = “False”, [Parameter(HelpMessage=”Override destination source directory. Default is \\server.com\source_directory”)] [string]$DestDirectory = “\\server.com\source_directory”)
[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’) | Out-Null
$OSVersion = [Microsoft.VisualBasic.Interaction]::InputBox(“Enter Windows 10 version `n (Example 1809 = 17763) `n 1709 = 16299 `n 1803 = 17134 `n 1809 = 17763”, “Windows 10 Version”, “16299”)
if($OSVersion.Length -lt 1)
{
Write-Host “No OS Value Entered”
Exit
}
Write-Host “$OSVersion” Selected -ForegroundColor Yellow
$Language = [Microsoft.VisualBasic.Interaction]::InputBox(“Enter Language version `n (Example en-us) `n en-us “, “Language Version”, “en-us”)
if($Language.Length -lt 1)
{
Write-Host “No Language Value Entered”
Exit
}
Write-Host “$Language Selected” -ForegroundColor Yellow
$CreateProdMedia = [Microsoft.VisualBasic.Interaction]::InputBox(“Create Production Media? `n (Example false) `n true `n or `n false”, “Create Production Media”, “false”)
if($CreateProdMedia.Length -lt 1)
{
Write-Host “No Production Media Value Entered”
Exit
}
#######################
The Create Production Media section above will create an SCCM Operating System Upgrade package for you if you select “true” when prompted.
Next, it is important to place your network shares correctly. The naming convention I use for $0Source for the OEM WIM makes sense to me, but may not make sense to you, so update accordingly.
#########
Function DownloadSource {
Write-Host “Copying WIM File” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan
$copysourcedir = “\\server.com\source_directory”
$0Source = $OSVersion + “.0source_” + $Language
$OEM = (Get-ChildItem $copysourcedir -Directory | select-string -Pattern ($0source) | Sort-Object -Descending | Select-Object -First 1)
Write-Host “Copying $Language source files for Windows 10 $OSVersion” -ForegroundColor Yellow
copy-item $copysourcedir\$OEM\sources\install.wim $PSScriptRoot\$OSVersion\$Language\install_$Language.wim -Verbose
$TimeStamp = Get-Date
}
##########################
That will copy the WIM file to where you are running the script from. It will create directories based on built number and language.
The next function is just a cleanup if the directories already exist on your system from a previous month.
##############
Function Cleanup {
Try {
Write-Host “Cleaning Up” -ForegroundColor Green
if (Test-Path -path $PSScriptRoot\$OSVersion\$Language) {Remove-Item -Path $PSScriptRoot\$OSVersion\$Language -Force -ErrorAction Continue}
& DISM /Cleanup-WIM
}
Catch
{
Write-Warning “Cleanup failed”
Throw $Error
}
}
################
After the functions are setup, we next start logging and check on a few items. If ADK is not installed, it will exit out. If the mount directory does not exist, it will create it.
########
#Script Logging. Stop Transcript if already running and begin.
$ErrorActionPreference=”SilentlyContinue”
Stop-Transcript | out-null
$ErrorActionPreference = “Continue” # or “Stop”
Start-Transcript -path “$PSScriptRoot\$OSVersion $Language updateWIM.log” -append
#Check for Windows ADK Installation
Write-Host “Checking for ADK Installation” -ForegroundColor Yellow
$ADKdir = Get-ChildItem “C:\Program Files (x86)\Windows Kits\10”
if (!( $ADKdir))
{Write-Host “Windows ADK is not installed.” -ForegroundColor DarkRed
Exit
}
#Check for Mount Directory
$Mountdir = “$PSScriptRoot\$OSVersion\$Language\mount”
if (!(test-path -path “$PSScriptRoot\$OSVersion\$Language\mount”)) {New-Item -ItemType directory -Path “$PSScriptRoot\$OSVersion\$Language\mount”
Write-Host “Creating Mount Directory” -ForegroundColor Yellow}
else
{Write-Host “Mount Directory Already Exists” -ForegroundColor Yellow}
########################
The next section will copy the updates that are being used to update the WIM, including any CU (I put flash and .net in the same folder) SSU or Dynamic Updates. It will look for the newest folder for the CU patches or the naming convention I have set up below.
#############################
#Copying Servicing Stack Update and Cumulative Updates
Write-Host “Copying Servicing Stack Update to Working Directory” -ForegroundColor Yellow
$PatchSource = “\\server.com\source_directory\Patches”
$SS = “SS”
$CU = “CU”
$DU = “DU”
$UPDATES = $SS,$CU,$DU
Foreach ($i in $UPDATES)
{
if (!(test-path -path “$PSScriptRoot\$OSVersion\$Language\$i”)) {New-Item -ItemType directory -Path “$PSScriptRoot\$OSVersion\$Language\$i”
Write-Host “Creating $i Directory” -ForegroundColor Yellow}
}
$SERVESTACK = “servicestack_” + $OSVersion
$PatchDir = (Get-ChildItem $PatchSource -Directory | select-string -Pattern ($SERVESTACK) | Sort-Object -Descending | Select-Object -First 1)
Write-Host “Copying $PatchDir” -ForegroundColor Yellow
copy-item $PatchSource\$PatchDir\* $PSScriptRoot\$OSVersion\$Language\SS #-Verbose
$PATCHFOLDER = $OSVersion + “.”
$PatchDir = (Get-ChildItem $PatchSource -Directory | select-string -Pattern ($PATCHFOLDER) | Sort-Object -Descending | Select-Object -First 1)
Write-Host “Copying $PatchDir” -ForegroundColor Yellow
copy-item $PatchSource\$PatchDir\* $PSScriptRoot\$OSVersion\$Language\CU #-Verbose
$DYNAMICUP = “DynamicUpdate_” + $OSVersion
$PatchDir = (Get-ChildItem $PatchSource -Directory | select-string -Pattern ($DYNAMICUP) | Sort-Object -Descending | Select-Object -First 1)
Write-Host “Copying $PatchDir” -ForegroundColor Yellow
copy-item $PatchSource\$PatchDir\* $PSScriptRoot\$OSVersion\$Language\DU -Recurse #-Verbose
###########################
Now we call the download function from above and download the OEM WIM file. We then compare the WIM version versus the ADK installed in order to use the correct or newer version to modify the WIM file.
#################
#Download Source files
DownloadSource
Write-Host “Getting WIM and ADK information” -ForegroundColor Yellow
$WIMVersion = (Get-WindowsImage -ImagePath “$PSScriptRoot\$OSVersion\$Language\install_$Language.wim” -Name “Windows 10 Enterprise”).Version
$ENTindex = (Get-WindowsImage -ImagePath “$PSScriptRoot\$OSVersion\$Language\install_$Language.wim” -Name “Windows 10 Enterprise”).ImageIndex
$tempdir = Get-Location
$tempdir = $tempdir.tostring()
$dismpath = “C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\x86\DISM”
$appToMatch = ‘*Windows Assessment and Deployment Kit*’
$appVersion = $WIMVersion
$TimeStamp = Get-Date
$result = (get-item $dismpath\dism.exe).versioninfo.productversion
If ($result -lt $appVersion) {
Write-Host “Windows ADK is not correct version to modify WIM.” -ForegroundColor DarkRed
Exit
}
else
{Write-Host “Windows ADK $result and WIM Version $WIMVersion” -ForegroundColor Yellow
#################################
NOTE: If copying each section instead of downloading the script, that there is an open bracket at the Write-Host that continues below.
We now extract the WIM.
####################
# Extract ENT WIM
Write-Host “Exporting Enterprise WIM to new file” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan
Dism /Export-Image /SourceImageFile:”$PSScriptRoot\$OSVersion\$Language\install_$Language.wim” /SourceIndex:$ENTindex /DestinationImageFile:”$PSScriptRoot\$OSVersion\$Language\install-optimized_$Language.wim”
Write-Host “Export Index $ENTindex Completed” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan
##################
Next we just mount the WIM. Timestamps if you want to see how long you were bored.
###################
# Mount Wim File
Write-Host “Mounting WIM File” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan
#$ENTindex = (Get-WindowsImage -ImagePath “$PSScriptRoot\$OSVersion\$Language\install_optimized_$Language.wim” -Name “Windows 10 Enterprise”).ImageIndex
dism /mount-wim /wimfile:$PSScriptRoot\$OSVersion\$Language\install-optimized_$Language.wim /index:1 /mountdir:$PSScriptRoot\$OSVersion\$Language\mount
Write-Host “Mounting Completed” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan
###################
Now we simply patch the mounted WIM with the Servicing Stack, CU, and any Dynamic Updates.
###############
Write-Host “Updating WIM” -ForegroundColor Yellow
Foreach ($i in $UPDATES)
{
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan
dism /image:$PSScriptRoot\$OSVersion\$Language\mount /add-package /packagepath:$PSScriptRoot\$OSVersion\$Language\$i
Write-Host “$i Updates Completed” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan
}
If ($EnablePause -eq “True”) {
pause
}
####################
This is where the pause is in the script if enabled True. This allows you to check the for whatever reason you may have.
Next we reset the base and cleanup the WIM.
#############
# Cleanup WIM
Write-Host “Cleaning up WIM” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan
DISM /Cleanup-Image /Image=$PSScriptRoot\$OSVersion\$Language\mount /StartComponentCleanup /ResetBase
Write-Host “Cleanup Completed” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan
##############
And simply unmount the WIM.
####################
#Unmount WIM
Write-Host “Unmounting WIM File” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan
Dism /Unmount-Image /MountDir:”$PSScriptRoot\$OSVersion\$Language\mount” /Commit
Write-Host “Unmount Completed” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan
#####################
Now, if you said “True” for create production media it will first update your MDT with the WIM file. (If you do not create reference images, you can eliminate where needed.)
########################
If ($CreateProdMedia -eq “True”) {
If ($Language -eq “en-us”) {
#Copy to Production MDT
Write-Host “Copy WIM file to Production MDT” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan
$MDTwimpath = “\\MDTserver\Win10x64\Operating Systems”
$MDTOSVAR = “_b” + $OSVersion
$MDTsourceDir = (Get-ChildItem $MDTwimpath -Directory | select-string -Pattern ($MDTOSVAR) | Sort-Object -Descending | Select-Object -First 1)
copy-item $PSScriptRoot\$OSVersion\$Language\install-optimized_$Language.wim $MDTwimpath\$MDTsourceDir\Sources\install.wim -Force
Write-Host “Copy To Production MDT Completed” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan } #Copy to MDT close bracket
####################
The next section will create your upgrade package files that will be ready to be used with SCCM in a brand new directory and include any Dynamic Updates.
###################
#Create Upgrade Files
Write-Host “Creating Upgrade Files” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan
$PatchDir = (Get-ChildItem $PatchSource -Directory | select-string -Pattern ($PATCHFOLDER) | Sort-Object -Descending | Select-Object -First 1)
$UpdateVER = (“$PatchDir”).Remove(0,14)
Write-Host “$UpdateVER” -ForegroundColor Yellow
$DestDirectory = “\\server\destination”
$OS =”W10x64.ENT_” + $OSVersion + “_”
$NewDirectory = $OS + “.” + $UpdateVER + “_” + $Language
$0Source = $OSVersion + “.0source_” + $Language
$OEM = (Get-ChildItem $DestDirectory -Directory | select-string -Pattern ($0source) | Sort-Object -Descending | Select-Object -First 1)
$NewDirectoryName = (“$OEM”).Remove(17,19) + $UpdateVER + “_” + $Language
Write-Host “New Directory Name is $NewDirectoryName” -ForegroundColor Yellow
New-Item -ItemType directory -Path $DestDirectory\$NewDirectoryName
Copy-Item “$DestDirectory\$OEM\*” -Destination “$DestDirectory\$NewDirectoryName\” -Recurse -Verbose
Write-Host “OEM Source Files Copied” -ForegroundColor Yellow
$DYNAMICUP = “DynamicUpdate_” + $OSVersion
$PatchDir = (Get-ChildItem $PatchSource -Directory | select-string -Pattern ($DYNAMICUP) | Sort-Object -Descending | Select-Object -First 1)
copy-item $PatchSource\$PatchDir\sources* -Destination “$DestDirectory\$NewDirectoryName\Sources\” -Recurse -Verbose
Write-Host “Dynamic Update Source Files Copied… Now Copying Updated WIM File.” -ForegroundColor Yellow
copy-item $PSScriptRoot\$OSVersion\$Language\install-optimized_$Language.wim $DestDirectory\$NewDirectoryName\Sources\install.wim -Force
Write-Host “UIP Files Completed” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan
######################
SCCM Admin Console should be installed in order to run the next section. Your site code and provider machine name need to be entered.
After the standard generated code, a package is created in the Admin Console with current month information and build number.
#################
###### Generated from Configuration Manager
#
# Press ‘F5’ to run this script. Running this script will load the ConfigurationManager
# module for Windows PowerShell and will connect to the site.
#
# This script was auto-generated at ‘5/30/2018 8:08:22 AM’.
# Uncomment the line below if running in an environment where script signing is
# required.
#Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
# Site configuration
$SiteCode = “XXX” # Site code
$ProviderMachineName = “server.name.com” # SMS Provider machine name
# Customizations
$initParams = @{}
#$initParams.Add(“Verbose”, $true) # Uncomment this line to enable verbose logging
#$initParams.Add(“ErrorAction”, “Stop”) # Uncomment this line to stop the script on any errors
# Do not change anything below this line
# Import the ConfigurationManager.psd1 module
if((Get-Module ConfigurationManager) -eq $null) {
Import-Module “$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1” @initParams
}
# Connect to the site’s drive if it is not already present
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}
# Set the current location to be the site code.
Set-Location “$($SiteCode):\” @initParams
####### End of Generated Configuration Manager Code
$SCCMVersion = “10.0.” + $UpdateVER
$SCCMName = (“$NewDirectoryName”).Remove(0,11)
$SCCMName2 = (“$SCCMName”).split(‘_’)[0]
$SCCMName3 = “Win10x64 ” + $SCCMName2 + ” ” + $Language + ” ” + “($UpdateVER)”
$SCCMdescription = get-date -UFormat “%B %Y”
########################
This is the section is the actual detail for the packages created. It will also update your task sequence (we use test task sequences.)
####################
if ($OSVersion -eq 16299) {
$RETIRED = (Get-CMOperatingSystemInstaller -Name “RETIRED_*1709*$Language”) $RETIRED_ID = ($RETIRED.packageID)
Set-CMOperatingSystemInstaller -Id $RETIRED_ID -NewName “NEW_1709 _$Language” -Description “NEW” -Path “$DestDirectory\$NewDirectoryName” -Version “$SCCMVersion”
$NEWNAME = (Get-CMOperatingSystemInstaller -Name “NEW_*1709*$Language”)
if ($Language -eq en-us) {
Set-CMTSStepUpgradeOperatingSystem -TaskSequenceId XXXXXXXX -UpgradePackage $NEWNAME }
}
if ($OSVersion -eq 17134) {
$RETIRED = (Get-CMOperatingSystemInstaller -Name “RETIRED_*1803*$Language”) $RETIRED_ID = ($RETIRED.packageID)
Set-CMOperatingSystemInstaller -Id $RETIRED_ID -NewName “NEW_1803 _$Language” -Description “NEW” -Path “$DestDirectory\$NewDirectoryName” -Version “$SCCMVersion”
$NEWNAME = (Get-CMOperatingSystemInstaller -Name “NEW_*1803*$Language”)
if ($Language -eq en-us) {
Set-CMTSStepUpgradeOperatingSystem -TaskSequenceId XXXXXXXX -UpgradePackage $NEWNAME }
}
############################
Write-Host “Configuration Manger Package Creation Complete” -ForegroundColor Yellow
$TimeStamp = Get-Date
Write-Host “$TimeStamp” -ForegroundColor Cyan} #”Configuration Manager CMDLETS found Close Block
}
} #Process Close Block
Cleanup
Stop-Transcript
#############################
And that’s it. I had to clean it up a bit in order to share, but those are the main steps and processes. I will probably come back and add to it as I add more items. Including replicating out to a DP group as part of the process. I have hesitated as our SCCM guys don’t like when we replicate WIMS without checking site replication backlog first.
Feel free to contact me and comment or suggest as you want.
https://github.com/shotgn22/osd (wimUpdate.ps1)
Thank you for reading this far.
My name is Scott Graves, I have been doing IT since 1995 and am currently heavy into OSD, Task Sequences and Upgrades. I have set up a Twitter (@shotgn22) if you have any questions. Thank you Adam Gross of ASquareDozen.com and Chris Buck of this page. Dave Segura of OSD Builder, Mike Terrill, Gary Block, Michael Neuhaus and many others are sites I visit regularly. Without them, I wouldn’t have been able to automate what I have thus far. That said, this works great for my situation, and I know everyone has different environments and situations, but I hope that if anything, there is something I have used above that makes the “ding” go off and helps someone out.
Read more Patch Current WIM Files
Add comment