VMware Cloud Community
pacifierzWBG
Contributor
Contributor

Need Help getting vSwitch details using PowerCLI

I have a requirement to get the Standard vSwitch details of ESXi host using PowerCLI.

Expecting the output like below

pastedImage_16.png

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

pastedImage_15.png

http://www.vmwarealert.com/
0 Kudos
2 Replies
Alex_Romeo
Leadership
Leadership

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

Blog: https://www.aleadmin.it/
0 Kudos
LucD
Leadership
Leadership

You could check if the Active and STandby NIC property is empty, before doing the join.

$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

   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

0 Kudos