Dear Members
Can you help to suggest a powercli script to get vCenter ESXi physical network uplink status (Up/down) in a tabular format. The details under esxcli.network.nic.list invoke in tabular format for all the hosts in the vCenter server.
$EsxHosts = Get-VMHost
foreach($EsxHost in $EsxHosts){
$esxcli = Get-VMHost $EsxHost | Get-EsxCli -V2
$esxcli.network.nic.list.Invoke()
}
Regards
Narayanan.
That is due to your screenwidth in the console.
Try adding Format-Table
ForEach-Object -Process {
$esxcli = Get-EsxCli -VMHost $esx -V2
$esxcli.network.vswitch.standard.list.Invoke() |
ForEach-Object -Process {
$vss = $_
$vss.Uplinks | where{$_ -ne $null} | %{
$nic = $_
$esxcli.network.nic.get.Invoke(@{nicname="$_"}) |
Select @{N='VMHost';E={$esx.Name}},
@{N='Switch';E={$vss.Name}},
@{N='NIC';E={$nic}},
LinkStatus
}
}
$esxcli.network.vswitch.dvs.vmware.list.Invoke() |
ForEach-Object -Process {
$vds = $_
$vds.Uplinks | where{$_ -ne $null} | %{
$nic = $_
$esxcli.network.nic.get.Invoke(@{nicname="$_"}) |
Select @{N='VMHost';E={$esx.Name}},
@{N='Switch';E={$vds.Name}},
@{N='NIC';E={$nic}},
LinkStatus
}
}
} | Format-Table -AutoSize
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could do something like this (it handles VSS and VDS).
ForEach-Object -Process {
$esxcli = Get-EsxCli -VMHost $esx -V2
$esxcli.network.vswitch.standard.list.Invoke() |
ForEach-Object -Process {
$vss = $_
$vss.Uplinks | where{$_ -ne $null} | %{
$nic = $_
$esxcli.network.nic.get.Invoke(@{nicname="$_"}) |
Select @{N='VMHost';E={$esx.Name}},
@{N='Switch';E={$vss.Name}},
@{N='NIC';E={$nic}},
LinkStatus
}
}
$esxcli.network.vswitch.dvs.vmware.list.Invoke() |
ForEach-Object -Process {
$vds = $_
$vds.Uplinks | where{$_ -ne $null} | %{
$nic = $_
$esxcli.network.nic.get.Invoke(@{nicname="$_"}) |
Select @{N='VMHost';E={$esx.Name}},
@{N='Switch';E={$vds.Name}},
@{N='NIC';E={$nic}},
LinkStatus
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you LuCD for the reply. Script works but the uplinks status connected under dVSwitch not getting the correct status (vmnic...)
VSS - vswitch0
DVS - dvLANXX
Please advise.
VMHost Switch NIC LinkStatus
------ ------ --- ----------
esx1 vSwitch0 vmnic3 Up
esx1 vSwitch0 vmnic0 Up
esx1 vSwitch0 vmn... Up
esx1 vSwitch0 vmn... Up
esx1 vSwitch0 vmn... Up
esx1 vSwitch0 vmn... Up
esx2 vSwitch0 vmnic3 Up
esx2 vSwitch0 vmnic2 Up
esx2 vSwitch0 vmn... Up
esx2 vSwitch0 vmn... Up
esx2 vSwitch0 vmnic4 Up
esx2 vSwitch0 vmnic5 Up
Tried in the below way but unable to get dVSwitch information and ESXi host name in the first column
$Collection = @()
$Esxihosts = Get-VMHost | Where-Object {$_.ConnectionState -eq "Connected"}
foreach ($Esxihost in $Esxihosts) {
$Esxcli = Get-EsxCli -VMHost $Esxihost
$Esxihostview = Get-VMHost $EsxiHost | get-view
$NetworkSystem = $Esxihostview.Configmanager.Networksystem
$Networkview = Get-View $NetworkSystem
$DvSwitchInfo = Get-VDSwitch -VMHost $Esxihost
if ($DvSwitchInfo -ne $null) {
$DvSwitchHost = $DvSwitchInfo.ExtensionData.Config.Host
$DvSwitchHostView = Get-View $DvSwitchHost.config.host
$VMhostnic = $DvSwitchHostView.config.network.pnic
$DVNic = $DvSwitchHost.config.backing.PnicSpec.PnicDevice
}
$VMnics = $Esxihost | get-vmhostnetworkadapter -Physical #$_.NetworkInfo.Pnic
Foreach ($VMnic in $VMnics){
$realInfo = $Networkview.QueryNetworkHint($VMnic)
$pNics = $esxcli.network.nic.list() | where-object {$vmnic.name -eq $_.name} | Select-Object Description, Link
$Description = $esxcli.network.nic.list()
$CDPextended = $realInfo.connectedswitchport
if ($vmnic.Name -eq $DVNic) {
$vSwitch = $DVswitchInfo | where-object {$vmnic.Name -eq $DVNic} | select-object -ExpandProperty Name
}
else {
$vSwitchname = $Esxihost | Get-VirtualSwitch | Where-object {$_.nic -eq $VMnic.DeviceName}
$vSwitch = $vSwitchname.name
}
$CDPdetails = New-Object PSObject
$CDPdetails | Add-Member -Name EsxName -Value $esxihost.Name -MemberType NoteProperty
$CDPdetails | Add-Member -Name VMNic -Value $VMnic -MemberType NoteProperty
$CDPdetails | Add-Member -Name vSwitch -Value $vSwitch -MemberType NoteProperty
$CDPdetails | Add-Member -Name Link -Value $pNics.Link -MemberType NoteProperty
$CDPdetails | Add-Member -Name PortNo -Value $CDPextended.PortId -MemberType NoteProperty
$CDPdetails | Add-Member -Name Device-ID -Value $CDPextended.devID -MemberType NoteProperty
$CDPdetails | Add-Member -Name Switch-IP -Value $CDPextended.Address -MemberType NoteProperty
$CDPdetails | Add-Member -Name MacAddress -Value $vmnic.Mac -MemberType NoteProperty
$CDPdetails | Add-Member -Name SpeedMB -Value $vmnic.ExtensionData.LinkSpeed.SpeedMB -MemberType NoteProperty
$CDPdetails | Add-Member -Name Duplex -Value $vmnic.ExtensionData.LinkSpeed.Duplex -MemberType NoteProperty
$CDPdetails | Add-Member -Name Pnic-Vendor -Value $pNics.Description -MemberType NoteProperty
$CDPdetails | Add-Member -Name Pnic-drivers -Value $vmnic.ExtensionData.Driver -MemberType NoteProperty
$CDPdetails | Add-Member -Name PCI-Slot -Value $vmnic.ExtensionData.Pci -MemberType NoteProperty
$collection += $CDPdetails
}
}
$Collection | Sort-Object esxname, vmnic | ft *
Regards
Narayanan.
I had a type where I used $vss instead of $vds.
I corrected the code above.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you LuCD for the update.
I can able to get the dvSwitch uplink status showing int the output but its not showing full instead that coming 3 dots for dVSwitch & the vmnic.
VMHost Switch NIC LinkStatus
------ ------ --- ----------
esx1 vSwitch0 vmnic3 Up
esx1 vSwitch0 vmnic0 Up
esx1 dvVLA... vmn... Up
esx1 dvVLA... vmn... Up
esx1 dvVLA... vmn... Up
esx1 dvVLA... vmn... Up
esx2 vSwitch0 vmnic3 Up
esx2 vSwitch0 vmnic2 Up
esx2 dvVLA... vmn... Up
esx2 dvVLA... vmn... Up
esx2 dvVLA... vmnic4 Up
esx2 dvVLA... vmnic5 Up
Regards
Narayanan
That is due to your screenwidth in the console.
Try adding Format-Table
ForEach-Object -Process {
$esxcli = Get-EsxCli -VMHost $esx -V2
$esxcli.network.vswitch.standard.list.Invoke() |
ForEach-Object -Process {
$vss = $_
$vss.Uplinks | where{$_ -ne $null} | %{
$nic = $_
$esxcli.network.nic.get.Invoke(@{nicname="$_"}) |
Select @{N='VMHost';E={$esx.Name}},
@{N='Switch';E={$vss.Name}},
@{N='NIC';E={$nic}},
LinkStatus
}
}
$esxcli.network.vswitch.dvs.vmware.list.Invoke() |
ForEach-Object -Process {
$vds = $_
$vds.Uplinks | where{$_ -ne $null} | %{
$nic = $_
$esxcli.network.nic.get.Invoke(@{nicname="$_"}) |
Select @{N='VMHost';E={$esx.Name}},
@{N='Switch';E={$vds.Name}},
@{N='NIC';E={$nic}},
LinkStatus
}
}
} | Format-Table -AutoSize
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Perfect LuCD. Thank you so much. Looks good.
Additionally can you help to populate the additional fields along with this data if possible.
PCI Device Driver Admin Status Link Status Speed Duplex MAC Address MTU Description
Regards
Narayanan
No clue what you mean by 'Admin status' and 'Description'.
The others you can do like this
ForEach-Object -Process {
$esxcli = Get-EsxCli -VMHost $esx -V2
$esxcli.network.vswitch.standard.list.Invoke() |
ForEach-Object -Process {
$vss = $_
$vss.Uplinks | where{$_ -ne $null} | %{
$nic = $_
$netAdapt = Get-VMHostNetworkAdapter -VMHost $esxcli.VMHost -Name $nic
$esxcli.network.nic.get.Invoke(@{nicname="$_"}) |
Select @{N='VMHost';E={$esx.Name}},
@{N='Switch';E={$vss.Name}},
@{N='NIC';E={$nic}},
@{N='Driver';E={$_.DriverInfo.Driver}},
@{N='MAC';E={$_.VirtualAddress}},
@{N='MTU';E={$vss.MTU}},
@{N='Speed';E={$netAdapt.BitRatePerSec}},
@{N='Duplex';E={$netAdapt.FullDuplex}},
@{N='PCI';E={$netAdapt.PciId}},
LinkStatus
}
}
$esxcli.network.vswitch.dvs.vmware.list.Invoke() |
ForEach-Object -Process {
$vds = $_
$vds.Uplinks | where{$_ -ne $null} | %{
$nic = $_
$netAdapt = Get-VMHostNetworkAdapter -VMHost $esxcli.VMHost -Name $nic
$esxcli.network.nic.get.Invoke(@{nicname="$_"}) |
Select @{N='VMHost';E={$esx.Name}},
@{N='Switch';E={$vds.Name}},
@{N='NIC';E={$nic}},
@{N='Driver';E={$_.DriverInfo.Driver}},
@{N='MAC';E={$_.VirtualAddress}},
@{N='MTU';E={$vds.MTU}},
@{N='Speed';E={$netAdapt.BitRatePerSec}},
@{N='Duplex';E={$netAdapt.FullDuplex}},
@{N='PCI';E={$netAdapt.PciId}},
LinkStatus
}
}
} | Format-Table -AutoSize
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Wonderful LuCD for the updated details. Thanks.
Actually the admin status and description are taken from the below output
PS C:\compaq> $esxcli = Get-EsxCli -VMhost 192.168.1.5 -V2
PS C:\compaq> $esxcli.network.nic.list.Invoke()
AdminStatus : Up
Description : Broadcom Corporation NetXtreme BCM5719 Gigabit Ethernet
Driver : tg3
Duplex : Half
Link : Down
LinkStatus : Down
MACAddress : 3c:a8:2a:e7:a7:20
MTU : 1500
Name : vmnic0
PCIDevice : 0000:03:00.0
Speed : 0
Regards
Narayanan.
Ok, try like this
ForEach-Object -Process {
$esxcli = Get-EsxCli -VMHost $esx -V2
$esxcli.network.vswitch.standard.list.Invoke() |
ForEach-Object -Process {
$vss = $_
$vss.Uplinks | where{$_ -ne $null} | %{
$nic = $_
$netAdapt = Get-VMHostNetworkAdapter -VMHost $esxcli.VMHost -Name $nic
$nicList = $esxcli.network.nic.list.Invoke() | where{$_.Name -eq $nic}
$esxcli.network.nic.get.Invoke(@{nicname="$_"}) |
Select @{N='VMHost';E={$esx.Name}},
@{N='Switch';E={$vss.Name}},
@{N='NIC';E={$nic}},
@{N='Description';E={$nicList.Description}},
@{N='AdminStatus';E={$nicList.AdminStatus}},
@{N='Driver';E={$_.DriverInfo.Driver}},
@{N='MAC';E={$_.VirtualAddress}},
@{N='MTU';E={$vss.MTU}},
@{N='Speed';E={$netAdapt.BitRatePerSec}},
@{N='Duplex';E={$netAdapt.FullDuplex}},
@{N='PCI';E={$netAdapt.PciId}},
LinkStatus
}
}
$esxcli.network.vswitch.dvs.vmware.list.Invoke() |
ForEach-Object -Process {
$vds = $_
$vds.Uplinks | where{$_ -ne $null} | %{
$nic = $_
$netAdapt = Get-VMHostNetworkAdapter -VMHost $esxcli.VMHost -Name $nic
$nicList = $esxcli.network.nic.list.Invoke() | where{$_.Name -eq $nic}
$esxcli.network.nic.get.Invoke(@{nicname="$_"}) |
Select @{N='VMHost';E={$esx.Name}},
@{N='Switch';E={$vds.Name}},
@{N='NIC';E={$nic}},
@{N='Description';E={$nicList.Description}},
@{N='AdminStatus';E={$nicList.AdminStatus}},
@{N='Driver';E={$_.DriverInfo.Driver}},
@{N='MAC';E={$_.VirtualAddress}},
@{N='MTU';E={$vds.MTU}},
@{N='Speed';E={$netAdapt.BitRatePerSec}},
@{N='Duplex';E={$netAdapt.FullDuplex}},
@{N='PCI';E={$netAdapt.PciId}},
LinkStatus
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LuCD for the modification and all the help. Got the expected output. Much Appreciated!!
Looks PCI ID shows Blank in the output. So modified the variable as like below and got the output for PCI.
@{N='PCI';E={$netAdapt.PciId}}, > @{N='PCIDevice';E={$nicList.PCIDevice}},
Tried adding below lines to export in csv format (export-csv) to the path but getting in incorrect characters in the spreadsheet.
| Format-Table -Autosize | Export-csv C:\Reports\Networklinkreport.csv
Can you help to suggest to export the report with DD/MM/YYYY format if possible.
Regards
Narayanan.
You don't need the Format-Table in that case, just do
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Getting the below error after export-csv line addition to the script. Seems the date taking based on the number of lines
PS C:\Temp> .\network-report-update.ps1
PS C:\Temp> .\network-report-update.ps1
Export-csv : Could not find a part of the path 'C:\Reports\Networklinkreport-05\29\2020.csv'.
At C:\compaq\network-report-update.ps1:98 char:5
+ } | Export-csv -Path "C:\Reports\Networklinkreport-$(Get-Date -Format ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Export-Csv], DirectoryNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand
PS C:\Temp> Get-Date
Saturday, September 5, 2020 12:30:41 PM
PS C:\Temp> Get-Date -Format 'dd\/mm\/yyyy'
05/31/2020
PS C:\Temp> Get-Date -Format 'dd/mm/yyyy'
05/32/2020
Regards
Narayanan.
Then replace the Format on Get-Date
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LuCD - Modified the line below and exported the output.
Export-csv -Path "C:\Reports\Networklinkreport_$(Get-Date -Format 'ddMMMyyyy HH_mm').csv"
Thank you so much for your time and support to make this completed.
Regards
Narayanan.
Hi LucD,
How do we use param([string]$vmhost) & param([string]$cluster) in this code so that if i want to get the details/report for a specific host or a specific cluster i do pull. Rather than pulling for whole vcenter.
Thanks
Bikash
Hi LucD
This script provides me what exactly I need for a while, but How Can I get only a list for specific host List?
Most of time my scripts start as follows;
$cluster=Get-Cluster -Name “Test-Cluster”
$HostList=Get-Cluster $cluster | Get-VMHost | Sort-Object Name –Descending | ft -Autosize
I only need to get a result of a specific cluster or chasis to list vmnic States
At the beginning of your script when I change esx entry with $HostList I got errors.
Get-VMHost -PipelineVariable esx |
Also is it possible to sort output in numeric order according to vmnic counter?
to apply the script, I figured out that adding chasis definition after esx provides me the result for that chasis. We have chasis named as esxc2p and blades named as esxc2p01 to esxc2p12.
Get-VMHost -PipelineVariable esx "esxc2p*" |
From my previous post, how can I use this with $HostList or Cluster based?