Offline VHD or WIM Patching from Windows 7 Update

How to update my PowerShell script to work from Windows Server 2008 R2 and what is required for it to work from Windows 7.

John Savill

May 11, 2013

1 Min Read
Offline VHD or WIM Patching from Windows 7 Update

Q: I'm trying to use your script to offline patch a VHD file, but it's not working on Windows 7 or Windows 2008 R2--why?

A. The script I created which is available at Windows IT Pro's "Add Updates to an Offline VHD or WIM File" was written specifically for Windows 8 and Windows Server 2012. It uses the Mount-VHD and Dismount-VHD cmdlets, which aren't available in Windows 7 and Windows Server 2008 R2, so the script would fail.

For a Windows Server 2008 R2 box that has the Hyper-V role installed, an alternate approach for mounting and dismounting the virtual hard disk (VHD) would be to use the Msvm_ImageManagementService. For example, I would replace the line in my original script:

mount-vhd -path $updateTarget

With:

if (Get-WmiObject win32_OperatingSystem -Filter "Version > '6.2'") #if Windows 2012{    mount-vhd -path $updateTarget}else #Windows 2008 R2 have to mount another way{    $VHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "rootvirtualization" -computername "."    $VHDService.Mount($updateTarget)}

Likewise you would need to replace the dismount code to be this:

if (Get-WmiObject win32_OperatingSystem -Filter "Version > '6.2'") #if Windows 2012{    dismount-vhd -path $updateTarget -confirm:$false}else{    $VHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "rootvirtualization" -computername "."    $VHDService.Unmount($updateTarget)}

If you want to be able to run on Windows 7 or Windows 2008 R2 without the Hyper-V role, you would need to leverage diskpart, and basically dynamically create a short script to select the vdisk of the VHD, then attach it.

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like