7 Powerful Scripts for Practical Hyper-V Network Configurations

I firmly believe in empowerment. I feel that I should supply you with knowledge, provide you with how-tos, share insights and experiences, and release you into the world to make your own decisions. However, I came to that approach by standing at the front of a classroom. During class, we’d almost invariably walk through exercises. Since this is a blog and not a classroom, I do things differently. We don’t have common hardware in a controlled environment, so I typically forgo the exercises bit. As a result, that leaves a lot of my readers at the edge of a cliff with no bridge to carry them from theory to practice. And, of course, there are those of you that would love to spend time reading about concepts but just really need to get something done right now. If you’re stopped at Hyper-V networking, this is the article for you.

Script Inventory

These scripts are included in this article:

Basic Usage

I’m going to show each item as a stand-alone script. First, you’ll locate the one that best aligns with what you’re trying to accomplish. You’ll copy/paste that into a .ps1 PowerShell script file on your system. You’ll need to edit the script to provide information about your environment so that it will work for you. I’ll have you set each of those items at the beginning of the script. Then, you’ll just need to execute the script on your host.

Most scripts will have its own “basic usage” heading that explains a bit about how you’d use it without modification.

Enhanced Usage

I could easily compile these into standalone executables that you couldn’t tinker with. Even though I want to give you a fully prepared springboard, I also want you to learn how the system works and what you’re doing to it.

Most scripts will have its own “enhanced usage” heading that gives some ideas how you might exploit or extend it yourself.

Configure networking for a single host with a single adapter

Use this script for a standalone system that only has one physical adapter. It will:

Basic Usage for this Script

You just need to enter the necessary information for these items and execute it.

Advanced Usage for this Script

As-is, this script should be complete for most typical single-adapter systems. You might choose to disable some items. For instance, if you are using this on Windows 10, you might not want to provide a fixed IP address. In that case, just put a # sign at the beginning of lines 42 onward. When the virtual network adapter is created, it will remain in DHCP mode.

[CmdletBinding()]param()
#requires -Modules Hyper-V, NetAdapter

## User Modifications Start Here ##
$VirtualSwitchName = 'vSwitch'

# use Get-NetAdapter to locate name for the following item
$Net1AdapterName = 'Ethernet'

$ManagementIP = '192.168.25.10'
$ManagementSubnet = '255.255.255.0'
$ManagementGateway = '192.168.25.1'
$ManagementVLAN = 0 # leave at 0 if no VLAN

$DNSServer1 = '192.168.25.5'
$DNSServer2 = '' # leave blank if no secondary DNS

## User Modifications End Here ##

$Adapter1 = Get-NetAdapter -Name $Net1AdapterName

Write-Verbose -Message 'Disabling VMQ'
$VMQProperties = Get-NetAdapterAdvancedProperty -Name $Adapter1.Name | where DisplayName -Match 'V.*M.*Q'
$VMQProperties | foreach {
    if ($_.RegistryValue -eq 1) { Set-NetAdapterAdvancedProperty -InputObject $_ -RegistryValue 0 }
}

Write-Verbose -Message 'Creating virtual switch'
$VMSwitch = New-VMSwitch -Name $VirtualSwitchName -NetAdapterName $TeamName -AllowManagementOS $false -MinimumBandwidthMode Weight

Write-Verbose -Message 'Creating virtual management adapter'
Add-VMNetworkAdapter -ManagementOS -Name 'Management' -SwitchName $VirtualSwitchName
if ($ManagementVLAN -gt 0)
{
	Write-Verbose -Message 'Setting management adapter VLAN'
	Set-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName 'Management' -Access -VlanId $ManagementVLAN
}

Write-Verbose -Message 'Setting management adapter IP information'
#netsh always works; New-NetIPAddress will fail if it thinks the adapter is disconnected
netsh interface ip set address 'vEthernet (Management)' static $ManagementIP $ManagementSubnet $ManagementGateway 1
$DNSServers = New-Object System.Collections.ArrayList
$DNSServers += $DNSServer1
if ($DNSServer2) { $DNSServers += $DNSServer2 }
Set-DnsClientServerAddress -InterfaceAlias 'vEthernet (Management)' -ServerAddresses $DNSServers

 

