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?
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
}
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'}
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.
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
}