Search
StarWind is a hyperconverged (HCI) vendor with focus on Enterprise ROBO, SMB & Edge

Automate the Hyper-V Virtual Machine deployment with PowerShell

  • February 17, 2016
  • 23 min read
IT and Virtualization Consultant. Romain is specializing in Microsoft technologies such as Hyper-V, System Center, storage, networking, and MS Azure. He is a Microsoft MVP and MCSE in Server Infrastructure and Private Cloud.
IT and Virtualization Consultant. Romain is specializing in Microsoft technologies such as Hyper-V, System Center, storage, networking, and MS Azure. He is a Microsoft MVP and MCSE in Server Infrastructure and Private Cloud.

For large Hyper-V infrastructure, IT people use often Virtual Machine Manager (VMM) to automate tasks as VM deployment. VMM provides profiles (hardware, operating system and application) and then we use these profiles to build a VM Template. Most of the time, a VM Template contains a VM hardware configuration and the OS configuration (join the domain, product key and so on). Then VMs are deployed from templates which decrease the time to deploy a working VM.

The convenient about VMM is its price. VMM is expensive and it is often medium/large business which implement VMM. So for other companies which can’t buy VMM, are they forced to do without VM deployment automation? Of course no. Other tools can help us to make automation without VMM and it is called PowerShell.

Since Windows Server 2012, a lot of Hyper-V PowerShell commands (in Hyper-V module) have been added. Now easily we can create, configure and manage VM from PowerShell.


PowerShell command
For large Hyper-V infrastructure, IT people use often Virtual Machine Manager (VMM) to automate tasks as VM deployment. VMM provides profiles (hardware, operating system and application) and then we use these profiles to build a VM Template. Most of the time, a VM Template contains a VM hardware configuration and the OS configuration (join the domain, product key and so on). Then VMs are deployed from templates which decrease the time to deploy a working VM.

The convenient about VMM is its price. VMM is expensive and it is often medium/large business which implement VMM. So for other companies which can’t buy VMM, are they forced to do without VM deployment automation? Of course no. Other tools can help us to make automation without VMM and it is called PowerShell.

Since Windows Server 2012, a lot of Hyper-V PowerShell commands (in Hyper-V module) have been added. Now easily we can create, configure and manage VM from PowerShell.

In this topic we will see how to create and set a Virtual Machine from PowerShell. The scripts presented are examples. They can be changed, modified and improved regarding your needs. They are here for explanation.

To follow this topic, I assume that you have knowledge about Hyper-V and PowerShell.

Create a VM

To create a Virtual Machine from PowerShell, you can run the New-VM PowerShell cmdlet as below:

  • Name: Name of the Virtual Machine
  • Path: Path where will be stored VM files
  • NewVHDPath: Create a VHD(X) file to the specified path (Dynamic disk)
  • New-VHDSizeBytes: Size of the VHD(X) file
  • Generation: VM generation (1 or 2)
  • MemoryStartupBytes: Memory assigned to the VM (static Memory)
  • SwitchName: switch name where the network adapter will be connected

Below you can find an example of VM creation with the above command:

1

If you don’t want to attach now a VHD(x) with your VM because you have a syspreped VHDX, you can run the below command:

Configure a VM

To configure deeply a VM, you can use the Set-VM PowerShell cmdlet. Below an example:

  • Name: Name of the VM you would like to edit
  • ProcessorCount: number of vCPU that you want to assign to the VM
  • DynamicMemory: Enable the Dynamic Memory
  • MemoryMinimumBytes: set the minimum memory value
  • MemoryStartupBytes: set the startup memory value
  • MemoryMaximumBytes: set the maximum memory value
  • AutomaticStartAction: action which is run when the Hyper-V service is starting (Nothing, Start, StartIfRunning)
  • AutomaticStartDelay: number of second to wait before the automatic start action is run
  • AutomaticStopAction: action which is run when the Hyper-V service is stopping (Save, Shutdown, TurnOff)

Below you can find an example of VM configuration with the above command:

2

3

If you want to configure the VM with static memory you can use the below command:

Manage VM Storage

Add the OS disk and set the primary boot

If you have run the below command to create a VM without VHD(X), maybe you would like to attach now your syspreped VHD(X):

To attach the VHD(x) to the VM, you have just to run Add-VMHardDiskDrive PowerShell command:

  • VMName: Name of the related VM
  • Path: Path to the VHD(X) file

Below you can find an example of attaching a VHD(X) file to a VM:

4

5

Now you can set this disk to primary boot by using the Set-VMFirmware PowerShell cmdlet:

  • VMName: VM of the related VM

Below you can find an example of the using of the above command:

6

7

Adding additional VHD(X)

To create a new VHD(X) you can use the New-VHD cmdlet as below:

  • Path: absolute path to the VHD(X) file
  • SizeBytes: Size of the VHD(X)
  • Dynamic (can be replace by fixed): Dynamic or fixed VHD(X)

Once you have created the disk, you can attach it by using Add-VMHardDiskDrive as below:

Manage network adapters

Add network adapters

To add a network adapter, you can run the below command:

  • VMName: name of the VM
  • SwitchName: Name of the VM Switch to connect to
  • Name: Name of the network adapter

Below you can find an example of the above command:

8

9

Configure network adapters

There are a lot of options in network adapters configuration. You can configure all of them by using PowerShell but I present only some of them. You can find the help of these PowerShell cmdlet in TechNet. To configure the Network Adapter, you can run the below command:

  • MacAddressSpoofing: Enable or disable Mac Spoofing
  • DHCPGuard: Enable or disable DHCP Guard
  • RouterGuard: Enable or disable router guard

Below you can find an example of the use of this PowerShell cmdlet:

10

11

Set the VLAN

To set the VLAN on a network adapter, you can use the Set-VMNetworkAdapterVLAN PowerShell cmdlet:

  • Access: Set the Access mode on the network adapter
  • VlanId: set the VLAN ID value

To leave untagged the network adapter, you can run the below command:

Below you can find an example of this PowerShell command:

12

13

Script to automatically deploy a VM

Below you can find a script that I have written to deploy several VMs from the same configuration. This script is not perfect and can be improved by adding try/catch. You can also modify parameters to be passed in command line as arguments. In this way you will be able to make a loop to deploy quickly several VMs. To use this script you need a sysprep VHD(X).

Conclusion

If you can’t buy VMM and you want to deploy automatically VM, it is still possible by using PowerShell. All examples presented are not exhaustive. There are several other combinations possible. All cmdlet documentation can be found in the TechNet.

Hey! Found Romain’s insights useful? Looking for a cost-effective, high-performance, and easy-to-use hyperconverged platform?
Taras Shved
Taras Shved StarWind HCI Appliance Product Manager
Look no further! StarWind HCI Appliance (HCA) is a plug-and-play solution that combines compute, storage, networking, and virtualization software into a single easy-to-use hyperconverged platform. It's designed to significantly trim your IT costs and save valuable time. Interested in learning more? Book your StarWind HCA demo now to see it in action!