Configure a standalone host with 2-4 gigabit adapters for converged networking

Use this script for a standalone host that has between two and four gigabit adapters that you want to use in a converged networking configuration. It will:

  • Create a team on the adapters
  • Disable VMQ for the physical adapters and the teamed adapter
  • Create a virtual switch on the team
  • Create a virtual network adapter for the management operating system to use
  • Optionally place the management adapter into a VLAN
  • Assign an IP, subnet, and gateway to the management adapter
  • Specify one or two DNS servers

Basic Usage for this Script

You just need to enter the necessary information for these items and execute it. Be aware that it will have problems if you already have a team.

Advanced Usage for this Script

This script serves as the base for the remaining scripts on this page. Likewise, you could use it as a base for your own. You could also use any of the items as examples for whatever similar actions you wish to accomplish in your own scripts.

[CmdletBinding()]param()
#requires -Modules Hyper-V, NetAdapter, NetLbfo

## User Modifications Start Here ##
$VirtualSwitchName = 'vSwitch'
$TeamName = 'vSwitch'

# use Get-NetAdapter to locate names for the following items
$Net1AdapterName = 'Ethernet'
$Net2AdapterName = 'Ethernet 2'
$Net3AdapterName = '' # leave blank if no third adapter
$Net4AdapterName = '' # leave blank if no fourth adapter

$ManagementIP = '192.168.25.10'
$ManagementSubnet = '255.255.255.0'
$ManagementGateway = '192.168.25.1'
$ManagementVLAN = 0 # leave at 0 if no VLAN

$DNSServer1 = '192.168.25.5'
$DNSServer2 = '' # leave blank if no secondary DNS

## User Modifications End Here ##

$Adapter1 = Get-NetAdapter -Name $Net1AdapterName
$Adapter2 = Get-NetAdapter -Name $Net2AdapterName
$Adapter3 = Get-NetAdapter -Name $Net3AdapterName
$Adapter4 = Get-NetAdapter -Name $Net4AdapterName

Write-Verbose -Message 'Creating team'
$Team = New-NetLbfoTeam -Name $VirtualSwitchName -TeamMembers $Adapter1.Name, $Adapter2.Name -TeamingMode SwitchIndependent -LoadBalancingAlgorithm Dynamic -Confirm:$false
$Adapter3, $Adapter4 | foreach {
	if ($_ -ne $null) { $AddedAdapter = Add-NetLbfoTeamMember -Team $TeamName -Name $_.Name -Confirm:$false }
}

$TeamAdapter = Get-NetAdapter 'vSwitch'

Write-Verbose -Message 'Disabling VMQ'
$TeamAdapter, $Adapter1, $Adapter2, $Adapter3, $Adapter4 | foreach {
	if ($_)
 {
		$VMQProperties = Get-NetAdapterAdvancedProperty -Name $_.Name | where DisplayName -Match 'V.*M.*Q'
		if ($VMQProperties)
		{
			$VMQProperties | foreach {
				if ($_.RegistryValue -eq 1) { Set-NetAdapterAdvancedProperty -InputObject $_ -RegistryValue 0 }
			}
		}
	}
}

Write-Verbose -Message 'Creating virtual switch'
$VMSwitch = New-VMSwitch -Name $VirtualSwitchName -NetAdapterName $TeamName -AllowManagementOS $false -MinimumBandwidthMode Weight

Write-Verbose -Message 'Creating virtual management adapter'
Add-VMNetworkAdapter -ManagementOS -Name 'Management' -SwitchName $VirtualSwitchName
if ($ManagementVLAN -gt 0)
{
	Write-Verbose -Message 'Setting management adapter VLAN'
	Set-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName 'Management' -Access -VlanId $ManagementVLAN
}

Write-Verbose -Message 'Setting management adapter IP information'
netsh interface ip set address 'vEthernet (Management)' static $ManagementIP $ManagementSubnet $ManagementGateway 1 #netsh always works; New-NetIPAddress will fail if it thinks the adapter is disconnected
$DNSServers = New-Object System.Collections.ArrayList
$DNSServers += $DNSServer1
if ($DNSServer2) { $DNSServers += $DNSServer2 }
Set-DnsClientServerAddress -InterfaceAlias 'vEthernet (Management)' -ServerAddresses $DNSServers

 

