If you set up Windows on a virtual machine, you do not have to activate roles and features in the live system with the Server Manager. Alternatively, you can mount the VHDX for offline servicing with DISM or PowerShell. This will speed up the provisioning of VMs.

DISM and the cmdlets of the namesake PowerShell module are not only used for processing WIM archives but have also been extended for servicing VHDs for some time. This capability can be used to automate the configuration of the guest operating system.

This is especially true if you previously installed Windows on a VHD without user interaction using Wim2VHD. Then, instead of attaching the virtual drive to a VM and booting it up, you can add the features you need right away offline.

The steps required for this, namely mounting the VHD, activating the features, unmounting the image, and writing the changes, can easily be packed into a script and processed in one go.

Mounting VHD ^

If you decide to use DISM, mount the VHD according to the following pattern:

dism /mount-image /imageFile:"F:\myvm.vhdx" /MountDir:F:\mnt /index:1
Mount virtual drive for offline servicing with DISM

Mount virtual drive for offline servicing with DISM

The parameter index must always have a value of 1 for VHD(X)s. The equivalent command in PowerShell looks like this:

Mount-WindowsImage -Path f:\mnt -ImagePath "F:\myvm.vhdx" -Index 1
Mounting virtual drive for offline servicing with PowerShell

Mounting virtual drive for offline servicing with PowerShell

Displaying and activating features ^

Next, you can display the list of Windows features to determine their exact names for activation.

Dism /Image:F:\mnt /Get-Features

If you want to display only a specific option from the long list of features, pipe the output to findstr.exe.

With PowerShell, on the other hand, you can specify the name right away. Wildcards are also allowed:

Get-WindowsOptionalFeature -Path f:\mnt -FeatureName Hyper*

Then, activate the required feature in this way:

Dism /Image:F:\mnt /enable-Feature:Microsoft-Hyper-V /ALL

This command would install Hyper-V.

Installing Hyper V using DISM in an offline VHD

Installing Hyper V using DISM in an offline VHD

The PowerShell version looks like this:

Enable-WindowsOptionalFeature -Path f:\mnt -FeatureName Microsoft-Hyper-V
View and enable features with PowerShell subsequently unmount VHD and save changes

View and enable features with PowerShell subsequently unmount VHD and save changes

This method of installing Hyper-V using nested virtualization can prove useful for a lab running Azure Stack HCI. If you want to activate the hypervisor in the online host VM, it might fail due to the prerequisite check, even though the prerequisites are objectively met. However, if you add it offline, there is a good chance that Hyper-V will work in the VM afterward.

Unmount VHD and save changes ^

Finally, you need only unmount the VHDX and save the changes. To do so, do one of the following:

Enter:

dism /unmount-image /mountDir:F:\mnt /commit

or in PowerShell:

Subscribe to 4sysops newsletter!

Dismount-WindowsImage -Path f:\mnt -Save

The virtual drive can now be added to a VM if it has not already been added.

+3
avatar
0 Comments

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account