VMware Cloud Community
jondercikwork
Contributor
Contributor

Get Host NICs assigned to a dvSwitch

I am looking to get a lsit of the host network adapters assigned to a dvSwitch so they can be used to create a Standard vSwitch.  When I have tried using the get-virtualSwitch cmdlet it does not return anything with the NICs assigned to the dvSwitch.

Also, how do I remove a physical nic from a switch either standard or dvs?

Anyone have any suggestions?

18 Replies
LucD
Leadership
Leadership

Try the following to get a report of the PNics connected to the dvSwitches.

$report = @()
foreach($sw in (Get-VirtualSwitch -Distributed)){
    $uuid = $sw.ExtensionData.Summary.Uuid
    $sw.ExtensionData.Config.Host | %{
        $esx = Get-View $_.Config.Host
        $netSys = Get-View $esx.ConfigManager.NetworkSystem
        $netSys.NetworkConfig.ProxySwitch | where {$_.Uuid -eq $uuid} | %{
            $_.Spec.Backing.PnicSpec | %{
                $row = "" | Select Host,dvSwitch,PNic
                $row.Host = $esx.Name
                $row.dvSwitch = $sw.Name
                $row.PNic = $_.PnicDevice
                $report += $row            }
        }
    }
}
$report 

Removing a PNic from a regular switch can be done with the Set-VirtualSwitch cmdlet and the Nic parameter.

Removing a PNic from a distributed switch is a bit more complicated and requires using the SDK methods. It is similar to my dvSwitch scripting – Part 4 – NIC teaming post


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jondercikwork
Contributor
Contributor

I actually found an easier way to get the NICs assigned to the dvSwitch from the PowerCLI blog site.

$dvSwitchName ="dvSwitchTest"
$dvSwitch = Get-VirtualSwitch -Name $dvSwitchName

Get-VMHostNetworkAdapter -DistributedSwitch $dvSwitch | select  VMHost, Name, Mac, IP

Ya, it is the removal part that is being a PITA.  This is yet another reason for me to dislike/hate the dvSwitch and I really hope Vmware rethinks how it is implemented, because it kinda stinks.

Thank you for the help.

0 Kudos
SuperSpike
Contributor
Contributor

Hey LucD, in what version of PowerCLI did the -Distributed parameter for Get-VirtualSwitch get added? I've only got -VM, -VMhost, -Name, and -Server as valid parameters. Is this new to 4.1 U1?

@Virtual_EZ
0 Kudos
LucD
Leadership
Leadership

Correct, that was introduced in PowerCLI 4.1U1.

See the Release Notes, the What's New section.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jondercikwork
Contributor
Contributor

I used Onyx to get the SDK code I needed.  I dont know exactly what each line does, but it works....

0 Kudos
SuperSpike
Contributor
Contributor

That's a great little script. So I can get the hosts in the dvSwitch, the dvSwitch name and the PNIC names. What attribute contains the PNIC state? I want to know if my dvSwitch uplinks are up or down. I'm guessing this is buried in the SDK somewhere, but I'm not sure where to find it.

@Virtual_EZ
0 Kudos
LucD
Leadership
Leadership

With a small adaptation you can include the state of the uplinks

$report = @()
foreach($sw in (Get-VirtualSwitch -Distributed)){
    $uuid = $sw.ExtensionData.Summary.Uuid
    $sw.ExtensionData.Config.Host | %{
        $portStates = $sw.ExtensionData.FetchDVPorts($null)
        $esx = Get-View $_.Config.Host
        $netSys = Get-View $esx.ConfigManager.NetworkSystem
        $netSys.NetworkConfig.ProxySwitch | where {$_.Uuid -eq $uuid} | %{
            foreach($pnicSpec in $_.Spec.Backing.PnicSpec){
                $row = "" | Select Host,dvSwitch,PNic,PortLinkUp
                $row.Host = $esx.Name
                $row.dvSwitch = $sw.Name
                $row.PNic = $pnicSPec.PnicDevice
                $row.PortLinkUp = ($portStates | where{$_.Key -eq $pnicSPec.UplinkPortKey}).State.RunTimeInfo.LinkUp 
                $report += $row
            }         }     } } $report


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
SuperSpike
Contributor
Contributor

Very cool. Thank you. Can I ask a (hopefully) simple question? Where can I find documentation on all of the Get-View managed object and data object types?

For example, how on earth does someone know that "dvswitch.ExtensionData.Config.Host.ExtensionData.FetchDVPorts.State.RunTimeInfo.LinkUp" exists? :smileylaugh:

The SDK is obviously available, but it can be difficult to know where to start. Is there an easy way to "walk" the SDK "tree" using Get-View for a particular View Type?

I took a look at Onyx and that looks great for scripting something that you know how to do in the GUI, but for retrieving state info, it might not be so easy.

In this particular case, I'm trying to write a monitoring script for my PNICs, which are in both standard and distributed vSwitches.

@Virtual_EZ
0 Kudos
LucD
Leadership
Leadership

I'm afraid that the SDK Reference is 'the' source to use.

And spending time investigating objects with the help of the Reference.

And looking and trying to understand existing scripts from this community and blog posts.

Using Onyx to check how the vSphere client does it.

You absolutely need an editor/debugger that has a very good object viewer.

I use PowerShellPlus from Idera, but there are others.

Another handy trick is to use the Format-Custom cmdlet with sufficient Depth.

in this book there is a chapter that shows you how start working with these SDK objects in PowerCLI.

For state info it is useful to browse the different enumerations in the SDK Reference.

There is no single source, you just need to invest some time Smiley Wink


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
SuperSpike
Contributor
Contributor

