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:

  1. mount-vhd -path $updateTarget

With:

  1. if (Get-WmiObject win32_OperatingSystem -Filter "Version > '6.2'") #if Windows 2012
  2. {
  3. mount-vhd -path $updateTarget
  4. }
  5. else #Windows 2008 R2 have to mount another way
  6. {
  7. $VHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "root\virtualization" -computername "."
  8. $VHDService.Mount($updateTarget)
  9. }

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

  1. if (Get-WmiObject win32_OperatingSystem -Filter "Version > '6.2'") #if Windows 2012
  2. {
  3. dismount-vhd -path $updateTarget -confirm:$false
  4. }
  5. else
  6. {
  7. $VHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "root\virtualization" -computername "."
  8. $VHDService.Unmount($updateTarget)
  9. }

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.