Configure a standalone host with 2-4 10 GbE adapters for converged networking

Use this script for a standalone host that has between two and four 10GbE adapters that you want to use in a converged networking configuration. It will:

  • Create a team on the adapters
  • Create a virtual switch on the team
  • Create a virtual network adapter for the management operating system to use
  • Optionally place the management adapter into a VLAN
  • Assign an IP, subnet, and gateway to the management adapter
  • Specify one or two DNS servers

It won’t take a great deal of sleuthing to discover that this script is identical to the preceding one, except that it does not disable VMQ.

[CmdletBinding()]param()
#requires -Modules Hyper-V, NetAdapter, NetLbfo

## User Modifications Start Here ##
$VirtualSwitchName = 'vSwitch'
$TeamName = 'vSwitch'

# use Get-NetAdapter to locate names for the following items
$Net1AdapterName = 'Ethernet'
$Net2AdapterName = 'Ethernet 2'
$Net3AdapterName = '' # leave blank if no third adapter
$Net4AdapterName = '' # leave blank if no fourth adapter

$ManagementIP = '192.168.25.164'
$ManagementSubnet = '255.255.255.0'
$ManagementGateway = '192.168.25.1'
$ManagementVLAN = 0 # leave at 0 if no VLAN

$DNSServer1 = '192.168.25.5'
$DNSServer2 = '' # leave blank if no secondary DNS

## User Modifications End Here ##

$Adapter1 = Get-NetAdapter -Name $Net1AdapterName
$Adapter2 = Get-NetAdapter -Name $Net2AdapterName
$Adapter3 = Get-NetAdapter -Name $Net3AdapterName
$Adapter4 = Get-NetAdapter -Name $Net4AdapterName

Write-Verbose -Message 'Creating team'
$Team = New-NetLbfoTeam -Name $VirtualSwitchName -TeamMembers $Adapter1.Name, $Adapter2.Name -TeamingMode SwitchIndependent -LoadBalancingAlgorithm Dynamic -Confirm:$false
$Adapter3, $Adapter4 | foreach {
	if ($_ -ne $null) { $AddedAdapter = Add-NetLbfoTeamMember -Team $TeamName -Name $_.Name -Confirm:$false }
}

$TeamAdapter = Get-NetAdapter 'vSwitch'

Write-Verbose -Message 'Creating virtual switch'
$VMSwitch = New-VMSwitch -Name $VirtualSwitchName -NetAdapterName $TeamName -AllowManagementOS $false -MinimumBandwidthMode Weight

Write-Verbose -Message 'Creating virtual management adapter'
Add-VMNetworkAdapter -ManagementOS -Name 'Management' -SwitchName $VirtualSwitchName
if ($ManagementVLAN -gt 0)
{
	Write-Verbose -Message 'Setting management adapter VLAN'
	Set-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName 'Management' -Access -VlanId $ManagementVLAN
}

Write-Verbose -Message 'Setting management adapter IP information'
netsh interface ip set address 'vEthernet (Management)' static $ManagementIP $ManagementSubnet $ManagementGateway 1 #netsh always works; New-NetIPAddress will fail if it thinks the adapter is disconnected
$DNSServers = New-Object System.Collections.ArrayList
$DNSServers += $DNSServer1
if ($DNSServer2) { $DNSServers += $DNSServer2 }
Set-DnsClientServerAddress -InterfaceAlias 'vEthernet (Management)' -ServerAddresses $DNSServers

 

Configure a clustered host with 2-4 gigabit adapters for converged networking

Use this script for a host that has between two and four gigabit adapters that will be a member of a cluster. Like the previous scripts, it will employ a converged networking configuration. The script will:

  • Create a team on the adapters
  • Disable VMQ for the physical adapters and the teamed adapter
  • Create a virtual switch on the team
  • Create virtual network adapters for the management operating system to use for management traffic, cluster communications, and Live Migration
  • Optionally place the virtual adapters into VLANs
  • Assign an IP, subnet, and gateway to the management adapter
  • Assign an IP and subnet mask to the cluster and Live Migration adapters
  • Prevent the cluster and Live Migration adapters from registering in DNS
  • Specify one or two DNS servers

