Configuring Hyper-V hosts using PowerShell

Introduction

Versions of Microsoft Hyper-V prior to Windows Server 2012 didn’t include a built-in Windows PowerShell module for managing Hyper-V hosts and virtual machines running on them. To remedy this lack, a PowerShell management library for Hyper-V was developed and hosted on CodePlex, Microsoft’s free open source project hosting site. This PowerShell library is called PSHyper-V and included about 80 different cmdlets administrators could use to manage Hyper-V on Windows Server 2008 R2. A separate version of this library was then later released for Windows Server 2008 R2 SP1.

Beginning with Windows Server 2012, Microsoft now includes a built-in PowerShell module for Hyper-V that provides over a hundred Windows PowerShell cmdlets that can be used to manage both Hyper-V hosts and virtual machines running on these hosts. Because of its flexibility and support for automation, Windows PowerShell is now the preferred in-box tool for managing Hyper-V hosts and virtual machines in large environments such as datacenters and cloud-computing environments. This article demonstrates some of the ways you can use PowerShell to manage Hyper-V hosts running Windows Server 2012 and later using the cmdlets available in the built-in PowerShell module. The explanation and procedures below have been adapted from my book Training Guide: Installing and Configuring Windows Server 2012 R2 (Microsoft Press, 2014) and contains additional information not provided in this article.

Configuring Hyper-V hosts

After you have installed the Hyper-V role on a server, you still need to configure the role to meet the needs of your environment. Configuring a Hyper-V host involves steps such as the following:

  • Configuring virtual machine storage
  • Creating virtual switches
  • Performing other configuration tasks

Hyper-V hosts can also be configured using either the Hyper-V Manager snap-in or Windows PowerShell cmdlets. To view the configuration of a host using the Hyper-V Manager snap-in, right-click on the host in the console tree and select Hyper-V Settings to display the Hyper-V Settings dialog box shown in the screen shots in this section. To view the configuration of a host using Windows PowerShell, use the Get-VMHost cmdlet as shown here:

PS C:\> Get-VMHost -ComputerName HOST4
 
Name  LogicalProcessorCount MemoryCapacity(M) VirtualMachineMigrationEnabled
----  --------------------- ----------------- ------------------------------
HOST4 4                     24570.2421875     False
To view all configuration settings associated with the specified host, pipe the output of the preceding command into the Format-List cmdlet and specify a wildcard:
PS C:\> Get-VMHost -ComputerName HOST4 | Format-List *
 
