Powershell – extracting VLAN IDs from the Hyper-V host

Lately I have been replicating live Hyper-V cluster configuration to the similar cluster in the DR. One of the things I had to do is to ask our networking guys to point the same VLANs to the DR servers. In our case VLANs are configured on the VMs adaper side, therefore for me to collect all the VLANs I had to check every VM. Boring 🙂

So I decided to check the WMI. Hyper-V has the Networking classes – http://msdn.microsoft.com/en-us/library/cc136953(v=vs.85).aspx

What I needed was in the Msvm_VLANEndpointSettingData class, AccessVLAN property. Here is a command to get this property out: 

Get-WmiObject -Class Msvm_VLANEndpointSettingData -Namespace "root\virtualization" -ComputerName "." | format-table -property AccessVLAN -Autosize

Unfortunatelly the above class does not have direct mapping to a VM names, but this is I notice a frequent with VMI, when data are distributed across several places and you need to collect them from several places and then combine together.

VM names are stored in the Msvm_ComputerSystem class: 

Get-WmiObject -Class Msvm_ComputerSystem -Namespace "root\virtualization" -ComputerName "." 

To link both classes Msvm_SyntheticEthernetPortSettingData class can be used since it has properties from the both above classes:

Get-WmiObject -Class Msvm_SyntheticEthernetPortSettingData -Namespace "root\virtualization" -ComputerName "."

This is something similar to a SQL query – there are several tables and you need to join them. In Powershell joining will be done using FOR loops. Here’s a script [just copy it to a name.ps1 file]:

"________________________________________________________________________________"
# Connecting to a Msvm_ComputerSystem WMI
$vms = Get-WmiObject -Class Msvm_ComputerSystem -Namespace "root\virtualization" -ComputerName "."

# Connecting to a Msvm_SyntheticEthernetPortSettingData
$mapping = Get-WmiObject -Class Msvm_SyntheticEthernetPortSettingData -Namespace "root\virtualization" -ComputerName "."

# Connecting to a Msvm_VLANEndpointSettingData VMI
$vlans = Get-WmiObject -Class Msvm_VLANEndpointSettingData -Namespace "root\virtualization" -ComputerName "."

# For every detected VM we are searching for a VLAN via mapping class:
foreach($Vms_Obj in $vms)
{
 foreach($map_Obj in $mapping)
 {
  # This check is needed to match objects from different WMI classes
  If ($map_Obj.InstanceID.Contains($Vms_Obj.Name))
  {
   foreach($vlan_Obj in $vlans)
   {
    If ($map_Obj.Connection -match $vlan_Obj.ElementName)
    {
    Write-Host $vlan_Obj.AccessVLAN `t $Vms_Obj.ElementName `t $map_Obj.Description #" "
    }
    else
    {
    # Uncomment for debuging
    #Write-Host "Skiping: "
    #Write-Host $map_Obj.Connection
    #Write-Host $vlan_Obj.ElementName
    }
   }
  }
  else
  {
  # Uncomment for debuging
  #Write-Host "Skiping: " $Vms_Obj.Name
  }
 }
}
"________________________________________________________________________________"

Let me know if this code can be improved.

Advertisements

One thought on “Powershell – extracting VLAN IDs from the Hyper-V host

Leave a Comment here

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s