The possibility of Servicing Offline a Virtual Hard Disk Image or a Windows Image is not something new. Actually, it’s a very handy way of adding or removing local packages, updates, drivers and Windows Features with minimal effort to your image. My story begins with my home lab, which I wanted to quickly install Updates and packages such as WMF 5.1 without using Windows Update to my Windows Virtual Machines that I use as Templates for Testing.
For that reason, I manage to compile the following PowerShell Script which will allow you to service offline a Windows .VHD/.VHDX virtual hard disk image. Grab the code below, save it to a PowerShell script and execute it directly from the Target Machine which the virtual hard disk image resides.
Some hints prior firing up the script
- The Mount Folder must exist on the System, the script does not create the folder automatically.
- The script will search all the underlying folders for packages in the folder that will be specified during the prompted questions.
- It can also service a Windows Image File (.WIM) but with no option to select different indexes. For this operation, the script needs to be altered.
# AUTHOR: George Markou
# DATE : November 2017
# Version: 1.0
# COMMENT: Script to automate the servicing of a .VHD / .VHDX Windows 7/ Server 2008 R2 or greater offline Virtual Disk Image.
# This script sample is provided "AS IS" with no warranties, confers no rights and
# is not supported by the authors or Microsoft Corporation.
$VHDPAth = Read-Host -Prompt "Enter the path of the Virtual Disk File"
$MountPath = Read-Host -Prompt "Enter the path for mounting the Virtual Disk File"
$updatePath = Read-Host -Prompt "Enter Base Folder containing downloaded Windows Updates or Packages"
$windowsUpdates = Get-ChildItem -Path $updatePath -Include *.msu,*.cab -Recurse -Force | Select-Object -Property FullName
DISM /Mount-image /imagefile:$VHDPath /index:1 /Mountdir:$MountPath
DISM.exe /image:$MountPath /LogPath:PackageInstallation.log /LogLevel:1 /Add-Package /PackagePath:$updatePath
DISM /Unmount-image /Mountdir:$MountPath /Commit
- In order to complete the installation of the packages, you need to boot the system that is using this Virtual Hard Disk File.
- Packages that have dependencies other packages, will be skipped and not installed.
- A log file is produced after execution. Can be found in the same folder were the Script Resides.
- The script is intended to be used only for testing purposes and not for production servers.
The script has been tested and reported to be working with the following OS versions.
- Windows 10 for desktop editions (Home, Pro, Enterprise, and Education)
- Windows Server 2016
- Windows 8.1
- Windows 8
- Windows Server 2012 R2
- Windows Server 2012
- Windows 7
- Windows Server 2008 R2
- Windows Server 2008 SP2
Thanks for reading!