AlbertWT
Virtuoso
Virtuoso

PowerCLI to extract all vSwitch network info to CSV

Jump to solution

Hi People,

Can anyone please assist me with the PowerCLi script to be executed against one VCenter server to gather each VMHost (ESX & ESXi) Networking configuration as in Configuration tab | Networking | View: vSphere Standard Switch

For Example here as follows:

ESX01

  vSwitch0, vmnic3, vmnic4

  Service Console 2

  vmk2: 10.1.1.4 | VLAN 1

  Management Network

  vmk1: 10.1.2.1 | VLAN 2

  VMotion

  vmk2: 10.1.3.1 | VLAN 3

  vSwitch1, vmnic2, vmnic1 vmnic0

  VLAN 45

  VLAN 12

  MS Cluster Heart Beat Network

  VLAN 8

ESX02

  vSwitch0, vmnic3, vmnic4

  Service Console 2

  vmk2: 10.2.1.4 | VLAN 1

  Management Network

  vmk1: 10.2.2.1 | VLAN 2

  VMotion

  vmk2: 10.2.3.1 | VLAN 3

  vSwitch1, vmnic2, vmnic1 vmnic0

  VLAN 45

  VLAN 12

  MS Cluster Heart Beat Network

  VLAN 8

...

Any type of format will do in CSV as long as the information can be used to create the vSwitch manually on each ESX host.

This is just in case I need to rebuild one of the ESX / ESXi host in the event of the ESX to ESXi upgrade went wrong which requires me to rebuild the server from scratch.

Cheers,

Al

/* Any kind of comment or input would be greatly appreciated */
Tags (3)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

This version will give the active and standby NICs for each vSwitch.

&{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


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

View solution in original post

24 Replies
LucD
Leadership
Leadership

Try something like this

&{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="NIC";E={if($vsw.Nic){[string]::Join(',',$vsw.Nic)}}},
               
@{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


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

AlbertWT
Virtuoso
Virtuoso

Many thanks Luc,

That was quick and very good :smileycool:

Cheers !

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
AlbertWT
Virtuoso
Virtuoso

Luc,

Somehow the Script was partially correct, since it only returns the VMnic that is currently active, but not the standby NIC, is there any possible way to get the standby NIC uplinks as well ?

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
LucD
Leadership
Leadership

This version will give the active and standby NICs for each vSwitch.

&{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


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

View solution in original post

AlbertWT
Virtuoso
Virtuoso

Luc, many thanks for the revised edition.

however does the StandBy NIC column meant to be empty ?

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
sangepu
Contributor
Contributor

Nice script snippet. Below would be useful for environment where portgroup NIC Teaming is set to 'Override switch failover order' i.e. if portgroup NIC teaming is not same as vSwitch

Get-VMHost <ESXHostName> | % {
        $esx = $_
        $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="vSwitch";E={$vsw.Name}},
                    @{N="Portgroup";E={$pg.Name}},
                    @{N="VLAN";E={$pg.VLanId}},
                    @{N="Active NIC";E={
                                    If ($pg.extensiondata.spec.policy.Nicteaming.NicOrder.ActiveNic) {
                                    [string]::Join(',',$pg.extensiondata.spec.policy.Nicteaming.NicOrder.ActiveNic)
                                    } Else {
                                        [string]::Join(',',$vsw.ExtensionData.Spec.Policy.NicTeaming.NicOrder.ActiveNic)
                                        }}},
                    @{N="Standby NIC";E={
                                    If ($pg.extensiondata.spec.policy.Nicteaming.NicOrder.StandbyNic) {
                                    [string]::Join(',',$pg.extensiondata.spec.policy.Nicteaming.NicOrder.StandbyNic)
                                    } Else {
                                        [string]::Join(',',$vsw.ExtensionData.Spec.Policy.NicTeaming.NicOrder.StandbyNic)
                                        }}},
                    @{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}}}
            }
        }
    }

vkaranam
Enthusiast
Enthusiast

Hello Lucd,

1.)

I tried this Script  and i am getting an error saying

Exception calling "Add" with "2" argument(s): "Item has already been added. Key in dictionary: ''  Key being added: ''"

i tried the below method but it is still getting the error.

if($vNicTab -eq ""){continue}

      #else {$vNicTab.Add($_.Portgroup,$_)}

2.)

How can i retrieve the uplinks details and the vmnic details for the Nexus 1k Switch. The current script is not generating this output.

Thanks in advance.

Thanks

vKar

0 Kudos
LucD
Leadership
Leadership

1) That seems to indicate you ran the script against multiple ESXi nodes, and these ESXi nodes have like-named portgroups.

Hence the duplicate.

As a bypass, you could run the code against a single ESXi node, and reinitialise the $vNicTab at the beginning

2) This script doesn't handle dvSwitches, and hence no Nexus switches.

For dvSwitches the script needs to be adapted


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

0 Kudos
kumawat_29
Contributor
Contributor

Hi LuCD,

I am new to VMware and found information useful for my work. We are building VM inventory in Amdocs Cramer tool, we want to reconcile this data back to inventory.

Can you please explain steps to run this script ?

Can I directly copy and paste in PowerCli command prompt ?

Regards,

Pradeep

0 Kudos
LucD
Leadership
Leadership

You can copy/paste multiple lines to the PowerCLI command prompt.

But the more common practice is to use a .ps1 file, add the lines in there, and then run the script from prompt.

For exampe, if you save the script as C:\Scripts\test.ps1, you can do C:\Scripts\test.ps1 from the PowerCLI command prompt.

XtraVirt has a very good Beginners Guide explaining this.

Alan also mentions this manual in his PowerCLI – Where do I start ?!  post.


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

0 Kudos
vmCalgary
Enthusiast
Enthusiast

LucD​,

I love the output from the following but I'm missing a bit of information for my needs.

&{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="NIC";E={if($vsw.Nic){[string]::Join(',',$vsw.Nic)}}},
               
@{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

How do I get the following columns?

VM Name, VM_IP, Virtual Host + the output from above:  vSwitch, NIC,  PortGroup,  VLAN, Device, IP

I am planning on retiring a cluster and need to move VMs. My goal is to accurately identify which VMs and VLANs/PortGroups I need to relocate. I do have a specific cluster name if that helps.

LucD
Leadership
Leadership

The VM properties you want are easy to obtain from a Get-VM, and then mapping the information to the network information above might be done through the portgroupname above and the networkname of the vNIC(s) on the VM.

Do you have VMs with more than 1 vNIC?


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

0 Kudos
vmCalgary
Enthusiast
Enthusiast

Do you have VMs with more than 1 vNIC?

Yes we have VMs with more than one vNIC.

0 Kudos
LucD
Leadership
Leadership

Try something like this

$clusterName = 'MyCluster'

$vmhosts = Get-Cluster -Name $clusterName | Get-VMHost

foreach($esx in $vmhosts){

    $vNicTab = @{}

    $esx.ExtensionData.Config.Network.Vnic | %{

        $vNicTab.Add($_.Portgroup,$_)

    }

    foreach($vsw in (Get-VirtualSwitch -VMHost $esx)){

        $pgTab = @{}

        foreach($pg in (Get-VirtualPortGroup -VirtualSwitch $vsw)){

            $obj = New-Object PSObject -Property @{

                vSwitch = $vsw.Name

                NIC = &{if($vsw.Nic){[string]::Join(',',$vsw.Nic)}}

                VLAN = $pg.VLanId

                Device = &{if($vNicTab.ContainsKey($pg.Name)){$vNicTab[$pg.Name].Device}}

                IP = &{if($vNicTab.ContainsKey($pg.Name)){$vNicTab[$pg.Name].Spec.Ip.IpAddress}}

            }

            $pgTab.Add($pg.Name,$obj)

        }

    }

    foreach($vm in Get-VM -Location $esx){

        foreach($nic in Get-NetworkAdapter -VM $vm){

            New-Object PSObject -Property @{

                VM = $vm.Name

                VMHost = $esx.Name

                vNIC = $nic.Name

                IP = $vm.Guest.IPAddress -join '|'

                Portgroup = $nic.NetworkName

                vSwitch = $pgTab[$nic.NetworkName].vSwitch

                NIC = $pgTab[$nic.NetworkName].NIC

                VLAN = $pgTab[$nic.NetworkName].VLAN

                Device = $pgTab[$nic.NetworkName].Device

                IPSw = $pgTab[$nic.NetworkName].IP

            }

        }

    }

}


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

vmCalgary
Enthusiast
Enthusiast

Luc,

I get what you're doing. Conceptually the same as a LEFT OUTER JOIN in MySQL. My results were not what I expected. You can see the blanks for yourself.

Device    :
NIC       :
IPSw      :
VLAN      :
IP        : 10.187.230.226
VM        : VMname.no.cg.int.where_I_work.net
Portgroup : CSD--10.187.230.0/24--CSC-MGMT
vSwitch   :
vNIC      : Network adapter 1
VMHost    : vh315no.vim.dcs.mlb.inet

Device    :
NIC       :
IPSw      :
VLAN      :
IP        : 10.187.230.156|fe80::250:56ff:fe89:648d
VM        : VMname2.no.cg.nms.mlb.inet
Portgroup : CSD--10.187.230.0/24--CSC-MGMT
vSwitch   :
vNIC      : Network adapter 1
VMHost    : vh315no.vim.dcs.mlb.inet

The earlier code from my Nov 7, 2017 10:51 am post gave me:

ESX       : vh301bb.vim.dcs.mlb.inet
vSwitch   : vSwitch1
NIC       : vmnic3,vmnic1
Portgroup : RSC--64.59.161.64/29--Public
VLAN      : 650
Device    :
IP        :

ESX       : vh301bb.vim.dcs.mlb.inet
vSwitch   : vSwitchUSB0
NIC       : vusb0
Portgroup : IMM_Network0
VLAN      : 0
Device    : vmk1
IP        : 169.254.95.120

Essentially all  I need: VMname, Portgroups used, VLANS used, esxi host used. IP address of the VM is a would be nice to verify that the IP truly matches the portgroup on the esxi host is a match (sanity check).

VM: VMname2.no.cg.nms.mlb.inet

IP: 10.187.230.156|fe80::250:56ff:fe89:648d

VMHost: vh315no.vim.dcs.mlb.inet

Portgroup:  CSD--10.187.230.0/24--CSC-MGMT

VLAN: 155

    

Below is from the original code.

pastedImage_12.png

bansne
Enthusiast
Enthusiast

Hi ,

Is there a way to get teaming and failover policy details of vDS too?.

All I am looking for is to get vDS config , vlan/portgroupname/teaming and failover policy info/swithch name.

regards

bansne

0 Kudos
LucD
Leadership
Leadership

You can start with Re: List Portgroups with Private VLANS

Needs to add teaming & failover


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

bansne
Enthusiast
Enthusiast

also the script shared above to pull active/standby nics doesn't work well for me. I am unable to fetch nic status details rest host,portgroup name is reflecting.

I am trying to extract for dvs maybe I need to change the syntax

0 Kudos
LucD
Leadership
Leadership

Yes, this thread is for a report on a VSS.
A VDS reporting script would look quite different.


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

0 Kudos