Hyper-V, References, Resources, Scripting, Virtualization, Windows
Leave a comment

Enumerating Hyper-V virtual switches

Here is a script you can use to enumerate all the virtual switches you created using Hyper-V Virtual Network Manager.

‘Script Start
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Set objWMIService = GetObject(“winmgmts:\\” & “.” & “\root\virtualization”)
Set colItems = objWMIService.ExecQuery(“SELECT * FROM Msvm_VirtualSwitch”, “WQL”, _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
WScript.Echo “ElementName: ” & objItem.ElementName
WScript.Echo “Name: ” & objItem.Name
Next

‘Script End

Name property gives the text you have provided during switch creation and ElementName is the GUID assigned to the virtual switch. There are many other properties most of which are default values.

Msvm_VirtualSwitch class is neither documented on MSDN nor I could find anything on Google. I generally use PowerShell to understand WMI objects and methods/Properties provided by each of these objects. PowerShell revealed the Msvm_VirtualSwitch class and it’s properties. PowerShell’s Get-WMIObject command-let is an excellent tool to learn WMI interfaces. Here is how I found Msvm_VirtualSwitch class

Get-WmiObject -namespace root\virtualization -list will list all the classes under root\virtualization name space.
$VSwitch = get-wmiobject -namespace root\virtualization -class Msvm_VirtualSwitch will instantiate the class in to $VSwitch
Now, $VSwitch | get-member | where {$_.Membertype -match ‘Property’} will list all properties of a Virtual Switch and $VSwitch | get-member | where {$_.Membertype -match ‘Method’} will list all the methods supported by this class.

MSDN documentation is still beta and I expect all this to be available once Hyper-V RTM is released. Have fun..!

Update: Msvm_VirtualSwitch is now documented on MSDN