1 2 Previous Next 24 Replies Latest reply on Feb 19, 2018 12:18 AM by bansne

    PowerCLI to extract all vSwitch network info to CSV

    AlbertWT Master

      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

        • 1. Re: PowerCLI to extract all vSwitch network info to CSV
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          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
          3 people found this helpful
          • 2. Re: PowerCLI to extract all vSwitch network info to CSV
            AlbertWT Master

            Many thanks Luc,

            That was quick and very good

             

            Cheers !

            • 3. Re: PowerCLI to extract all vSwitch network info to CSV
              AlbertWT Master

              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 ?

              • 4. Re: PowerCLI to extract all vSwitch network info to CSV
                LucD Guru
                vExpertCommunity WarriorsUser Moderators

                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

                1 person found this helpful
                • 5. Re: PowerCLI to extract all vSwitch network info to CSV
                  AlbertWT Master

                  Luc, many thanks for the revised edition.

                  however does the StandBy NIC column meant to be empty ?

                  • 6. Re: PowerCLI to extract all vSwitch network info to CSV
                    sangepu Novice

                    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}}}
                                }
                            }
                        }

                    1 person found this helpful
                    • 7. Re: PowerCLI to extract all vSwitch network info to CSV
                      vkaranam Hot Shot

                      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

                      • 8. Re: PowerCLI to extract all vSwitch network info to CSV
                        LucD Guru
                        vExpertUser ModeratorsCommunity Warriors

                        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

                        • 9. Re: PowerCLI to extract all vSwitch network info to CSV
                          kumawat_29 Novice

                          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

                          • 10. Re: PowerCLI to extract all vSwitch network info to CSV
                            LucD Guru
                            Community WarriorsUser ModeratorsvExpert

                            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.

                            • 11. Re: PowerCLI to extract all vSwitch network info to CSV
                              vmCalgary Novice

                              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.

                              1 person found this helpful
                              • 12. Re: PowerCLI to extract all vSwitch network info to CSV
                                LucD Guru
                                vExpertUser ModeratorsCommunity Warriors

                                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?

                                • 13. Re: PowerCLI to extract all vSwitch network info to CSV
                                  vmCalgary Novice

                                  Do you have VMs with more than 1 vNIC?

                                   

                                  Yes we have VMs with more than one vNIC.

                                  • 14. Re: PowerCLI to extract all vSwitch network info to CSV
                                    LucD Guru
                                    Community WarriorsvExpertUser Moderators

                                    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

                                                }

                                            }

                                        }

                                    }

                                     

                                     

                                    1 person found this helpful
                                    1 2 Previous Next