I have a requirement to get the Standard vSwitch details of ESXi host using PowerCLI.
Expecting the output like below
I'm using the below script and I get the output with some error (screenshot below). Please help
$VMHost='ESXi01'
$report = @()
$Vswitches = Get-Virtualswitch -VMHost $VMHost | where Name -Like "*vSwitch*"
foreach($vswitch in $vswitches){
$portgroup = $vswitch | Get-VirtualPortGroup
$nicteam = Get-NicTeamingPolicy $portgroup[0]
$row = "" | select Vswitch,ActiveNic,StandbyNic
$row.Vswitch = $Vswitch.Name
$row.Activenic= [string]::Join(',',$nicteam.activenic)
$row.Standbynic=[string]::Join(',',$nicteam.StandbyNic)
$report += $row
}
$report | Export-Csv D:\vSwitchInfo.csv -NoTypeInformation -UseCulture
Hi,
I think it can help you
PowerCLI to extract all vSwitch network info to CSV
&{foreach($esx in Get-VMHost){
$vNicTab = @{}
$esx.ExtensionData.Config.Network.Vnic | %{
$vNicTab.Add($_.Portgroup,$_)
}
foreach($vsw in (Get-VirtualSwitch -VMHost $esx)){
foreach($pg in (Get-VirtualPortGroup -VirtualSwitch $vsw)){
Select -InputObject $pg -Property @{N="ESX";E={$esx.name}},
@{N="vSwitch";E={$vsw.Name}},
@{N="Active NIC";E={[string]::Join(',',$vsw.ExtensionData.Spec.Policy.NicTeaming.NicOrder.ActiveNic)}},
@{N="Standby NIC";E={[string]::Join(',',$vsw.ExtensionData.Spec.Policy.NicTeaming.NicOrder.StandbyNic)}},
@{N="Portgroup";E={$pg.Name}},
@{N="VLAN";E={$pg.VLanId}},
@{N="Device";E={if($vNicTab.ContainsKey($pg.Name)){$vNicTab[$pg.Name].Device}}},
@{N="IP";E={if($vNicTab.ContainsKey($pg.Name)){$vNicTab[$pg.Name].Spec.Ip.IpAddress}}}
}
}
}} | Export-Csv report.csv -NoTypeInformation -UseCulture
Alessandro Romeo
You could check if the Active and STandby NIC property is empty, before doing the join.
$report = @()
$Vswitches = Get-Virtualswitch -VMHost $VMHost | where Name -Like "*vSwitch*"
foreach($vswitch in $vswitches){
$portgroup = $vswitch | Get-VirtualPortGroup
$nicteam = Get-NicTeamingPolicy $portgroup[0]
$row = "" | select Vswitch,ActiveNic,StandbyNic
$row.Vswitch = $Vswitch.Name
if($nicteam.activenic){
$row.Activenic= [string]::Join(',',$nicteam.activenic)
}
if($nicteam.StandbyNic){
$row.Standbynic=[string]::Join(',',$nicteam.StandbyNic)
}
$report += $row
}
$report | Export-Csv D:\vSwitchInfo.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference