VMware Cloud Community
zik
Enthusiast
Enthusiast
Jump to solution

Is NIC connected to a standard or to a distributed portgroup?

In thread Get-View Config.Hardwar.Device Network Adapter Key 4000 resolve to Device Type? posted this code to get E1000 devices.


$vm = Get-View -ViewType VirtualMachine -Filter @{'Name'='MyVM'}

$vm.Config.Hardware.Device | %{$_.GetType()}

$vm.Config.Hardware.Device | Where {$_ -is [VMware.Vim.VirtualE1000]}


How do I tell if this is connected to a standard or to a distributed portgroup?


Also, if it is connected to a standard portgroup, how do I find its switch?

Reply
0 Kudos
1 Solution

Accepted Solutions
zik
Enthusiast
Enthusiast
Jump to solution

I found this excellent example: Re: Need VM portgroup inventory

$vm = Get-View -ViewType VirtualMachine -Filter @{'Name'='MyVM'}

$vm.UpdateViewData("Runtime.Host.ConfigManager.NetworkSystem.NetworkInfo.Vswitch","Runtime.Host.ConfigManager.NetworkSystem.NetworkInfo.PortGroup")

$nic = $vm.Config.Hardware.Device | Where {$_ -is [VMware.Vim.VirtualEthernetCard]}

if ($nic.Backing -is [VMware.Vim.VirtualEthernetCardNetworkBackingInfo]) {  ## Standard Switch

  ($vm.Runtime.LinkedView.Host.ConfigManager.LinkedView.NetworkSystem.NetworkInfo.Portgroup | ?{$_.Spec.Name -eq $nic.Backing.DeviceName}).Spec.vSwitchName

}

View solution in original post

Reply
0 Kudos
3 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can find the switch with:

$vm | Get-VIObjectByVIView | Get-VDSwitch

You can tell if the portgroup is a distributed portgroup with:

if (($vm | Get-VIObjectByVIView | Get-VDSwitch).GetType().Name -eq 'VmwareVDSwitchImpl') {'Distributed portgroup'} else {'Standard portgroup'}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
zik
Enthusiast
Enthusiast
Jump to solution

Well, Get-VDSwitch only returns distributed switches and, if there are two switches attached, it doesn't tell me which is connected to which NIC.

I can use Get-VirtualSwitch to return both standard and distributed switches but there is a warning that it may not in the future, and it also doesn't match up the NIC.

Something like this will get me the portgroup:

$pg = Get-View -Id ((($vm.config.hardware.device | where {$_ -is [VMware.Vim.VirtualEthernetCard]} | select -first 1).Backing.Network))

or, rather a Network view.  I don't see any links from there back to the switch.

Reply
0 Kudos
zik
Enthusiast
Enthusiast
Jump to solution

I found this excellent example: Re: Need VM portgroup inventory

$vm = Get-View -ViewType VirtualMachine -Filter @{'Name'='MyVM'}

$vm.UpdateViewData("Runtime.Host.ConfigManager.NetworkSystem.NetworkInfo.Vswitch","Runtime.Host.ConfigManager.NetworkSystem.NetworkInfo.PortGroup")

$nic = $vm.Config.Hardware.Device | Where {$_ -is [VMware.Vim.VirtualEthernetCard]}

if ($nic.Backing -is [VMware.Vim.VirtualEthernetCardNetworkBackingInfo]) {  ## Standard Switch

  ($vm.Runtime.LinkedView.Host.ConfigManager.LinkedView.NetworkSystem.NetworkInfo.Portgroup | ?{$_.Spec.Name -eq $nic.Backing.DeviceName}).Spec.vSwitchName

}

Reply
0 Kudos