Automation

 View Only
Expand all | Collapse all

ESX host per-vSwitch NIC status report - excluding unpatched NICs

vmtraining

vmtrainingNov 05, 2019 03:39 PM

  • 1.  ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Nov 05, 2019 03:08 PM

    I'm writing a small script to check the ESX host network connections and flag up if a NIC connection goes down.

    A small issue is that the physical hosts have 8-10 NICs so I want to specify which NICs are connected for each vSwitch.

    I've got this far, but

    (a) It seems overly complex at this stage

    (b) I have an error in the vSwitch output I'm trying to fathom.

    Anyone with a better knowledge of extended data views have a better suggestion?

    #List of vDS and NICs in use by ESX hosts in order to exclude unpatched NICS

    $vDSw1 = 'SRV-PRI-DSwitch1'

    $vDSw2 = 'SRV-PRI-DSwitch2'

    $UsedNICsSw1 = "vmnic0|vmnic1|vmnic4|vmnic6"

    $UsedNICsSw2 = "vmnic0|vmnic5|vmnic7|vmnic8"

    $VMH = Get-VMHost | sort Name

    $SwitchAssignments = ForEach($vds in Get-VDSwitch){

        Get-View -Id $vds.ExtensionData.Config.host.config.host -Property Name | Select Name, @{N='vDSwitch';E={$vds.Name}} | Sort-Object Name

    }

    ForEach ($VMHost in $VMH) {

        $DvSwitchInfo = Get-VDSwitch -VMHost $VMHost

        If ($DvSwitchInfo -ne $null) {

            $DvSwitchHost = $DvSwitchInfo.ExtensionData.Config.Host

            $DVNic = $DvSwitchHost.config.backing.PnicSpec.PnicDevice 

        } 

        ForEach($pNic in $VMHost.ExtensionData.Config.Network.Pnic){

            If ($DVNic -contains $pNic.device) { 

                $vSwitch = $DVswitchInfo | select-object -ExpandProperty Name 

            } 

            Else

                $vSwitchname = $VMHost | Get-VirtualSwitch -Standard | Where-object {$_.nic -eq $pNic.Device

                $vSwitch = $vSwitchname.name 

            }

            If($pNic.LinkSpeed -eq $null){

                $LinkStatus = 'Down'

            }

            Else {

                $LinkStatus = $pNic.LinkSpeed.SpeedMb

            }

            $Details = [pscustomobject]@{EsxName = $VMHost.Name;VMNic = $pNIC.device;vSwitch = $vSwitch; Status = $LinkStatus}  

            If ( ($SwitchAssignments | ? { $_.name -eq $VMHost.name}).vDSwitch -eq $vDSw1) {

                $Details | Where-Object {$_.Status -eq "down"} |? {$_ -match $UsedNICsSW1}

            }

            If ( ($SwitchAssignments | ? { $_.name -eq $VMHost.name}).vDSwitch -eq $vDSw2) {

                $Details | Where-Object {$_.Status -eq "down"} |? {$_ -match $UsedNICsSW2}

            }

        }

    }  



  • 2.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Broadcom Employee
    Posted Nov 05, 2019 03:39 PM

    Moderator note: Moved to PowerCLI



  • 3.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Nov 05, 2019 04:29 PM

    Can you try something like this?
    It will only show the pNics that are connected to a VDS and that are down.

    Get-View -ViewType DistributedVirtualSwitch -PipelineVariable vds |

      ForEach-Object -Process {

        $vds.Config.Host |

          ForEach-Object -Process {

            $pNIcs = $_.Config.Backing.PnicSpec.pnicdevice

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

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

            $netsys.NetworkInfo.Pnic | where { $pNics -contains $_.Device -and $_.LinkSpeed -eq $null } |

            Select @{ N = 'VDS'; E = { $vds.Name } },

              @{N = 'VMHost'; E = { $esx.Name } },

              @{N = 'pNic Down'; E = { $_.Device } }

        }

    }



  • 4.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Nov 06, 2019 07:15 AM

    Thanks Luc, however I do need the standard vSwitch NICs as well because they we have NICs attached there on on another network, which is why I was trying from the ESX host standpoint. This has given me a start though I'll see if I can figure it.



  • 5.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs
    Best Answer

    Posted Nov 06, 2019 10:28 AM

    Try like this

    Get-View -ViewType HostSystem -PipelineVariable esx |

    ForEach-Object -Process {

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

        foreach($vss in $netSys.NetworkInfo.Vswitch){

            foreach($nic in $vss.Pnic){

                $netSys.NetworkInfo.Pnic | where{$_.Key -eq $nic -and $_.LinkSpeed -eq $null} |

                Select @{N='VMHost';E={$esx.Name}},

                    @{N='Switch';E={$vss.Name}},

                    @{N='SwitchType';E={'VSS'}},

                    @{N='pNic Down';E={$_.Device}}

            }

        }

        foreach($vds in $netSys.NetworkInfo.ProxySwitch){

            foreach($nic in $vds.Spec.Backing.PnicSpec){

                $netSys.NetworkInfo.Pnic | where{$_.Device -eq $nic.PnicDevice -and $_.LinkSpeed -eq $null} |

                Select @{N='VMHost';E={$esx.Name}},

                    @{N='Switch';E={$vds.DvsName}},

                    @{N='SwitchType';E={'VDS'}},

                    @{N='pNic Down';E={$_.Device}}

            }

        }

    }



  • 6.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Nov 06, 2019 12:19 PM

    Excellent thanks, I can work this into my report easily.



  • 7.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Apr 22, 2020 05:38 PM

    LucD

    so...

    do I change the HostSystem with my esx name and its will show the NIcs state on vds?

    Get-View -ViewType HostSystem -PipelineVariable esx |

    ForEach-Object -Process {

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

        foreach($vss in $netSys.NetworkInfo.Vswitch){

            foreach($nic in $vss.Pnic){

                $netSys.NetworkInfo.Pnic | where{$_.Key -eq $nic -and $_.LinkSpeed -eq $null} |

                Select @{N='VMHost';E={$esx.Name}},

                    @{N='Switch';E={$vss.Name}},

                    @{N='SwitchType';E={'VSS'}},

                    @{N='pNic Down';E={$_.Device}}

            }

        }

        foreach($vds in $netSys.NetworkInfo.ProxySwitch){

            foreach($nic in $vds.Spec.Backing.PnicSpec){

                $netSys.NetworkInfo.Pnic | where{$_.Device -eq $nic.PnicDevice -and $_.LinkSpeed -eq $null} |

                Select @{N='VMHost';E={$esx.Name}},

                    @{N='Switch';E={$vds.DvsName}},

                    @{N='SwitchType';E={'VDS'}},

                    @{N='pNic Down';E={$_.Device}}

            }

        }

    }

    Blog: htt



  • 8.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Apr 22, 2020 06:04 PM

    No, this is using the Get-View cmdlet, use the Filter parameter.

    Get-View -ViewType HostSystem -PipelineVariable esx -Filter @{Name=$yourHostname}


    Also remove the the test on the LinkSpeed otherwise you will only get pNIC that are down/not connected.

    This will give the result for VSS and VDS



  • 9.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Apr 22, 2020 06:22 PM

    I just want to get the state of the vmnics on vds and std . Could you please help me to modifiy your script to get the list of vmnics up and down? (vds and sw) , I dont have too experience on powercli scripts



  • 10.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Apr 22, 2020 06:30 PM

    Try like this

    $esxName = 'MyEsx'

    Get-View -ViewType HostSystem -Filter @{Name = $esxName } -PipelineVariable esx |

    ForEach-Object -Process {

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

        foreach ($vss in $netSys.NetworkInfo.Vswitch) {

            foreach ($nic in $vss.Pnic) {

                $netSys.NetworkInfo.Pnic | Where-Object { $_.Key -eq $nic } |

                Select-Object @{N = 'VMHost'; E = { $esx.Name } },

                @{N = 'Switch'; E = { $vss.Name } },

                @{N = 'SwitchType'; E = { 'VSS' } },

                @{N = 'pNic'; E = { $_.Device } },

                @{N = 'SpeedMB'; E = { $_.LinkSpeed.SpeedMB } },

                @{N = 'Duplex'; E = { $_.LinkSpeed.Duplex } }

            }

        }

        foreach ($vds in $netSys.NetworkInfo.ProxySwitch) {

            foreach ($nic in $vds.Spec.Backing.PnicSpec) {

                $netSys.NetworkInfo.Pnic | Where-Object { $_.Device -eq $nic.PnicDevice } |

                Select-Object @{N = 'VMHost'; E = { $esx.Name } },

                @{N = 'Switch'; E = { $vds.DvsName } },

                @{N = 'SwitchType'; E = { 'VDS' } },

                @{N = 'pNic'; E = { $_.Device } },

                @{N = 'SpeedMB'; E = { $_.LinkSpeed.SpeedMB } },

                @{N = 'Duplex'; E = { $_.LinkSpeed.Duplex } }

            }

        }

    }



  • 11.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Apr 22, 2020 07:03 PM

    you rock! :smileywink: , thanks.. One question more:

    In my example below the host 4 the "SpeedMB" and "Duplex"   are empty because the vmnic is DOWN. Is there any way to show the state down on this field and not empty? I need to create an automation script to avoid incidents

    VMHost     : host1

    Switch     : vSwitch0

    SwitchType : VSS

    pNic       : vmnic5

    SpeedMB    : 10000

    Duplex     : True

    VMHost     :  host2

    Switch     : vSwitch0

    SwitchType : VSS

    pNic       : vmnic0

    SpeedMB    : 10000

    Duplex     : True

    VMHost     :  host3

    Switch     : vSwitchUSB0

    SwitchType : VSS

    pNic       : vusb0

    SpeedMB    : 100

    Duplex     : True

    VMHost     :  host4

    Switch     : dvSwitch-

    SwitchType : VDS

    pNic       : vmnic1

    SpeedMB    :

    Duplex     :

    VMHost     : host5

    Switch     : dvSwitch-

    SwitchType : VDS

    pNic       : vmnic4

    SpeedMB    : 10000

    Duplex     : True



  • 12.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Apr 22, 2020 07:11 PM

    Try like this

    $esxName = 'MyEsx'

    Get-View -ViewType HostSystem -Filter @{Name=$esxName} -PipelineVariable esx |

    ForEach-Object -Process {

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

        foreach($vss in $netSys.NetworkInfo.Vswitch){

            foreach($nic in $vss.Pnic){

                $netSys.NetworkInfo.Pnic | where{$_.Key -eq $nic} |

                Select @{N='VMHost';E={$esx.Name}},

                    @{N='Switch';E={$vss.Name}},

                    @{N='SwitchType';E={'VSS'}},

                    @{N='pNic';E={$_.Device}},

                    @{N='SpeedMB';E={$_.LinkSpeed.SpeedMB}},

                    @{N='Duplex';E={$_.LinkSpeed.Duplex}},

                    @{N='Status';E={if($_.LinkSpeed -eq $null){'Down'}else{'Up'}}}

            }

        }

        foreach($vds in $netSys.NetworkInfo.ProxySwitch){

            foreach($nic in $vds.Spec.Backing.PnicSpec){

                $netSys.NetworkInfo.Pnic | where{$_.Device -eq $nic.PnicDevice} |

                Select @{N='VMHost';E={$esx.Name}},

                    @{N='Switch';E={$vds.DvsName}},

                    @{N='SwitchType';E={'VDS'}},

                    @{N='pNic';E={$_.Device}},

                    @{N='SpeedMB';E={$_.LinkSpeed.SpeedMB}},

                    @{N='Duplex';E={$_.LinkSpeed.Duplex}},

                    @{N='Status';E={if($_.LinkSpeed -eq $null){'Down'}else{'Up'}}}

            }

        }

    }



  • 13.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Apr 22, 2020 07:21 PM

    Awesome!! I got the state fine! .... its possible to add a refresh the host's network system on script before to list the state of the vmnics?

    Thanks in advance!



  • 14.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Apr 22, 2020 07:27 PM

    Just add the line after the initialisation of the $netSys object

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

        $netSys.RefreshNetworkSystem()



  • 15.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Apr 22, 2020 07:33 PM

    Thanks!!! :smileywink: I got what I wanted.



  • 16.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Apr 23, 2020 12:13 AM

    Does performing the network refresh during the business hours can cause performance impact or any outage?



  • 17.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Apr 23, 2020 06:27 AM

    Any action on the vCenter has potential impact.
    It all depends on the size and complexity of your environment, but in my personal experience this method has not caused an extreme additional load on any of my vCenters.

    But your mileage may vary.



  • 18.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Apr 23, 2020 07:08 AM

    That's great. many thanks Luc for your clarification.



  • 19.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Jul 25, 2020 05:30 PM

    Hi LuCD,

    i have tried the below and got error. Can you please help to advise

    Get-View : 7/25/2020 5:26:42 PM Get-View                Invalid object specified for the Filter parameter  - 'Hashtable{

    String,

    null}'. Filter accepts objects of type  'Hashtable{String, String}'.

    At C:\tsm_images\vmnic1.ps1:1 char:1

    + Get-View -ViewType HostSystem -Filter @{Name=$esxName} -PipelineVaria ...

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidArgument: (System.Collections.Hashtable:Hashtable) [Get-View], VimException

        + FullyQualifiedErrorId : Core_GetVIView_TryGetFilterParam_InvalidValue,VMware.VimAutomation.ViCore.Cmdlets.Comman

       ds.DotNetInterop.GetVIView

    i was looking to get the Physical network state of all the ESXi host in the vCenter with status (up/down) with switch name.

    Regards

    Narayanan.



  • 20.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Jul 25, 2020 05:52 PM

    The variable $esxName seems to be empty, hence the null error



  • 21.  RE: ESX host per-vSwitch NIC status report - excluding unpatched NICs

    Posted Jul 25, 2020 06:35 PM

    Thanks LuCD for the quick reply.

    i have removed the first line "$esxName = 'MyEsx'" as i require all the esxi hosts in the vCenter,  so i have connected the vCenter server directly and run the below

    Get-View -ViewType HostSystem -Filter @{Name=$esxName} -PipelineVariable esx |

    ForEach-Object -Process {

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

        foreach($vss in $netSys.NetworkInfo.Vswitch){

            foreach($nic in $vss.Pnic){

                $netSys.NetworkInfo.Pnic | where{$_.Key -eq $nic} |

                Select @{N='VMHost';E={$esx.Name}},

                    @{N='Switch';E={$vss.Name}},

                    @{N='SwitchType';E={'VSS'}},

                    @{N='pNic';E={$_.Device}},

                    @{N='SpeedMB';E={$_.LinkSpeed.SpeedMB}},

                    @{N='Duplex';E={$_.LinkSpeed.Duplex}},

                    @{N='Status';E={if($_.LinkSpeed -eq $null){'Down'}else{'Up'}}}

            }

        }

        foreach($vds in $netSys.NetworkInfo.ProxySwitch){

            foreach($nic in $vds.Spec.Backing.PnicSpec){

                $netSys.NetworkInfo.Pnic | where{$_.Device -eq $nic.PnicDevice} |

                Select @{N='VMHost';E={$esx.Name}},

                    @{N='Switch';E={$vds.DvsName}},

                    @{N='SwitchType';E={'VDS'}},

                    @{N='pNic';E={$_.Device}},

                    @{N='SpeedMB';E={$_.LinkSpeed.SpeedMB}},

                    @{N='Duplex';E={$_.LinkSpeed.Duplex}},

                    @{N='Status';E={if($_.LinkSpeed -eq $null){'Down'}else{'Up'}}}

            }

        }

    }

    It would be very helpful how how to get the network status report for all the esxi hosts in the vCenter server with formatted table.

    I have tried below using esxcli v2 in simple format but didnt get the host name and formatted output what exactly i am looking.

    $EsxHosts = Get-VMHost

    foreach($EsxHost in $EsxHosts){

      $esxcli = Get-VMHost $EsxHost | Get-EsxCli -V2

      $esxcli.network.nic.list.Invoke()

    }

    Thank you

    Regards

    Narayanan.