Basic Usage for this Script

You just need to enter the necessary information for these items and execute it. It is essentially the same as the stand-alone multi-gigabit adapter script except that it also adds adapters for cluster communications and Live Migration traffic.

It does not arrange the adapters in an optimal order for Live Migration. The cluster will automatically prioritize the cluster and Live Migration adapters over the management adapter, but it might prioritize the cluster adapter over the Live Migration adapter. Practically, that will have no meaningful effect; these designations are mostly cosmetic. If you’d like to force the issue, you’ll need to do so separately. You could, of course, use Failover Cluster Manager for this. I’ve included a script later in this article that makes the setting change for you. You cannot combine these scripts because the cluster must exist before you can specify the Live Migration adapter order. Also, you only need to specify the order one time, not once per node.

Advanced Usage for this Script

You could do a great number of things with this script. One suggestion would be to add cluster creation/join logic. It would be non-trivial, but you’d be able to combine the Live Migration adapter ordering script.

[CmdletBinding()]param()
#requires -Modules Hyper-V, NetAdapter, NetLbfo

## User Modifications Start Here ##
$VirtualSwitchName = 'vSwitch'
$TeamName = 'vSwitch'

# use Get-NetAdapter to locate names for the following items
$Net1AdapterName = 'Ethernet 2'
$Net2AdapterName = 'Ethernet 3'
$Net3AdapterName = 'Ethernet 4' # leave blank if no third adapter
$Net4AdapterName = 'Ethernet 5' # leave blank if no fourth adapter

$ManagementIP = '192.168.25.164'
$ManagementSubnet = '255.255.255.0'
$ManagementGateway = '192.168.25.1'
$ManagementVLAN = 0 # leave at 0 if no VLAN

$ClusterIP = '192.168.10.164'
$ClusterSubnet = '255.255.255.0'
$ClusterVLAN = 0 # leave at 0 if no VLAN

$LiveMigrationIP = '192.168.15.164'
$LiveMigrationSubnet = '255.255.255.0'
$LiveMigrationVLAN = 0 # leave at 0 if no VLAN

$DNSServer1 = '192.168.25.5'
$DNSServer2 = '192.168.25.6' # leave blank if no secondary DNS

## User Modifications End Here ##

$Adapter1 = Get-NetAdapter -Name $Net1AdapterName
$Adapter2 = Get-NetAdapter -Name $Net2AdapterName
$Adapter3 = Get-NetAdapter -Name $Net3AdapterName
$Adapter4 = Get-NetAdapter -Name $Net4AdapterName

Write-Verbose -Message 'Creating team'
$Team = New-NetLbfoTeam -Name $VirtualSwitchName -TeamMembers $Adapter1.Name, $Adapter2.Name -TeamingMode SwitchIndependent -LoadBalancingAlgorithm Dynamic -Confirm:$false
$Adapter3, $Adapter4 | foreach {
	if ($_ -ne $null) { $AddedAdapter = Add-NetLbfoTeamMember -Team $TeamName -Name $_.Name -Confirm:$false }
}

$TeamAdapter = Get-NetAdapter 'vSwitch'

Write-Verbose -Message 'Disabling VMQ'
$TeamAdapter, $Adapter1, $Adapter2, $Adapter3, $Adapter4 | foreach {
	if ($_)
 {
		$VMQProperties = Get-NetAdapterAdvancedProperty -Name $_.Name | where DisplayName -Match 'V.*M.*Q'
		if ($VMQProperties)
		{
			$VMQProperties | foreach {
				if ($_.RegistryValue -eq 1) { Set-NetAdapterAdvancedProperty -InputObject $_ -RegistryValue 0 }
			}
		}
	}
}

Write-Verbose -Message 'Creating virtual switch'
$VMSwitch = New-VMSwitch -Name $VirtualSwitchName -NetAdapterName $TeamName -AllowManagementOS $false -MinimumBandwidthMode Weight

