Hyper-V How To: Map your Virtual Network Settings using Script

Some friends here on the Hyper-V team shared a PowerShell 2.0 script for getting a mapping your virtual network settings:

# Navigate Network Topology to determine Virtual Switch type
# ie: Internal, External, External-Shared, or Private

# Prompt for the Hyper-V Server to use
$HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
write-host
# Get the list of all available network switches
$query = "Select * From Msvm_VirtualSwitch"
$VirtualSwitches = gwmi -namespace "root\virtualization" -Query $query -computername $HyperVServer
# Iterate over each virtual switch
foreach ($VirtualSwitch in $VirtualSwitches)
{
# Initialize variables for counting number of internal and external ports per switch
$InternalPortCount = 0
$ExternalPortCount = 0
# Get the Switch ports on the virtual switch
$query = "Associators of {$VirtualSwitch} where ResultClass=CIM_SwitchPort"
$switchPorts = gwmi -namespace "root\virtualization" -Query $query -computername $HyperVServer
# A VM only switch with no VMs connected will return null
if ($switchPorts -ne $null)
{
# Iterate over each switch port
foreach ($switchPort in @($switchPorts))
{
# Get the Msvm_SwitchLANEndpoint associated with the switch port
$query = "Associators of {$switchPort} where ResultClass=Msvm_SwitchLANEndpoint"
$SwitchLANEndpoint = gwmi -namespace "root\virtualization" -Query $query -computername $HyperVServer
# If there is no active connection on the switch port the results will be null
if ($SwitchLANEndpoint -ne $null)
{
# Get the CIM_EthernetPort for the Msvm_SwitchLANEndpoint
$query = "Associators of {$SwitchLANEndpoint} where ResultClass=CIM_EthernetPort"
$EthernetPort = gwmi -namespace "root\virtualization" -Query $query -computername $HyperVServer
# Check to see if the associated Ethernet port is an internal port
if ($EthernetPort.__CLASS -eq "Msvm_InternalEthernetPort")
{
$InternalPortCount = $InternalPortCount + 1
}
# Check to see if the associated Ethernet port is an external port
if ($EthernetPort.__CLASS -eq "Msvm_ExternalEthernetPort")
{
$ExternalPortCount = $ExternalPortCount + 1
}
}
}
}
switch ($InternalPortCount)
{
0
{
switch ($ExternalPortCount)
{
0 {$output = "The virtual switch '" + $VirtualSwitch.ElementName + "' is a virtual machine only virtual network."}
1 {$output = "The virtual switch '" + $VirtualSwitch.ElementName + "' is an external-only virtual network."}
default {$output = "The virtual switch '" + $VirtualSwitch.ElementName + "' is not a standard virtual network."}
}
}
1
{
switch ($ExternalPortCount)
{
0 {$output = "The virtual switch '" + $VirtualSwitch.ElementName + "' is an internal virtual network."}
1 {$output = "The virtual switch '" + $VirtualSwitch.ElementName + "' is an external virtual network."}
default {$output = "The virtual switch '" + $VirtualSwitch.ElementName + "' is not a standard virtual network."}
}
}
default {$output = "The virtual switch '" + $VirtualSwitch.ElementName + "' is not a standard virtual network."}
}
write-host $output
}

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