Fair enough. Could you possible point me in the direction of where to find the dvSwitch's pNIC teaming/failover policy? Since this setting is for a port group and not actually a dvSwitch, I would guess it's somewhere under Data Object DVSPort...something. I haven't been able to find it in the SDK, though. I suppose I would need to enumerate every port group in each dvSwitch?

So for example, when looking at my dvSwitch uplinks, I want to get the up/down state (which I'm now getting with .LinkUp), but I'd also like to know if the NIC is in an active, standby, or unused state. It's easy to get this with a standard switch using the ActiveNic and StandbyNIC properties of the HostNicOrderPolicy data object, but I haven't been able to find a similar property or data object for a dvSwitch port group.

@Virtual_EZ
0 Kudos
RvdNieuwendijk
Leadership
Leadership

You can find the distributed virtual switches teaming policies with:

Get-VirtualSwitch -Distributed | ForEach-Object {
  $dvSwitch = $_
  $dvSwitch | Get-VirtualPortGroup | Where-Object {$_.Name -like "*DVUplinks*"} | ForEach-Object {
    $UplinkPortGroup = $_
    $Report = "" | Select-Object -Property Datacenter,VirtualSwitch,UplinkPortGroup,UplinkTeamingPolicy
    $Report.Datacenter = (Get-View $dvSwitch.Datacenter).Name
    $Report.VirtualSwitch = $dvSwitch.Name
    $Report.UplinkPortgroup = $UplinkPortGroup.Name
    $Report.UplinkTeamingPolicy = $UplinkPortGroup.ExtensionData.Config.DefaultPortConfig.UplinkTeamingPolicy.Policy.Value
    $Report
  }
}


Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
LucD
Leadership
Leadership

You start with the DistributedVirtualSwitch object.

From there you select the config property. That brings you to the DVSConfigInfo object.

In there you will see the uplinkPortgroup property. That is an array of MoRefs (or pointers) to the portgroups defined on the dvSwitch.

You can use the Get-View cmdlet to retrieve the DistributedVirtualPortgroup objects but you could also have used the Get-VirtualPortGroup cmdlet to start with.

Get-VirtualSwitch -Distributed -Name "dvSw"  | Get-VirtualPortGroup

In the object(s) returned by that last cmdlet you get a mapping to the corresponding DistributedVirtualPortgroup object through the EXtensiondata property.

What you need to know is that the PNics are all connected to a special portgroup.

This special portgroup is named with the following naming convention <dvSwitch-Name>-DVUplinks-<MoRef.value>.

Once you have that special portgroup, you follow the config.defaultPortConfig properties.

At that point you you have arrived at the DVPortSetting object.

You will notice that this specific object is extended by the VMwareDVSPortSetting object. In other words the dvSwitch provided by VMware uses extra properties on top of the default dvSwitch objects. As a sidenote, the Nexus switch uses again other extensions, but afaik, these are not made public.

Now follow the uplinkTeamingPolicy.policy.value path through the objects.

The allowed values are defined in the DistributedVirtualSwitchNicTeamingPolicyMode enumeration.

So with a bit of background knowledge about dvSwitches and knowing your way around the SDK Reference, this is all rather straightforward Smiley Wink


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Wolvvi
Contributor
Contributor

Add some additional info:

$report = @()

foreach($sw in (Get-VirtualSwitch -Distributed)){

    $uuid = $sw.ExtensionData.Summary.Uuid

    $sw.ExtensionData.Config.Host | %{

        $portStates = $sw.ExtensionData.FetchDVPorts($null)

        $esx = Get-View $_.Config.Host

        $netSys = Get-View $esx.ConfigManager.NetworkSystem

        $netSys.NetworkConfig.ProxySwitch | where {$_.Uuid -eq $uuid} | %{

            foreach($pnicSpec in $_.Spec.Backing.PnicSpec){

                $row = "" | Select Host,dvSwitch,PNic,PortLinkUp,DvUplink,confvlans,SpeedLink,DuplexLink

                $row.Host = $esx.Name

                $row.dvSwitch = $sw.Name

                $row.PNic = $pnicSPec.PnicDevice

                $row.PortLinkUp = ($portStates | where{$_.Key -eq $pnicSPec.UplinkPortKey}).State.RunTimeInfo.LinkUp

                $row.DvUplink =  ($portStates | where{$_.Key -eq $pnicSPec.UplinkPortKey}).config.Name

                $row.confvlans = (($portStates | where{$_.Key -eq $pnicSPec.UplinkPortKey}).State.RuntimeInfo.vlanids | ft -HideTableHeaders |out-string).TrimStart().TrimEnd()

                $row.SpeedLink = ($netSys.NetworkConfig.pnic | where {$_.Device -eq $pnicSPec.PnicDevice }).Spec.Linkspeed.SpeedMb

                $row.DuplexLink = ($netSys.NetworkConfig.pnic | where {$_.Device -eq $pnicSPec.PnicDevice }).Spec.Linkspeed.Duplex

                $report += $row

            }

        }

    }

}

$report

jratter
Contributor
Contributor

Is anyway to modify this so that I can specify the hostname, because as is it just reports on connected hosts, but what if I want to specify the host name?

 

 

0 Kudos
LucD
Leadership
Leadership

You can add a Where-clause when the script is fetching the ESXi nodes

$esx = Get-View $_.Config.Host | where{$_.Name -eq 'Your-ESXi-node'}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jratter
Contributor
Contributor

I will try that thank you.

0 Kudos
jratter
Contributor
Contributor

That didn't work.

Tags (1)
0 Kudos
LucD
Leadership
Leadership

Did you check what is in $esx?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos