VMware Cloud Community
antoniogemelli
Hot Shot
Hot Shot
Jump to solution

get switch

Hello, I use this script to get some network adapter information, I would like to get also switch too,

Get-VMHostNetworkAdapter |

Where-Object {$_.GetType().Name -eq "PhysicalNicImpl"} |

Select-Object -Property VMHost,Name,BitRatePerSec,FullDuplex,Mac,DhcpEnabled,

  @{Name="AutoNegotiate";Expression={

    if ($_.ExtensionData.Spec.LinkSpeed)

      {$false} else {$true}

  }},

  @{Name="LinkState";Expression={

    if ($_.ExtensionData.LinkSpeed)

     {"Up"} else {"Down"}

  }} | Export-Csv -Path C:\Users\gemela\info.csv -NoTypeInformation -UseCulture

Thanks

Tags (1)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, with VDS the vSwitch field is indeed blank.

Try this variation.

Get-VMHostNetworkAdapter -Physical |

Select-Object -Property VMHost,Name,BitRatePerSec,FullDuplex,Mac,DhcpEnabled,

  @{Name="AutoNegotiate";Expression={

   if ($_.ExtensionData.Spec.LinkSpeed)

  {$false} else {$true}

  }},

  @{Name="LinkState";Expression={

   if ($_.ExtensionData.LinkSpeed)

  {"Up"} else {"Down"}

  }},

  @{N='vSwitch';E={

   $vmnic = $_.Name

   $esxcli = Get-EsxCli -VMHost $_.VMHost

   $sw = @($esxcli.network.vswitch.standard.list() | where{$_.Uplinks -contains $vmNic} | select -ExpandProperty Name)

   $sw += $esxcli.network.vswitch.dvs.vmware.list() | where{$_.Uplinks -contains $vmNic} | select -ExpandProperty Name

   $sw -join '|'}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

8 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-VMHostNetworkAdapter -Physical |

Select-Object -Property VMHost,Name,BitRatePerSec,FullDuplex,Mac,DhcpEnabled,

  @{Name="AutoNegotiate";Expression={

   if ($_.ExtensionData.Spec.LinkSpeed)

  {$false} else {$true}

  }},

  @{Name="LinkState";Expression={

   if ($_.ExtensionData.LinkSpeed)

  {"Up"} else {"Down"}

  }},

  @{N='vSwitch';E={

   $vnic = $_.Name

   Get-VirtualSwitch -VMHost $_.VMHost | where{$_.Nic -contains $vnic} | Select -ExpandProperty Name

  }}  | Export-Csv -Path C:\Users\gemela\info.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

antoniogemelli
Hot Shot
Hot Shot
Jump to solution

vSwitch record are empty in the csv

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Works for me, the vSwitch property is empty when the vmnic is not used in any vSwitch.
Are you using VSS or VDS, or a mix of both types?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, with VDS the vSwitch field is indeed blank.

Try this variation.

Get-VMHostNetworkAdapter -Physical |

Select-Object -Property VMHost,Name,BitRatePerSec,FullDuplex,Mac,DhcpEnabled,

  @{Name="AutoNegotiate";Expression={

   if ($_.ExtensionData.Spec.LinkSpeed)

  {$false} else {$true}

  }},

  @{Name="LinkState";Expression={

   if ($_.ExtensionData.LinkSpeed)

  {"Up"} else {"Down"}

  }},

  @{N='vSwitch';E={

   $vmnic = $_.Name

   $esxcli = Get-EsxCli -VMHost $_.VMHost

   $sw = @($esxcli.network.vswitch.standard.list() | where{$_.Uplinks -contains $vmNic} | select -ExpandProperty Name)

   $sw += $esxcli.network.vswitch.dvs.vmware.list() | where{$_.Uplinks -contains $vmNic} | select -ExpandProperty Name

   $sw -join '|'}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

antoniogemelli
Hot Shot
Hot Shot
Jump to solution

This is perfect, thanks LucD.

0 Kudos
antoniogemelli
Hot Shot
Hot Shot
Jump to solution

Hi LucD,

One more question, Is possible to group the result? or get name of hardware? (one more column),

It will be useful if I can recognize which Nic is part of Broadcom 10/20 Gigabit Ethernet and which Nic are part of Intel...

nic.PNG

Thanks a lot.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VMHostNetworkAdapter -Physical |

Select-Object -Property VMHost,Name,BitRatePerSec,FullDuplex,Mac,DhcpEnabled,

  @{Name="AutoNegotiate";Expression={

   if ($_.ExtensionData.Spec.LinkSpeed)

  {$false} else {$true}

  }},

  @{Name="LinkState";Expression={

   if ($_.ExtensionData.LinkSpeed)

  {"Up"} else {"Down"}

  }},

  @{N='vSwitch';E={

   $script:vmnic = $_.Name

   $script:esxcli = Get-EsxCli -VMHost $_.VMHost

   $sw = @($esxcli.network.vswitch.standard.list() | where{$_.Uplinks -contains $vmNic} | select -ExpandProperty Name)

   $sw += $esxcli.network.vswitch.dvs.vmware.list() | where{$_.Uplinks -contains $vmNic} | select -ExpandProperty Name

   $sw -join '|'}},

  @{N='HW';E={

   $script:esxcli.network.nic.list() | where{$_.Name -eq $script:vmnic} | select -ExpandProperty Description}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

antoniogemelli
Hot Shot
Hot Shot
Jump to solution

Amazing, thanks LucD 🙂 

0 Kudos