Write-Verbose -Message 'Creating virtual management adapter'
Add-VMNetworkAdapter -ManagementOS -Name 'Management' -SwitchName $VirtualSwitchName
if ($ManagementVLAN -gt 0)
{
	Write-Verbose -Message 'Setting management adapter VLAN'
	Set-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName 'Management' -Access -VlanId $ManagementVLAN
}

Write-Verbose -Message 'Setting management adapter IP information'
netsh interface ip set address 'vEthernet (Management)' static $ManagementIP $ManagementSubnet $ManagementGateway 1 #netsh always works; New-NetIPAddress will fail if it thinks the adapter is disconnected

Write-Verbose -Message 'Creating virtual cluster traffic adapter'
Add-VMNetworkAdapter -ManagementOS -Name 'Cluster' -SwitchName $VirtualSwitchName
if ($ClusterVLAN -gt 0)
{
	Write-Verbose -Message 'Setting cluster adapter VLAN'
	Set-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName 'Cluster' -Access -VlanId $ClusterVLAN
}
Set-DnsClient -InterfaceAlias 'vEthernet (Cluster)' -RegisterThisConnectionsAddress $false
netsh interface ip set address 'vEthernet (Cluster)' static $ClusterIP $ClusterSubnet

Write-Verbose -Message 'Creating virtual Live Migration adapter'
Add-VMNetworkAdapter -ManagementOS -Name 'Live Migration' -SwitchName $VirtualSwitchName
if ($LiveMigrationVLAN -gt 0)
{
	Write-Verbose -Message 'Setting Live Migration adapter VLAN'
	Set-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName 'Live Migration' -Access -VlanId $LiveMigrationVLAN
}
Set-DnsClient -InterfaceAlias 'vEthernet (Live Migration)' -RegisterThisConnectionsAddress $false
netsh interface ip set address 'vEthernet (Live Migration)' static $LiveMigrationIP $LiveMigrationSubnet

Write-Verbose -Message 'Setting DNS client addresses'
$DNSServers = New-Object System.Collections.ArrayList
$DNSServers += $DNSServer1
if ($DNSServer2) { $DNSServers += $DNSServer2 }
Set-DnsClientServerAddress -InterfaceAlias 'vEthernet (Management)' -ServerAddresses $DNSServers

 

Configure a clustered host with 2-4 10 GbE adapters for converged networking

This script is identical to the preceding except that it leaves VMQ enabled. It does the following:

  • Create a team on the adapters
  • Create a virtual switch on the team
  • Create virtual network adapters for the management operating system to use for management traffic, cluster communications, and Live Migration
  • Optionally place the virtual adapters into VLANs
  • Assign an IP, subnet, and gateway to the management adapter
  • Assign an IP and subnet mask to the cluster and Live Migration adapters
  • Prevent the cluster and Live Migration adapters from registering in DNS
  • Specify one or two DNS servers

Basic Usage for this Script

These notes are identical to those of the preceding script.

You just need to enter the necessary information for these items and execute it. It is essentially the same as the stand-alone multi-gigabit adapter script except that it also adds adapters for cluster communications and Live Migration traffic.

It does not arrange the adapters in an optimal order for Live Migration. The cluster will automatically prioritize the cluster and Live Migration adapters over the management adapter, but it might prioritize the cluster adapter over the Live Migration adapter. Practically, that will have no meaningful effect; these designations are mostly cosmetic. If you’d like to force the issue, you’ll need to do so separately. You could, of course, use Failover Cluster Manager for this. I’ve included a script later in this article that makes the setting change for you. You cannot combine these scripts because the cluster must exist before you can specify the Live Migration adapter order. Also, you only need to specify the order one time, not once per node.

Advanced Usage for this Script

These notes are identical to those of the preceding script.

You could do a great number of things with this script. One suggestion would be to add cluster creation/join logic. It would be non-trivial, but you’d be able to combine the Live Migration adapter ordering script.

[CmdletBinding()]param()
#requires -Modules Hyper-V, NetAdapter, NetLbfo

## User Modifications Start Here ##
$VirtualSwitchName = 'vSwitch'
$TeamName = 'vSwitch'

