If you have to create several identical VMs that deviate significantly from the wizard's defaults in the Hyper-V Manager, it results in a lot of manual work. Alternatively, PowerShell offers cmdlets for this purpose that can add and configure virtual hardware, such as network adapters or VHDs.

The requirement to create several identical VMs arises, for example, when you want to combine virtual instances of Windows Server to form a cluster. A typical application for this is setting up a lab environment. The following instructions show the procedure using the example of Azure Stack HCI to evaluate it on a single machine.

In principle, you could simply configure a VM manually in such a case and then clone it. The Hyper-V Manager does not have an explicit function for this task, but for some time now, the Windows Admin Center has been able to do this. New VMs, automatically created by PowerShell, are probably the cleaner solution, though.

Creating and customizing a VM ^

The first step is to create the virtual machine. The command depends on whether the VHD for it already exists. If it does, then you tell New-VM the path to the virtual drive using the VHDPath parameter.

We are assuming that no VHD exists yet and that we will only install it afterwards:

After the VM ASHCI03 has been created, you can configure it using Set-VM. The following command sets the number of vCPUs and the available vRAM. It also disables snapshots, including the automatic checkpoints common in Windows 10. Since Azure Stack HCI uses nested virtualization, we don't activate Dynamic Memory.

Now it is time to configure the vNICs. When the VM is created, it automatically receives a network adapter. Its name can be saved in the variable $NicName, using

We need the adapter's default name because we want to name all NICs according to a consistent convention. This command gives it the new name vNIC01:

Creating and connecting network adapters ^

Microsoft's recommendation for Azure Stack HCI includes four vNICs for the virtual machine. The missing three can be created as follows:

These should be bound to an external-type vSwitch.

All steps start from creating and customizing the VM to adding the NICs and creating the VHDs

All steps start from creating and customizing the VM to adding the NICs and creating the VHDs

The following command connects all four vNICs to a virtual switch, which in our example is called External:

Since we need to enable the hypervisor in Azure Stack HCI, you need to allow spoofing of Mac addresses for nested virtualization:

At this point, you can create another necessary prerequisite, namely, to make the virtualization extensions of the CPU visible in the VM:

Creating and attaching drives ^

In addition to the system volume, Azure Stack HCI requires at least two more drives for Storage Spaces Direct. They are created with the New-VHD cmdlet, named ASHCI03-0[1,2,3].vhdx:

You then link the three VHDX to the virtual machine with this command:

Assigning new VHDs to the virtual machine inserting ISO into the virtual DVD drive and enabling nested virtualization

Assigning new VHDs to the virtual machine inserting ISO into the virtual DVD drive and enabling nested virtualization

Finally, assign the installation media to the VM using a virtual DVD drive:

The inside view in the Azure Stack HCI VM on the virtual network adapters and drives

The inside view in the Azure Stack HCI VM on the virtual network adapters and drives

For the VM to boot from the DVD drive, the boot order must be changed accordingly:

The new VM is now ready for the installation of the operating system. If Hyper-V cannot be enabled in Azure Stack HCI, you can enable the feature offline in VHDX.

Consolidating all commands in a function ^

For more efficient use, you can combine the above instructions into one function, specifying the name of the new VM as a parameter.

Subscribe to 4sysops newsletter!

Using the generateVM function to create a new virtual machine

Using the generateVM function to create a new virtual machine

If necessary, you can also set a variable for the paths used. Specify the paths as additional parameters when you execute the function.

+2
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