5 Replies Latest reply on May 27, 2019 4:58 PM by BBB36

    Get multiple vCenters beginning and end MAC Address ranges and export results to csv file.

    BBB36 Novice

      Hello. I'm trying to get this script to populate a csv file with vCenter names, start of Mac address range and end of Mac address range per vCenter server provided in the vclist.csv file. Here's what I have for now. When I run the script, I'm being prompted for an input object. The script appears to just populate the result vclist2.csv file with the length of the character. Can someone please help? Sorry I'm stil in the learning phase of Pwershell/PowerCLI so any help here would be greatly appreciated. Thank you.

      Below is the script

      *********************************************

      $vclist = import-csv .\vclist.csv

      Foreach ($vc in $vclist) {

      $vCenterSettings = Get-View -Id 'OptionManager-VpxSettings'

      $macaddress_begin = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].begin"}).Value

      $macaddress_end = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].end"}).Value

      Export-Csv -Path "d:\vclist2.csv"

      }

        • 1. Re: Get multiple vCenters beginning and end MAC Address ranges and export results to csv file.
          BBB36 Novice
          OK. So I've got it to at least provide some results but it seems to be only grabbing the first host and nothing else.

           

           

          ************************

           

           

          $vclist = import-csv "vclist.csv"

           

           


           

           

          Foreach ($vc in $vclist) {

           

           

            [PSCustomObject]@{

           

           

          $vCenterSettings = Get-View -Id 'OptionManager-VpxSettings'

           

           

          BegMAC = $macaddress_begin = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].begin"}).Value

           

           

          EndMAC = $macaddress_end = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].end"}).Value

           

           

            }

           

           

          $vc | Export-Csv -Path "\vclist1.csv" -Append -Force -NoTypeInformation

           

           

          }

           

           

          *************

           

           


           

           

          I see an output like this on the screen:

           

           


           

           

          VMware.Vim.OptionManager BegMAC EndMAC 

           

           

          ------------------------ ------ ------ 

           

           

          VMware.Vim.OptionManager 001a10000000 001a1000ffff

           

           


           

           

          And the output csv file just has the names of the vCenter servers.

           

           

          I think my issue now is with the for loop and I can't seem to get that to work. Any help/suggestions will be appreciated.

           

          • 2. Re: Get multiple vCenters beginning and end MAC Address ranges and export results to csv file.
            LucD Guru
            User ModeratorsvExpertCommunity Warriors

            You have to provide some input to the Export-Csv cmdlet.
            That can be done through the InputObject parameter (which also accepts pipeline input).
            In the following, I use the Foreach-Object cmdlet, since that places objects in the pipeline.


            I also  assume that you are connected to all vCenters before you run this script?
            In that case, you have to provide the Server parameter on the Get-View cmdlet.
            If yes, you will have to provide the column name in the code (I assume that is 'Name').
            Something like this

             

            Import-Csv -Path .\vclist.csv |

            ForEach-Object -Process {

               $vCenterSettings = Get-View -Id 'OptionManager-VpxSettings' -Server $_.Name

               New-Object PSObject -Property @{

                 'macaddress_begin' = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].begin"}).Value

                 'macaddress_end' = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].end"}).Value

               }

            } |

            Export-Csv -Path ".\vclist2.csv" -NoTypeInformation -UseCulture

            • 3. Re: Get multiple vCenters beginning and end MAC Address ranges and export results to csv file.
              BBB36 Novice

              LucD. Thanks so much.

              Is there a way to run the script without first connecting to all the vCenters?

              Essentially, what I do is log on to a jump host with a userid/password that has admin rights to all the vCenters and want to use that session for authentication.

              The vCenter server FQDN names will be specified in the vclist.csv file under a "VC" or "Server" heading.

              Is that possible?

              • 4. Re: Get multiple vCenters beginning and end MAC Address ranges and export results to csv file.
                LucD Guru
                vExpertUser ModeratorsCommunity Warriors

                Yes, you can add a Connect-VIServer and a Disconnect-VIServer to the script.

                The problem might be the User and Password.
                If you don't want to provide that on the Connect-VIServer line in the script, you might want to use the New-VICredentialStoreItem cmdlet.
                That allows you to do the connection without specifying a User/Password in the script.

                 

                Import-Csv -Path .\vclist.csv |

                ForEach-Object -Process {

                   Connect-VIServer -Server $_.VC | Out-Null

                   $vCenterSettings = Get-View -Id 'OptionManager-VpxSettings' -Server $_.VC

                   New-Object PSObject -Property @{

                     'macaddress_begin' = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].begin" }).Value

                     'macaddress_end' = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].end" }).Value

                   }

                   Disconnect-VIServer -Server $_.VC -Confirm:$false

                } |

                Export-Csv -Path ".\vclist2.csv" -NoTypeInformation -UseCulture

                • 5. Re: Get multiple vCenters beginning and end MAC Address ranges and export results to csv file.
                  BBB36 Novice

                  Hi LucD. My apologies for not responding sooner. I was out of town for a few days. I wanted to thank you and let you know that your solution helped me tremendously. Thank you very much.