# use Get-NetAdapter to locate names for the following items
$Net1AdapterName = 'Ethernet 2'
$Net2AdapterName = 'Ethernet 3'
$Net3AdapterName = 'Ethernet 4' # leave blank if no third adapter
$Net4AdapterName = 'Ethernet 5' # leave blank if no fourth adapter

$ManagementIP = '192.168.25.164'
$ManagementSubnet = '255.255.255.0'
$ManagementGateway = '192.168.25.1'
$ManagementVLAN = 0 # leave at 0 if no VLAN

$ClusterIP = '192.168.10.164'
$ClusterSubnet = '255.255.255.0'
$ClusterVLAN = 0 # leave at 0 if no VLAN

$LiveMigrationIP = '192.168.15.164'
$LiveMigrationSubnet = '255.255.255.0'
$LiveMigrationVLAN = 0 # leave at 0 if no VLAN

$DNSServer1 = '192.168.25.5'
$DNSServer2 = '192.168.25.6' # leave blank if no secondary DNS

## User Modifications End Here ##

$Adapter1 = Get-NetAdapter -Name $Net1AdapterName
$Adapter2 = Get-NetAdapter -Name $Net2AdapterName
$Adapter3 = Get-NetAdapter -Name $Net3AdapterName
$Adapter4 = Get-NetAdapter -Name $Net4AdapterName

Write-Verbose -Message 'Creating team'
$Team = New-NetLbfoTeam -Name $VirtualSwitchName -TeamMembers $Adapter1.Name, $Adapter2.Name -TeamingMode SwitchIndependent -LoadBalancingAlgorithm Dynamic -Confirm:$false
$Adapter3, $Adapter4 | foreach {
	if ($_ -ne $null) { $AddedAdapter = Add-NetLbfoTeamMember -Team $TeamName -Name $_.Name -Confirm:$false }
}

$TeamAdapter = Get-NetAdapter 'vSwitch'

Write-Verbose -Message 'Creating virtual switch'
$VMSwitch = New-VMSwitch -Name $VirtualSwitchName -NetAdapterName $TeamName -AllowManagementOS $false -MinimumBandwidthMode Weight

Write-Verbose -Message 'Creating virtual management adapter'
Add-VMNetworkAdapter -ManagementOS -Name 'Management' -SwitchName $VirtualSwitchName
if ($ManagementVLAN -gt 0)
{
	Write-Verbose -Message 'Setting management adapter VLAN'
	Set-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName 'Management' -Access -VlanId $ManagementVLAN
}

Write-Verbose -Message 'Setting management adapter IP information'
netsh interface ip set address 'vEthernet (Management)' static $ManagementIP $ManagementSubnet $ManagementGateway 1 #netsh always works; New-NetIPAddress will fail if it thinks the adapter is disconnected

Write-Verbose -Message 'Creating virtual cluster traffic adapter'
Add-VMNetworkAdapter -ManagementOS -Name 'Cluster' -SwitchName $VirtualSwitchName
if ($ClusterVLAN -gt 0)
{
	Write-Verbose -Message 'Setting cluster adapter VLAN'
	Set-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName 'Cluster' -Access -VlanId $ClusterVLAN
}
Set-DnsClient -InterfaceAlias 'vEthernet (Cluster)' -RegisterThisConnectionsAddress $false
netsh interface ip set address 'vEthernet (Cluster)' static $ClusterIP $ClusterSubnet

Write-Verbose -Message 'Creating virtual Live Migration adapter'
Add-VMNetworkAdapter -ManagementOS -Name 'Live Migration' -SwitchName $VirtualSwitchName
if ($LiveMigrationVLAN -gt 0)
{
	Write-Verbose -Message 'Setting Live Migration adapter VLAN'
	Set-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName 'Live Migration' -Access -VlanId $LiveMigrationVLAN
}
Set-DnsClient -InterfaceAlias 'vEthernet (Live Migration)' -RegisterThisConnectionsAddress $false
netsh interface ip set address 'vEthernet (Live Migration)' static $LiveMigrationIP $LiveMigrationSubnet