ComputerName                              : HOST4
VirtualHardDiskPath                       : C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks
VirtualMachinePath                        : C:\ProgramData\Microsoft\Windows\Hyper-V
FullyQualifiedDomainName                  : WORKGROUP
Name                                      : HOST4
MacAddressMinimum                         : 00155D0BE600
MacAddressMaximum                         : 00155D0BE6FF
MaximumStorageMigrations                  : 2
MaximumVirtualMachineMigrations           : 2
VirtualMachineMigrationEnabled            : False
VirtualMachineMigrationAuthenticationType : CredSSP
UseAnyNetworkForMigration                 : False
FibreChannelWwnn                          : C003FF0000FFFF00
FibreChannelWwpnMaximum                   : C003FF68F816FFFF
FibreChannelWwpnMinimum                   : C003FF68F8160000
LogicalProcessorCount                     : 4
MemoryCapacity                            : 25763766272
ResourceMeteringSaveInterval              : 01:00:00
NumaSpanningEnabled                       : True
HostNumaStatus                            : {HOST4}
NumaStatus                                :
InternalNetworkAdapters                   : {CONTOSO Virtual Switch}
ExternalNetworkAdapters                   : {CONTOSO Virtual Switch_External}
IovSupport                                : False
IovSupportReasons                         : {Ensure that the system has chipset...

Note that the configuration of virtual switches on a host is not included in the output of the Get-VMHost cmdlet. To view a list of virtual switches that have been created on the host, use the Get-VMSwitch cmdlet as shown here:

PS C:\> Get-VMSwitch
 
Name                   SwitchType NetAdapterInterfaceDescription
----                   ---------- ------------------------------
CONTOSO Virtual Switch External   Broadcom NetXtreme Gigabit Ethernet #2

To view all of the properties of all virtual switches on the host, use the following command:

PS C:\> Get-VMSwitch | Format-List *
 
ComputerName                        : HOST4
Name                                : CONTOSO Virtual Switch
Id                                  : cffd5106-2735-4c54-b9e1-1cad8944e8c0
Notes                               :
SwitchType                          : External
AllowManagementOS                   : True
NetAdapterInterfaceDescription      : Broadcom NetXtreme Gigabit Ethernet #2
AvailableVMQueues                   : 0
NumberVmqAllocated                  : 0
IovEnabled                          : False
IovVirtualFunctionCount             : 0
IovVirtualFunctionsInUse            : 0
IovQueuePairCount                   : 0
IovQueuePairsInUse                  : 0
AvailableIPSecSA                    : 0
NumberIPSecSAAllocated              : 0
BandwidthPercentage                 : 10
BandwidthReservationMode            : Absolute
DefaultFlowMinimumBandwidthAbsolute : 10000000
DefaultFlowMinimumBandwidthWeight   : 0
Extensions                          : {Microsoft NDIS Capture, Microsoft Windows Filtering Platform}
IovSupport                          : False
IovSupportReasons                   : {Ensure that the system has chipset support...
IsDeleted                           : False

Configuring virtual machine storage

Figure 1 shows the default location where a Hyper-V host stores the virtual hard disk files for its virtual machines. Such files might include the following:

  • VHD or VHDX, which can be the system drive or data drives for each virtual machine
  • AVHD files, which are the differencing disk files used for virtual machine snapshots

As part of the post-installation configuration of your Hyper-V hosts, you should change this location to the volume where you want the virtual hard disk files stored. This volume will depend on the type of storage solution that you have decided to use for your Hyper-V hosts.

Image
Figure 1: Configuring the location where virtual hard disk files will be stored.

You can also use the Set-VMHost cmdlet to configure the virtual hard disk location using Windows PowerShell. For example, you can use the following command to change the virtual hard disk location on HOST4 to the E:\VM Storage folder:

Set-VMHost -ComputerName HOST4 -VirtualHardDiskPath “E:\VM Storage”

Figure 2 shows the default location where a Hyper-V host stores its virtual machine configuration files. Such files can include the following:

  • XML files named with the globally unique identifier (GUID) used to internally identify a virtual machine or snapshot
  • BIN files containing the memory of a virtual machine or snapshot
  • VSV files containing the saved state from the devices associated with a virtual machine

Image
Figure 2: Configuring the location where virtual machine configuration files will be stored.

You can also use the Set-VMHost cmdlet to configure the virtual machine configuration files location using Windows PowerShell. For example, you can use the following command to change the virtual machine configuration files location on HOST4 to the E:\VM Configurations folder:

Set-VMHost -ComputerName HOST4 -VirtualMachinePath “E:\VM Configurations”

Creating virtual switches

Figure 3 shows the New Virtual Network Switch page of the Virtual Switch Manager dialog box for a Hyper-V host. You can use this page to create virtual switches of the external, internal, and private types. 

Image
Figure 3: Creating a new virtual switch in Hyper-V Manager.

Clicking Create Virtual Switch, shown in Figure 3, creates a new virtual switch of the type selected (here, External), and the properties of the new virtual switch can now be configured as shown in Figure 4. The key properties to configure are these:

  • Specifying a descriptive name for the new switch
  • Selecting the connection type (External, Internal, or Private) if you decide to change your initial choice

If you select External as the connection type, you must assign a physical network adapter on the host to the new switch. In that case, you also have the option of allowing the management operating system on the host to share the selected network adapter. If you are using a separate physical network for host management, and the host has another physical network adapter connected to the management network, you can clear the check box shown as selected in Figure 3. Additionally, you also have the option of enabling single-root I/O virtualization (SR-IOV) if the selected network adapter supports this feature. SR-IOV is described later in this chapter. Finally, if you are using virtual local area networks (VLANs) to segment traffic on your production network, you can enable this feature and specify a VLAN identifier here.

Image
Figure 4: Configuring the new virtual switch.

Note that creating a new virtual switch of type External also creates a new network connection in the Network Connections folder on the server, which can be displayed using the Network And Internet utility in Control Panel. The new network connection is typically named “vEthernet (<name of virtual switch>), and the TCP/IP settings assigned to the physical network adapter associated with the switch become the TCP/IP settings of the new connection.

You can also use the New-VMSwitch cmdlet to create new virtual switches using Windows PowerShell. For example, the following command could be used to create the CONTOSO Virtual Switch shown in Figure 7-4:

New-VMSwitch -Name “CONTOSO Virtual Switch” `

-NetAdapterName “Broadcom NetXtreme Gigabit Ethernet #2” -ComputerName HOST4

For help with the syntax of this cmdlet, use the Get-Help New-VMSwitch command.

In addition to creating and configuring virtual switches on your hosts, you can also configure the range of media access control (MAC) addresses that the host can use to dynamically assign MAC addresses to the virtual network adapters of virtual machines on the host. Figure 5 shows the default range of MAC addresses used for this purpose. Although this default range might suffice when only a few hosts are being deployed, it’s generally a good idea to assign a different MAC address range to each host to ensure that duplicate MAC addresses cannot be assigned to virtual machines on different hosts, which could cause network problems for the guest operating systems of such virtual machines.

Image
Figure 5: Configuring the range of MAC addresses that the host can assign to virtual network adapters.

You can also use the Set-VMHost cmdlet to configure the range of MAC addresses that the host can assign to virtual network adapters of virtual machines on the host. For example, you can use the following command to change the MAC address range on HOST4 to span from 00-00-04-00-00-00 to 00-00-04-FF-FF-FF:

Set-VMHost -ComputerName HOST4 -MacAddressMinimum 000004000000 `

-MacAddressMaximum 000004FFFFFF

Other configuration steps

Additional steps needed for configuring your Hyper-V hosts depend on what types of functionality you want to enable on them. For example, by using the Hyper-V Settings dialog box, you can:

  • Enable live migrations on the host, and configure the authentication protocol used for live migration, how many live migrations can be performed simultaneously, and which network or IP address can be used for incoming live migrations.
  • Configure how many simultaneous storage migrations can be performed on the host.
  • Enable Hyper-V Replica functionality on the host and the authentication protocol and ports used for replication, and whether replication will be allowed from any authenticated server or from a list of specified servers.
  • Enable NUMA spanning, a new feature of Hyper-V in Windows Server 2012 that allows virtual machines to span non-uniform memory architecture (NUMA) nodes on NUMA-capable hosts. NUMA spanning works by projecting a virtual NUMA topology to the guest operating system in a way that is optimized to match the NUMA topology of the underlying physical host machine. The effect is to provide virtual machines with additional computing resources for high-performance server applications such as Microsoft SQL Server and modern server operating systems such as Windows Server 2012 that include built-in NUMA optimizations.

You can also use Windows PowerShell to perform these additional configuration steps on your hosts. For example, you can use the Set-VMHost cmdlet to enable live migration, configure it to use CredSSP for authentication, and configure it to allow incoming live migrations using any available network as follows:

Set-VMHost -VirtualMachineMigrationEnabled $true `

-VirtualMachineMigrationAuthenticationType CredSSp -UseAnyNetworkForMigration $true

Additional Resources

For information about the syntax of the Hyper-V cmdlets and for usage examples, see “Hyper-V Cmdlets in Windows PowerShell

1 thought on “Configuring Hyper-V hosts using PowerShell”

  1. Hi there,

    Sorry if this is off-topic, but…

    I’m having trouble resetting (Hyper-V Manager > Hyper-V Settings > Reset Check Boxes > Reset).

    I know that there’s a cmdlet to do it through Power Shell, but I’m not being able to find it. Any chance you’d know it?

    Thanks!

Leave a Comment

Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top