When you are scripting against virtual switches – it is good to know what type of switch you are talking to. In order to figure this out you need to find out whether the virtual switch has an internal network connection, external network connection, neither or both. Here are some scripts that will do just that:
The logic flow for these scripts is as follows:
- Get all virtual switches (Msvm_VirtualSwitch) and then iterate over each switch.
- Follow the CIM_HostedAccessPoint association on the switch to get every CIM_SwitchPort associated with the virtual switch.
- Follow the Msvm_ActiveConnection association on the switch port to get every Msvm_SwitchLANEndpoint associated with each switch port.
- Follow the CIM_DeviceSAPImplementation association on the switch LAN endpoint to get every CIM_EthernetPort associated with each switch LAN endpoint.
- Look at the Ethernet port class (external or internal) and tally the number of internal and external Ethernet ports associated with the switch in question.
- Look at the final tally to figure out the type of the switch.
Some notes to make
- Many of these queries return 0, 1 or many results – which VBScript tends to do a better job of handling than PowerShell. Hence why the VBScript is shorter.
- UPDATE: An MVP pointed out how to make my PowerShell Script shorter – so it has been corrected.
- Switch ports that are connected to virtual machines do not return a Msvm_SwitchLANEndpoint (they return a Msvm_VMSwitchLANEndpoint) so these switch ports are not considered during this code.
- The “Msvm_ActiveConnection” object only exists on switch ports that are connected to internal Ethernet ports, external Ethernet ports or running virtual machines.
- It is possible to have a virtual switch with more than 1 external Ethernet port or more than 1 internal Ethernet port – but the Hyper-V UI will never make one (and such configurations are not tested or recommended).