Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Need to remotely set the VLAN IDs on your server core installation? Some friends on the Hyper-V team put together a powershell 2.0 script for setting vlan id which you could run on the server core directly (rename the extension of the script to .ps1). The script accepts a vm name, and vlan id, finds the first connected network adapter of that vm and sets it to that vlan id.
################################################################################
#
# Copyright ©2009 Microsoft Corporation. All rights reserved.
#
# File: modify-vlan.ps1
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
# ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
# PARTICULAR PURPOSE.
#
# Copyright ©2009 Microsoft Corporation. All rights reserved.
#
################################################################################param(
$vmName = $(throw "Must supply a virtual machine name"),
$vlanId = $(throw "Must supply vlan id"),
$computer = "."
)$ns = "root\virtualization"
# get the computer system
$vm = gwmi -namespace $ns -computerName $computer Msvm_ComputerSystem -filter "ElementName = '$vmName'"if ($vm -eq $null)
{
"No virtual machine with name '$vmName'"
return
}# get its related vssd
$vssd = gwmi -namespace $ns -computerName $computer -query "associators of {$vm} where AssocClass=Msvm_SettingsDefineState"# get its synthetic and enumlated NICs
$nics = gwmi -namespace $ns -computerName $computer -query "associators of {$vssd} where AssocClass=Msvm_VirtualSystemSettingDataComponent" |
where {$_.ResourceType -eq 10 -and ($_.ResourceSubType -eq 'Microsoft Synthetic Ethernet Port' -or
$_.ResourceSubType -eq 'Microsoft Emulated Ethernet Port')}if ($nics -eq $null)
{
# vm does not have any NICs.
"Virtual machine '$vmName' does not have any NICs"
return
}# Find the first nic which has a connection
$nic = $nics | where {$_.Connection -ne $null} | select-object -first 1if ($nic -eq $null)
{
"None of the NICs on virtual machine '$vmName' are connected"
return
}# get the connected switch port.
$connectedPort = [wmi]$nic.Connection[0]# need to set the external trunk list of the external port on this same switch
# to include this vlan id, else traffic won't get through.# first get its switch.
$switch = gwmi -namespace $ns -computerName $computer -query "associators of {$connectedPort} where AssocClass=Msvm_HostedAccessPoint"# enumerate all of its ports. Find the port which is externally connected.
$ports = gwmi -namespace $ns -computerName $computer -query "associators of {$switch} where ResultClass=Msvm_SwitchPort"
$externalPort = $nullforeach ($p in $ports)
{
$switchLanEndpoint = gwmi -namespace $ns -computerName $computer -query "associators of {$p} where ResultClass=Msvm_SwitchLanEndpoint"
if ($switchLanEndpoint -ne $null)
{
$externalEthernetPort = gwmi -namespace $ns -computerName $computer -query "associators of {$switchLanEndpoint} where resultclass=Msvm_ExternalEthernetPort"
if ($externalEthernetPort -ne $null)
{
# we got it, we found the switch port connected to the external
# port, save it and break out of the loop.
$externalPort = $p
break
}
}
}if ($externalPort -ne $null)
{
# get the port's VlanEndpoint
$vlan = gwmi -namespace $ns -computerName $computer -query "associators of {$externalPort} where AssocClass=Msvm_BindsTo"
# get the vlan's setting object
$vlanSetting = gwmi -namespace $ns -computerName $computer -query "associators of {$vlan} where AssocClass=Msvm_NetworkElementSettingData"# get the current trunk list and add the new vlan id into it.
# one thing additionally we could do here, which the Hyper-V UI does is clean up
# the old AccessVlan id of the port from the trunk list. But, we can't just remove the
# old AccessVlan from the trunklist, because another port might be using the same vlan id.
# We would have to enumerate all of the ports on the switch and build a new trunk list.
# For the purposes of this script, don't worry about cleaning up old vlan id' from the trunk list.
$trunkList = $vlanSetting.TrunkedVLANList
if ($trunkList -notcontains $vlanid)
{
$trunkList = $trunkList + $vlanId
$vlanSetting.TrunkedVLANList = $trunkList
$result = $vlanSetting.Put()
}
# set the vlan mode into trunking mode.
if ($vlan.DesiredEndpointMode -ne 5)
{
$vlan.DesiredEndpointMode = 5
$result = $vlan.Put()
}
}# Now that we have finished with the trunklist, set the vlan id of the original connected port.
$vlan = gwmi -namespace $ns -computerName $computer -query "associators of {$connectedPort} where AssocClass=Msvm_BindsTo"
$vlanSetting = gwmi -namespace $ns -computerName $computer -query "associators of {$vlan} where AssocClass=Msvm_NetworkElementSettingData"
$vlanSetting.AccessVlan = $vlanId
$result = $vlanSetting.Put()################################################################################
#
# .SYNOPSIS
#
# Sets the vlan id of the first connected NIC on a virtual machine.
#
#
# .DESCRIPTION
#
# Finds the first connected NIC of a virtual machine and sets up its VLAN
# settings in the same manner as the Hyper-V UI. First it checks to see if
# the NIC is connected to an external network, and if so it adds the VLAN id
# to the trunking list, and sets the endpoint mode to trunking. This makes sure
# that traffic with that VLAN id from the external network goes through the switch.
# Next it sets up the Access VLAN of the virtual machine's NIC.
#
# .PARAMETER VmName
#
# The name of the virtual machine to modify. We will modify the first connected
# NIC we find on this virtual machine.
#
# .PARAMETER VlanId
#
# The vlan id to set.
#
# .PARAMETER Computer
#
# The host computer where $vmname resides.
#
# .EXAMPLE
#
# C:\PS> mount-vlan.ps1 foo 512
#
#
################################################################################
For more info on how to use PS cmdlets see: https://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/index.mspx
See also James O’Neil’s New and improved PowerShell Library for Hyper-V. Now with more functions and... documentation!
For all 35 sample Hyper-V PS1 scripts in a zipfile, go to: Hyper-V PowerShell Example Scripts.zip-download