Connect a Host to a Virtual Network (Script)

 

Updated: May 13, 2016

Applies To: System Center 2012 R2 Virtual Machine Manager, System Center 2012 - Virtual Machine Manager

To make logical networks available to virtual machines on an external virtual network, you must associate a logical network with the physical network adapter on a host, and configure virtual network settings.

For more information about configuring network settings on Hyper-V hosts, see How to Configure Network Settings on a Hyper-V Host.

Disclaimer

The following script takes input for the VMHostName, LogNetName, HostAdapterName, and VirtualNetName parameters and associates the host with a logical network. The script then creates a virtual network on the host.

  
# Description:   This script associates a virtual machine host with a logical   
#                network and then creates a virtual network for the host.  
  
Param (  
   [parameter(Mandatory=$true)]  
   [String] $VMHostName=$(throw "Please provide the name of a virtual machine host."),  
  
   [parameter(Mandatory=$true)]  
   [String] $LogNetName=$(throw "Please provide the name of a logical network."),  
  
   [parameter(Mandatory=$true)]  
   [String] $HostAdapterName=$(throw "Please provide the name of the host adapter."),  
  
   [parameter(Mandatory=$true)]  
   [String] $VirtualNetName=$(throw "Please provide the virtual network name.")  
   )  
  
# Get the virtual machine host.  
$VMHost = Get-SCVMHost -ComputerName $VMHostName  
  
# Get the logical network.  
$LogNet = Get-SCLogicalNetwork -Name $LogNetName  
  
# Get the network adapter for the host.  
$HostAdapter = Get-SCVMHostNetworkAdapter -VMHost $VMHost -Name $HostAdapterName  
  
# Set the logical network on the host.  
Set-SCVMHostNetworkAdapter -VMHostNetworkAdapter $HostAdapter -AddOrSetLogicalNetwork $LogNet  
  
# Create the virtual network.  
New-SCVirtualNetwork -Name $VirtualNetName -Description "External virtual network for $VMHost" -VMHost $VMHost -VMHostNetworkAdapter $HostAdapter  

Set-SCVMHostNetworkAdapter
New-SCVirtualNetwork

Show: