11 June 2013

APPLYING WINDOWS UPDATE MSU FILES TO WIM IMAGES

This powershell script will inject all .msu files into a mounted WIM file. The script first scans the relative path of the directory that it was executed from for WIM files. Next, it mounts the WIM file. The script then scans the relative directory for all existing .msu files and writes them to an array. The msu files are then injected into the mounted WIM file. Once all updates are injected, the WIM is unmounted and cleaned up.

To use this, I suggest creating a separate directory and copying the script to it. Copy all of the pertinant .msu files and the WIM file over.

NOTE: Make sure to backup the WIM file before injecting the .msu files into it. Some updates are only online updates and will corrupt the WIM file if injected. You can read more on this topic in my blog located here.

You can download the script here.

 Clear-Host  
 $Global:MountPath  
 $Global:RelativePath  
 $Global:WimFile  
 $Global:UpdatesPath  
 Function GetRelativePath{  
      $Global:RelativePath=(split-path $SCRIPT:MyInvocation.MyCommand.Path -parent)+"\"  
      Write-Host $Global:RelativePath  
 }  
 Function GetWimFile{  
      $FileName = Get-ChildItem $Global:RelativePath | Where-Object {($_.Name -like "*.wim")}  
      $Global:WimFile = $Global:RelativePath+$FileName  
      Write-Host $Global:WimFile  
 }  
 Function MountWIM{  
      $Global:MountPath = $Global:RelativePath+"Mount\"  
      If ((Test-Path $Global:MountPath) -ne $true){  
           New-Item -ItemType directory -Path $Global:RelativePath"Mount"  
      }  
      Write-Host $Global:MountPath  
      $Arguments = "dism.exe /mount-wim /wimfile:"+$Global:WimFile+[char]32+"/index:1 /mountdir:"+$Global:MountPath  
      Write-Host $Arguments  
      Invoke-Expression -Command $Arguments  
 }  
 Function UnmountWIM{  
      $Arguments = "dism.exe /unmount-wim /mountdir:"+$Global:MountPath+[char]32+"/commit"  
      Write-Host $Arguments  
      Invoke-Expression -Command $Arguments  
 }  
 Function CleanupWIM{  
      $Arguments = "dism.exe /cleanup-wim"  
      Write-Host $Arguments  
      Invoke-Expression -Command $Arguments  
 }  
 Function GlobalMemoryCleanup{  
      Clear-Variable -Name MountPath -Scope Global -Force  
      Clear-Variable -Name WimFile -Scope Global -Force  
      Clear-Variable -Name UpdatesPath -Scope Global -Force  
      Clear-Variable -Name RelativePath -Scope Global -Force  
      Remove-Variable -Name MountPath -Scope Global -Force  
      Remove-Variable -Name WimFile -Scope Global -Force  
      Remove-Variable -Name UpdatesPath -Scope Global -Force  
      Remove-Variable -Name RelativePath -Scope Global -Force  
 }  
 GetRelativePath  
 GetWimFile  
 MountWIM  
 $Global:UpdatesPath = $Global:RelativePath+"*.msu"  
 $UpdatesArray = Get-Item $Global:UpdatesPath  
 ForEach ($Updates in $UpdatesArray) {  
      $Arguments = "dism.exe /image:"+$Global:MountPath+[char]32+"/Add-Package /PackagePath:"+$Updates  
      Write-Host $Arguments  
      Invoke-Expression -Command $Arguments  
      Start-Sleep -Seconds 10  
 }  
 UnmountWIM  
 CleanupWIM  
 Clear-Variable -Name Arguments -Scope Local -Force  
 Clear-Variable -Name Updates -Scope Local -Force  
 Clear-Variable -Name UpdatesArray -Scope Local -Force  
 Remove-Variable -Name Arguments -Scope Local -Force  
 Remove-Variable -Name Updates -Scope Local -Force  
 Remove-Variable -Name UpdatesArray -Scope Local -Force  
 GlobalMemoryCleanup  

0 comments:

Post a Comment