Write-Verbose -Message 'Setting DNS client addresses'
$DNSServers = New-Object System.Collections.ArrayList
$DNSServers += $DNSServer1
if ($DNSServer2) { $DNSServers += $DNSServer2 }
Set-DnsClientServerAddress -InterfaceAlias 'vEthernet (Management)' -ServerAddresses $DNSServers

 

Set preferred order for cluster Live Migration networks

This script aligns with the two preceding scripts to ensure that the cluster chooses the named “Live Migration” adapter first when moving virtual machines between nodes. The “Cluster” virtual adapter will be used second. The management adapter will be used as the final fallback.

Basic Usage for this Script

Use this script after you’ve run one of the above two clustered host scripts and joined them into a cluster.

Advanced Usage for this Script

Modify this to change the order of Live Migration adapters. You must specify all adapters recognized by the cluster. Check the “MigrationExcludeNetworks” registry key that’s in the same location as “MigrationNetworkOrder”.

[CmdletBinding()]param()
#requires -Modules FailoverClusters

$NetworksToInclude = [String]::Join(';', @(
    (Get-ClusterNetwork -Name 'Live Migration').Id,
    (Get-ClusterNetwork -Name 'Cluster').Id,
    (Get-ClusterNetwork -Name 'Management').Id
))

$VMParametersKey = 'HKLM:ClusterResourceTypesVirtual MachineParameters'

Write-Verbose -Message 'Setting Live Migration adapter order'
Set-ItemProperty -Path $VMParametersKey -Name 'MigrationNetworkOrder' -Value $NetworksToInclude

 

Exclude cluster networks from Live Migration

This script is intended to be used as an optional adjunct to the preceding script. Since my scripts set up all virtual adapters to be used in Live Migration, the network names used here are fabricated.

Basic Usage for this Script

You’ll need to set the network names to match yours, but otherwise, the script does not need to be altered.

Advanced Usage for this Script

This script will need to be modified in order to be used at all.

[CmdletBinding()]param()
#requires -Modules FailoverClusters

$NetworksToExclude = [String]::Join(';', @(
    (Get-ClusterNetwork -Name 'Storage1').Id,
    (Get-ClusterNetwork -Name 'Storage2').Id
))

$VMParametersKey = 'HKLM:ClusterResourceTypesVirtual MachineParameters'

Write-Verbose -Message 'Excluding adapters from Live Migration'
Set-ItemProperty -Path $VMParametersKey -Name 'MigrationExcludeNetworks' -Value $NetworksToExclude

 

Altaro Hyper-V Backup
Share this post

Not a DOJO Member yet?

Join thousands of other IT pros and receive a weekly roundup email with the latest content & updates!

6 thoughts on "7 Powerful Scripts for Practical Hyper-V Network Configurations"

  • Dave Bragg says:

    Hi Eric,

    First off, thanks for a great blog and for sharing your experience and tools.

    I’ve been playing with these scripts and had a small issue when I changed the Team name in the multi-adapter scripts.

    I made the following alterations and it worked like a charm. The line numbers in parentheses are for the clustered scripts.

    Line 30 (38) to “$Team = New-NetLbfoTeam -Name $TeamName ….” and 35 (43) to “$TeamAdapter = Get-NetAdapter $TeamName”

    Thanks again for sharing all of this. I’m finding it very helpful to see “real-world” examples as I am getting to grips with using Powershell. 🙂

    Regards,
    Dave

    • Eric Siron says:

      I noticed some unintended quirks the other day as well. I’ll incorporate your suggestions into a future update.
      Thanks for the feedback!

  • John Rhines says:

    Hi Eric,

    Thank you very much for the scripts! I have one question though, I’m having a hard time modifying the script to enable SR-IOV when creating the VMswitch. Any chance you could update the script to include that function?

    Thank you once again!
    John

    • Eric Siron says:

      What trouble are you having? Just amend the New-VMSwitch line to include -EnableIOV $true. IOV and teaming are not compatible, so it would break most of these scripts.

Leave a comment or ask a question

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

Your email address will not be published.

Notify me of follow-up replies via email

Yes, I would like to receive new blog posts by email

What is the color of grass?

Please note: If you’re not already a member on the Dojo Forums you will create a new account and receive an activation email.