vMarkusK1985
Expert
Expert

Report VMHost, VMNIC, DVS and CDP info

Jump to solution

Hello,

I tried to create a Report wir these Information:

Clustername, Hostname, VMNIC, PCI, DVS, CDP_Port, CDP_Device, CDP_Address  

Thats the actual script with a modified version of LucD​ `s Get-CDP Function:   $obj.

function Get-CDPDetails {

<#

    .DESCRIPTION

    .Example

    .PARAMETER

#Requires PS -Version 4.0

#Requires -Modules VMware.VimAutomation.Core, @{ModuleName="VMware.VimAutomation.Core";ModuleVersion="6.3.0.0"}

#>

[CmdletBinding()]

param(

    [Parameter(Mandatory=$True, ValueFromPipeline=$False, Position=0)]

        [String] $Clustername

)

Begin {

    $Validate = $True

    if (($myCluster = Get-Cluster -Name $Clustername).count -lt 1) {

       $Validate = $False

       thow "No Cluster '$myCluster' found"

    }

    function Get-CDP ($VMhost, $VMhostDVS, $VMhostDC){

        $objReport = @()

        $VMhost| %{Get-View $_.ID} |

        %{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} |

        %{ foreach($physnic in $_.NetworkInfo.Pnic){

    

            $obj = "" | Select-Object Clustername,Hostname,VMNIC,PCI,DVS,CDP_Port,CDP_Device,CDP_Address  

    

            $pnicInfo = $_.QueryNetworkHint($physnic.Device)

            foreach($hint in $pnicInfo){

              $obj.Clustername = $VMhost.parent.name

              $obj.Hostname = $esxname

              $obj.VMNIC = $physnic.Device

              $obj.PCI = $physnic.PCI

              $obj.DVS = ($VMhostDVS | where {$_.ExtensionData.Config.Host.Config.Host -eq $VMhost.ExtensionData.MoRef -and $_.Datacenter.name -eq $VMhostDC.name -and $_.ExtensionData.Config.Host.Config.Backing.PnicSpec.PnicDevice -eq $physnic.Device}).name

              if( $hint.ConnectedSwitchPort ) {

                $obj.CDP_Port = $hint.ConnectedSwitchPort.PortId

              } else {

                $obj.CDP_Port = "No CDP information available."

              }

              $obj.CDP_Device = $hint.ConnectedSwitchPort.DevId

              $obj.CDP_Address = $hint.ConnectedSwitchPort.Address

 

            }

            $objReport += $obj

          }

        }

        $objReport

    }

 

}

Process {

    $MyView = @()

    if ($Validate -eq $True) {

 

        foreach ($myHost in ($myCluster | Get-VMHost)) {

        $mydvs = $myhost | Get-VirtualSwitch -Distributed

        $mydc = $myhost | Get-Datacenter

        $CDP = Get-CDP $myHost $mydvs $mydc

        $MyView += $CDP       

  }

        

       $MyView | Sort Clustername, Hostname, VMNIC

    }

    }

}

The Report itself works except one limitation: If you have a large Environment with duplicate ESXi Host MoRef IDs the DVS Field has multiple values.

get-cdpdetails.png

The Issue is by Design because I filter on the MoRef in the DVS Object...

Any suggestions how to filter more efficient or maybe start completely different?

Kind Regards,

Markus

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
vMarkusK1985
Expert
Expert

OK, i was way to complex. Thats the modified function:

function Get-CDPDetails {

<#

    .NOTES

    ===========================================================================

    Created by: Markus Kraus

    Twitter: @VMarkus_K

    Private Blog: mycloudrevolution.com

    ===========================================================================

    Changelog: 

    2017.02 ver 1.0 Base Release 

    ===========================================================================

    External Code Sources:

    ===========================================================================

    Tested Against Environment:

    vSphere Version: ESXi 5.5 U2, ESXi 6.5

    PowerCLI Version: PowerCLI 6.3 R1, PowerCLI 6.5 R1

    PowerShell Version: 4.0, 5.0

    OS Version: Windows 8.1, Server 2012 R2

    Keyword: ESXi, NTP, SSH, Syslog, SATP,

    ===========================================================================

    .DESCRIPTION

    .Example

    .PARAMETER

#Requires PS -Version 4.0

#Requires -Modules VMware.VimAutomation.Core, @{ModuleName="VMware.VimAutomation.Core";ModuleVersion="6.3.0.0"}

#>

[CmdletBinding()]

param(

    [Parameter(Mandatory=$True, ValueFromPipeline=$False, Position=0)]

        [String] $Clustername

       

)

Begin {

    $Validate = $True

    if (($myCluster = Get-Cluster -Name $Clustername).count -lt 1) {

       $Validate = $False

       thow "No Cluster '$myCluster' found"

    }

    function Get-CDP ($VMhost){

        $VMhostProxySwitch = $VMhost.NetworkInfo.ExtensionData.ProxySwitch

        $objReport = @()

        $VMhost| %{Get-View $_.ID} |

        %{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} |

        %{ foreach($physnic in $_.NetworkInfo.Pnic){

    

            $obj = "" | Select-Object Clustername,Hostname,VMNIC,PCI,DVS,CDP_Port,CDP_Device,CDP_Address  

    

            $pnicInfo = $_.QueryNetworkHint($physnic.Device)

            foreach($hint in $pnicInfo){

              $obj.Clustername = $VMhost.parent.name

              $obj.Hostname = $esxname

              $obj.VMNIC = $physnic.Device

              $obj.PCI = $physnic.PCI

              $obj.DVS = ($VMhostProxySwitch | where {$_.Spec.Backing.PnicSpec.PnicDevice -eq $physnic.Device}).DvsName

              if( $hint.ConnectedSwitchPort ) {

                $obj.CDP_Port = $hint.ConnectedSwitchPort.PortId

              } else {

                $obj.CDP_Port = "No CDP information available."

              }

              $obj.CDP_Device = $hint.ConnectedSwitchPort.DevId

              $obj.CDP_Address = $hint.ConnectedSwitchPort.Address

 

          

            }

            $objReport += $obj

          }

        }

        $objReport

    }

 

}

Process {

    $MyView = @()

    if ($Validate -eq $True) {

 

        foreach ($myHost in ($myCluster | Get-VMHost)) {

        $CDP = Get-CDP $myHost

        $MyView += $CDP       

  }

       

        

       $MyView | Sort Clustername, Hostname, VMNIC

    }

    }

}

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK

View solution in original post

0 Kudos
1 Reply
vMarkusK1985
Expert
Expert

OK, i was way to complex. Thats the modified function:

function Get-CDPDetails {

<#

    .NOTES

    ===========================================================================

    Created by: Markus Kraus

    Twitter: @VMarkus_K

    Private Blog: mycloudrevolution.com

    ===========================================================================

    Changelog: 

    2017.02 ver 1.0 Base Release 

    ===========================================================================

    External Code Sources:

    ===========================================================================

    Tested Against Environment:

    vSphere Version: ESXi 5.5 U2, ESXi 6.5

    PowerCLI Version: PowerCLI 6.3 R1, PowerCLI 6.5 R1

    PowerShell Version: 4.0, 5.0

    OS Version: Windows 8.1, Server 2012 R2

    Keyword: ESXi, NTP, SSH, Syslog, SATP,

    ===========================================================================

    .DESCRIPTION

    .Example

    .PARAMETER

#Requires PS -Version 4.0

#Requires -Modules VMware.VimAutomation.Core, @{ModuleName="VMware.VimAutomation.Core";ModuleVersion="6.3.0.0"}

#>

[CmdletBinding()]

param(

    [Parameter(Mandatory=$True, ValueFromPipeline=$False, Position=0)]

        [String] $Clustername

       

)

Begin {

    $Validate = $True

    if (($myCluster = Get-Cluster -Name $Clustername).count -lt 1) {

       $Validate = $False

       thow "No Cluster '$myCluster' found"

    }

    function Get-CDP ($VMhost){

        $VMhostProxySwitch = $VMhost.NetworkInfo.ExtensionData.ProxySwitch

        $objReport = @()

        $VMhost| %{Get-View $_.ID} |

        %{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} |

        %{ foreach($physnic in $_.NetworkInfo.Pnic){

    

            $obj = "" | Select-Object Clustername,Hostname,VMNIC,PCI,DVS,CDP_Port,CDP_Device,CDP_Address  

    

            $pnicInfo = $_.QueryNetworkHint($physnic.Device)

            foreach($hint in $pnicInfo){

              $obj.Clustername = $VMhost.parent.name

              $obj.Hostname = $esxname

              $obj.VMNIC = $physnic.Device

              $obj.PCI = $physnic.PCI

              $obj.DVS = ($VMhostProxySwitch | where {$_.Spec.Backing.PnicSpec.PnicDevice -eq $physnic.Device}).DvsName

              if( $hint.ConnectedSwitchPort ) {

                $obj.CDP_Port = $hint.ConnectedSwitchPort.PortId

              } else {

                $obj.CDP_Port = "No CDP information available."

              }

              $obj.CDP_Device = $hint.ConnectedSwitchPort.DevId

              $obj.CDP_Address = $hint.ConnectedSwitchPort.Address

 

          

            }

            $objReport += $obj

          }

        }

        $objReport

    }

 

}

Process {

    $MyView = @()

    if ($Validate -eq $True) {

 

        foreach ($myHost in ($myCluster | Get-VMHost)) {

        $CDP = Get-CDP $myHost

        $MyView += $CDP       

  }

       

        

       $MyView | Sort Clustername, Hostname, VMNIC

    }

    }

}

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK

View solution in original post

0 Kudos