From 76d01e7eb817704a128109afe2dc73d4ba207d04 Mon Sep 17 00:00:00 2001 From: Markus Kraus Date: Wed, 29 Apr 2020 23:04:18 +0200 Subject: [PATCH 1/9] Create Get-UplinkDetail.ps1 --- Scripts/Get-UplinkDetail.ps1 | 147 +++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 Scripts/Get-UplinkDetail.ps1 diff --git a/Scripts/Get-UplinkDetail.ps1 b/Scripts/Get-UplinkDetail.ps1 new file mode 100644 index 0000000..6bdbb46 --- /dev/null +++ b/Scripts/Get-UplinkDetail.ps1 @@ -0,0 +1,147 @@ +function Get-UplinkDetails { +<# + .NOTES + =========================================================================== + Created by: Markus Kraus + =========================================================================== + Changelog: + 2017.03 ver 1.0 Base Release + 2020.03 ver 1.1 Add LLDP Support + =========================================================================== + External Code Sources: + Get-CDP Version from @LucD22 + https://communities.vmware.com/thread/319553 + + LLDP PowerCLI Tweak + https://tech.zsoldier.com/2018/05/vmware-get-cdplldp-info-from.html + =========================================================================== + Tested Against Environment: + vSphere Version: vSphere 6.7 U3 + PowerCLI Version: PowerCLI 11.5 + PowerShell Version: 5.1 + OS Version: Server 2016 + Keyword: ESXi, Network, CDP, LLDP, VDS, vSwitch, VMNIC + =========================================================================== + + .DESCRIPTION + This Function collects detailed informations about your ESXi Host connections to pSwitch and VDS / vSwitch. + LLDP Informations might only be available when uplinks are connected to a VDS. + + .Example + Get-VMHost -Name MyHost | Get-UplinkDetails -Type LLDP | Where-Object {$_.VDS -ne "-No Backing-"} | Format-Table -AutoSize + + .Example + Get-VMHost -Name MyHost | Get-UplinkDetails -Type CDP | Where-Object {$_.VDS -ne "-No Backing-"} | Format-Table -AutoSize + + .Example + Get-Cluster -Name MyCluster | Get-VMHost | Get-UplinkDetails -Type LLDP | Format-Table -AutoSize + + .Example + Get-Cluster -Name MyCluster | Get-VMHost | Get-UplinkDetails -Type CDP | Format-Table -AutoSize + + .PARAMETER myHosts + Hosts to process + + +#Requires PS -Version 5.0 +#Requires -Modules VMware.VimAutomation.Core, @{ModuleName="VMware.VimAutomation.Core";ModuleVersion="6.3.0.0"} +#> + +[CmdletBinding()] +param( + [Parameter(Mandatory=$True, ValueFromPipeline=$True, Position=0, HelpMessage = "Hosts to process")] + [ValidateNotNullorEmpty()] + [VMware.VimAutomation.ViCore.Impl.V1.Inventory.InventoryItemImpl[]] $myHosts, + [Parameter(Mandatory=$True, ValueFromPipeline=$False, Position=1, HelpMessage = "Type of infos to collect (CDP / LLDP)")] + [ValidateSet("CDP","LLDP")] + [String] $Type + +) + +Begin { + + + function Get-Info ($VMhost){ + $VMhostProxySwitch = $VMhost.NetworkInfo.ExtensionData.ProxySwitch + $VMhostSwitch = $VMhost.NetworkInfo.VirtualSwitch + + $objReport = @() + $VMhost| %{Get-View $_.ID} | + %{ Get-View $_.ConfigManager.NetworkSystem} | + %{ foreach($physnic in $_.NetworkInfo.Pnic){ + + if($Type -eq "CDP"){ + $obj = "" | Select-Object ClusterName,HostName,vmnic,PCI,MAC,VDS,vSwitch,CDP_Port,CDP_Device,CDP_Address + } + elseif($Type -eq "LLDP"){ + $obj = "" | Select-Object ClusterName,HostName,vmnic,PCI,MAC,VDS,vSwitch,LLDP_Port,LLDP_Chassis,LLDP_SystemName + } + else{ + Throw "Invalide Type" + } + + $pnicInfo = $_.QueryNetworkHint($physnic.Device) + foreach($hint in $pnicInfo){ + $obj.ClusterName = $VMhost.parent.name + $obj.HostName = $VMhost.name + $obj.vmnic = $physnic.Device + $obj.PCI = $physnic.PCI + $obj.MAC = $physnic.Mac + if ($backing = ($VMhostProxySwitch | where {$_.Spec.Backing.PnicSpec.PnicDevice -eq $physnic.Device})) { + $obj.VDS = $backing.DvsName + } else { + $obj.VDS = "-No Backing-" + } + if ($backing = ($VMhostSwitch | where {$_.Nic -eq $physnic.Device})) { + $obj.vSwitch = $backing.name + } else { + $obj.vSwitch = "-No Backing-" + } + if($Type -eq "CDP"){ + if( $hint.ConnectedSwitchPort ) { + $obj.CDP_Port = $hint.ConnectedSwitchPort.PortId + $obj.CDP_Device = $hint.ConnectedSwitchPort.DevId + $obj.CDP_Address = $hint.ConnectedSwitchPort.Address + } else { + $obj.CDP_Port = "-No Info-" + $obj.CDP_Device = "-No Info-" + $obj.CDP_Address = "-No Info-" + } + } + if($Type -eq "LLDP"){ + if( $hint.LldpInfo ) { + $obj.LLDP_Port = $hint.LldpInfo.PortId + $obj.LLDP_Chassis = $hint.LldpInfo.ChassisId + $obj.LLDP_SystemName = ($hint.LldpInfo.Parameter | where key -eq "System Name").Value + } else { + $obj.LLDP_Port = "-No Info-" + $obj.LLDP_Chassis = "-No Info-" + $obj.LLDP_SystemName = "-No Info-" + } + } + + + } + $objReport += $obj + } + } + $objReport + } + +} + +Process { + + $MyView = @() + + foreach ($myHost in $myHosts) { + + $Info = Get-Info $myHost + $MyView += $Info + + } + + $MyView | Sort-Object ClusterName, HostName, vmnic + + } +} From a918eacdadaf95fcd5d1e53852f2b8a54876016c Mon Sep 17 00:00:00 2001 From: Markus Kraus Date: Wed, 29 Apr 2020 23:04:44 +0200 Subject: [PATCH 2/9] Rename Get-UplinkDetail.ps1 to Get-UplinkDetails.ps1 --- Scripts/{Get-UplinkDetail.ps1 => Get-UplinkDetails.ps1} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Scripts/{Get-UplinkDetail.ps1 => Get-UplinkDetails.ps1} (100%) diff --git a/Scripts/Get-UplinkDetail.ps1 b/Scripts/Get-UplinkDetails.ps1 similarity index 100% rename from Scripts/Get-UplinkDetail.ps1 rename to Scripts/Get-UplinkDetails.ps1 From 3a4dada48d4586edd1f7701ba09c15d808a14d44 Mon Sep 17 00:00:00 2001 From: Markus Kraus Date: Thu, 7 May 2020 20:20:13 +0200 Subject: [PATCH 3/9] Rename Function --- ...t-UplinkDetails.ps1 => Get-VMHostUplinkDetails.ps1} | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) rename Scripts/{Get-UplinkDetails.ps1 => Get-VMHostUplinkDetails.ps1} (90%) diff --git a/Scripts/Get-UplinkDetails.ps1 b/Scripts/Get-VMHostUplinkDetails.ps1 similarity index 90% rename from Scripts/Get-UplinkDetails.ps1 rename to Scripts/Get-VMHostUplinkDetails.ps1 index 6bdbb46..9b47018 100644 --- a/Scripts/Get-UplinkDetails.ps1 +++ b/Scripts/Get-VMHostUplinkDetails.ps1 @@ -1,4 +1,4 @@ -function Get-UplinkDetails { +function Get-VMHostUplinkDetails { <# .NOTES =========================================================================== @@ -28,16 +28,16 @@ function Get-UplinkDetails { LLDP Informations might only be available when uplinks are connected to a VDS. .Example - Get-VMHost -Name MyHost | Get-UplinkDetails -Type LLDP | Where-Object {$_.VDS -ne "-No Backing-"} | Format-Table -AutoSize + Get-VMHost -Name MyHost | Get-VMHostUplinkDetails -Type LLDP | Where-Object {$_.VDS -ne "-No Backing-"} | Format-Table -AutoSize .Example - Get-VMHost -Name MyHost | Get-UplinkDetails -Type CDP | Where-Object {$_.VDS -ne "-No Backing-"} | Format-Table -AutoSize + Get-VMHost -Name MyHost | Get-VMHostUplinkDetails -Type CDP | Where-Object {$_.VDS -ne "-No Backing-"} | Format-Table -AutoSize .Example - Get-Cluster -Name MyCluster | Get-VMHost | Get-UplinkDetails -Type LLDP | Format-Table -AutoSize + Get-Cluster -Name MyCluster | Get-VMHost | Get-VMHostUplinkDetails -Type LLDP | Format-Table -AutoSize .Example - Get-Cluster -Name MyCluster | Get-VMHost | Get-UplinkDetails -Type CDP | Format-Table -AutoSize + Get-Cluster -Name MyCluster | Get-VMHost | Get-VMHostUplinkDetails -Type CDP | Format-Table -AutoSize .PARAMETER myHosts Hosts to process From be7f9827671b049ecb922fd69f9be582ceb42d0b Mon Sep 17 00:00:00 2001 From: Markus Kraus Date: Thu, 7 May 2020 20:25:40 +0200 Subject: [PATCH 4/9] Update Parameter --- Scripts/Get-VMHostUplinkDetails.ps1 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Scripts/Get-VMHostUplinkDetails.ps1 b/Scripts/Get-VMHostUplinkDetails.ps1 index 9b47018..a39c920 100644 --- a/Scripts/Get-VMHostUplinkDetails.ps1 +++ b/Scripts/Get-VMHostUplinkDetails.ps1 @@ -49,10 +49,11 @@ function Get-VMHostUplinkDetails { [CmdletBinding()] param( - [Parameter(Mandatory=$True, ValueFromPipeline=$True, Position=0, HelpMessage = "Hosts to process")] + [Parameter(Mandatory=$True, ValueFromPipeline=$True, Position=0, HelpMessage = "Specifies the hosts for which you want to retrieve the uplink details.")] [ValidateNotNullorEmpty()] - [VMware.VimAutomation.ViCore.Impl.V1.Inventory.InventoryItemImpl[]] $myHosts, - [Parameter(Mandatory=$True, ValueFromPipeline=$False, Position=1, HelpMessage = "Type of infos to collect (CDP / LLDP)")] + [Alias("myHosts")] + [VMware.VimAutomation.Types.VMHost[]] $VMHost, + [Parameter(Mandatory=$True, ValueFromPipeline=$False, Position=1, HelpMessage = "Type of infos you want to collect (CDP / LLDP)")] [ValidateSet("CDP","LLDP")] [String] $Type From e24187a954a52f7b6718a7106d8b093a9e4ed79d Mon Sep 17 00:00:00 2001 From: Markus Kraus Date: Thu, 7 May 2020 20:32:06 +0200 Subject: [PATCH 5/9] rename VMHost Parameter --- Scripts/Get-VMHostUplinkDetails.ps1 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Scripts/Get-VMHostUplinkDetails.ps1 b/Scripts/Get-VMHostUplinkDetails.ps1 index a39c920..ef843ec 100644 --- a/Scripts/Get-VMHostUplinkDetails.ps1 +++ b/Scripts/Get-VMHostUplinkDetails.ps1 @@ -62,12 +62,12 @@ param( Begin { - function Get-Info ($VMhost){ - $VMhostProxySwitch = $VMhost.NetworkInfo.ExtensionData.ProxySwitch - $VMhostSwitch = $VMhost.NetworkInfo.VirtualSwitch + function Get-Info ($VMhostToProcess){ + $VMhostProxySwitch = $VMhostToProcess.NetworkInfo.ExtensionData.ProxySwitch + $VMhostSwitch = $VMhostToProcess.NetworkInfo.VirtualSwitch $objReport = @() - $VMhost| %{Get-View $_.ID} | + $VMhostToProcess| %{Get-View $_.ID} | %{ Get-View $_.ConfigManager.NetworkSystem} | %{ foreach($physnic in $_.NetworkInfo.Pnic){ @@ -83,8 +83,8 @@ Begin { $pnicInfo = $_.QueryNetworkHint($physnic.Device) foreach($hint in $pnicInfo){ - $obj.ClusterName = $VMhost.parent.name - $obj.HostName = $VMhost.name + $obj.ClusterName = $VMhostToProcess.parent.name + $obj.HostName = $VMhostToProcess.name $obj.vmnic = $physnic.Device $obj.PCI = $physnic.PCI $obj.MAC = $physnic.Mac @@ -135,7 +135,7 @@ Process { $MyView = @() - foreach ($myHost in $myHosts) { + foreach ($myHost in $VMhost) { $Info = Get-Info $myHost $MyView += $Info From 228de51f25378d4a2fa57ebd8b03592d5502698d Mon Sep 17 00:00:00 2001 From: Markus Kraus Date: Thu, 7 May 2020 20:33:25 +0200 Subject: [PATCH 6/9] Fix code formatting --- Scripts/Get-VMHostUplinkDetails.ps1 | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Scripts/Get-VMHostUplinkDetails.ps1 b/Scripts/Get-VMHostUplinkDetails.ps1 index ef843ec..c5cd4c1 100644 --- a/Scripts/Get-VMHostUplinkDetails.ps1 +++ b/Scripts/Get-VMHostUplinkDetails.ps1 @@ -77,7 +77,7 @@ Begin { elseif($Type -eq "LLDP"){ $obj = "" | Select-Object ClusterName,HostName,vmnic,PCI,MAC,VDS,vSwitch,LLDP_Port,LLDP_Chassis,LLDP_SystemName } - else{ + else { Throw "Invalide Type" } @@ -90,12 +90,14 @@ Begin { $obj.MAC = $physnic.Mac if ($backing = ($VMhostProxySwitch | where {$_.Spec.Backing.PnicSpec.PnicDevice -eq $physnic.Device})) { $obj.VDS = $backing.DvsName - } else { + } + else { $obj.VDS = "-No Backing-" } if ($backing = ($VMhostSwitch | where {$_.Nic -eq $physnic.Device})) { $obj.vSwitch = $backing.name - } else { + } + else { $obj.vSwitch = "-No Backing-" } if($Type -eq "CDP"){ @@ -103,7 +105,8 @@ Begin { $obj.CDP_Port = $hint.ConnectedSwitchPort.PortId $obj.CDP_Device = $hint.ConnectedSwitchPort.DevId $obj.CDP_Address = $hint.ConnectedSwitchPort.Address - } else { + } + else { $obj.CDP_Port = "-No Info-" $obj.CDP_Device = "-No Info-" $obj.CDP_Address = "-No Info-" @@ -114,7 +117,8 @@ Begin { $obj.LLDP_Port = $hint.LldpInfo.PortId $obj.LLDP_Chassis = $hint.LldpInfo.ChassisId $obj.LLDP_SystemName = ($hint.LldpInfo.Parameter | where key -eq "System Name").Value - } else { + } + else { $obj.LLDP_Port = "-No Info-" $obj.LLDP_Chassis = "-No Info-" $obj.LLDP_SystemName = "-No Info-" From 0c3ff917d09fc93632b5ae99d8dc9720fe0c2663 Mon Sep 17 00:00:00 2001 From: Markus Kraus Date: Thu, 7 May 2020 20:34:28 +0200 Subject: [PATCH 7/9] Replace where with Where-Object --- Scripts/Get-VMHostUplinkDetails.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Scripts/Get-VMHostUplinkDetails.ps1 b/Scripts/Get-VMHostUplinkDetails.ps1 index c5cd4c1..05414df 100644 --- a/Scripts/Get-VMHostUplinkDetails.ps1 +++ b/Scripts/Get-VMHostUplinkDetails.ps1 @@ -88,13 +88,13 @@ Begin { $obj.vmnic = $physnic.Device $obj.PCI = $physnic.PCI $obj.MAC = $physnic.Mac - if ($backing = ($VMhostProxySwitch | where {$_.Spec.Backing.PnicSpec.PnicDevice -eq $physnic.Device})) { + if ($backing = ($VMhostProxySwitch | Where-Object {$_.Spec.Backing.PnicSpec.PnicDevice -eq $physnic.Device})) { $obj.VDS = $backing.DvsName } else { $obj.VDS = "-No Backing-" } - if ($backing = ($VMhostSwitch | where {$_.Nic -eq $physnic.Device})) { + if ($backing = ($VMhostSwitch | Where-Object {$_.Nic -eq $physnic.Device})) { $obj.vSwitch = $backing.name } else { @@ -116,7 +116,7 @@ Begin { if( $hint.LldpInfo ) { $obj.LLDP_Port = $hint.LldpInfo.PortId $obj.LLDP_Chassis = $hint.LldpInfo.ChassisId - $obj.LLDP_SystemName = ($hint.LldpInfo.Parameter | where key -eq "System Name").Value + $obj.LLDP_SystemName = ($hint.LldpInfo.Parameter | Where-Object key -eq "System Name").Value } else { $obj.LLDP_Port = "-No Info-" From f53c09ae036d951d432e8696aa7151126ebc897d Mon Sep 17 00:00:00 2001 From: Markus Kraus Date: Thu, 7 May 2020 20:35:44 +0200 Subject: [PATCH 8/9] Replace % with Foreach-Object. --- Scripts/Get-VMHostUplinkDetails.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Scripts/Get-VMHostUplinkDetails.ps1 b/Scripts/Get-VMHostUplinkDetails.ps1 index 05414df..899eb54 100644 --- a/Scripts/Get-VMHostUplinkDetails.ps1 +++ b/Scripts/Get-VMHostUplinkDetails.ps1 @@ -67,9 +67,9 @@ Begin { $VMhostSwitch = $VMhostToProcess.NetworkInfo.VirtualSwitch $objReport = @() - $VMhostToProcess| %{Get-View $_.ID} | - %{ Get-View $_.ConfigManager.NetworkSystem} | - %{ foreach($physnic in $_.NetworkInfo.Pnic){ + $VMhostToProcess| ForEach-Object{Get-View $_.ID} | + ForEach-Object{ Get-View $_.ConfigManager.NetworkSystem} | + ForEach-Object{ foreach($physnic in $_.NetworkInfo.Pnic){ if($Type -eq "CDP"){ $obj = "" | Select-Object ClusterName,HostName,vmnic,PCI,MAC,VDS,vSwitch,CDP_Port,CDP_Device,CDP_Address From ca6f6abeb7c36d79075da7fef2cf868f4bc1cec0 Mon Sep 17 00:00:00 2001 From: Markus Kraus Date: Thu, 7 May 2020 20:42:54 +0200 Subject: [PATCH 9/9] remove reporting array and sort --- Scripts/Get-VMHostUplinkDetails.ps1 | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/Scripts/Get-VMHostUplinkDetails.ps1 b/Scripts/Get-VMHostUplinkDetails.ps1 index 899eb54..9c0b25e 100644 --- a/Scripts/Get-VMHostUplinkDetails.ps1 +++ b/Scripts/Get-VMHostUplinkDetails.ps1 @@ -31,7 +31,7 @@ function Get-VMHostUplinkDetails { Get-VMHost -Name MyHost | Get-VMHostUplinkDetails -Type LLDP | Where-Object {$_.VDS -ne "-No Backing-"} | Format-Table -AutoSize .Example - Get-VMHost -Name MyHost | Get-VMHostUplinkDetails -Type CDP | Where-Object {$_.VDS -ne "-No Backing-"} | Format-Table -AutoSize + Get-VMHost -Name MyHost | Get-VMHostUplinkDetails -Type CDP | Where-Object {$_.VDS -ne "-No Backing-"} | Sort-Object ClusterName, HostName, vmnic | Format-Table -AutoSize .Example Get-Cluster -Name MyCluster | Get-VMHost | Get-VMHostUplinkDetails -Type LLDP | Format-Table -AutoSize @@ -137,16 +137,8 @@ Begin { Process { - $MyView = @() + $VMHost | Foreach-Object { Write-Output (Get-Info $_) } + +} - foreach ($myHost in $VMhost) { - - $Info = Get-Info $myHost - $MyView += $Info - - } - - $MyView | Sort-Object ClusterName, HostName, vmnic - - } }