6 Replies Latest reply on Sep 15, 2020 2:04 AM by CG21

    get the number of vms for each esx

    CG21 Enthusiast

      Hi,

      I carried out this script to have the list of vms for each esx, I would also have to display the number of vms by esx too,

      but a problem appears during the execution,

      no problem without line 17, but when I insert line 17 I have an error

      Can you help, please

      thank you

       

       

      Get-Module -Name VMware* -ListAvailable | Import-Module

       

      $user = 'root'

      $pswd = Read-Host -Prompt "Please enter your Password:"

       

       

      $report = @()

       

      Import-Csv -Path C:\Scripts\Tags_patch\ressources\List_test.csv -UseCulture | %{

       

          $srv = Connect-VIServer -Server $_.ESXName -User $user -Password $pswd

       

          $report += (Get-VM -Server $srv |

         

          Select @{N='VMHost';E={$_.VMHost.Name}},

       

              @{N=“NumVM“;E={($_ | Get-VM).Count}},

       

              Name,PowerState,

       

              @{n="HardDiskSizeGB"; E={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum}},

       

              @{N='GuestOS';E={$_.Guest.OSFullName}},

       

              @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}},

       

              @{N='IP';E={$_.Guest.IPAddress -join '|'}})

        

          Disconnect-VIServer -Server $srv -Confirm:$false

       

      }

       

      $report | Export-Csv -Path C:\Scripts\Tags_patch\resultats\test.csv -NoTypeInformation -UseCulture

        • 1. Re: get the number of vms for each esx
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          It would help if you show the error.

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: get the number of vms for each esx
            CG21 Enthusiast

            Sorry

            i made a mistake,

            I have no error but the result file returns me a value of 0 in the number of vms

             

            for example  i have 2 esx, and the result is like the attached file

            • 3. Re: get the number of vms for each esx
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              The pipeline variable does contain a single VM where you use it.

              Try like this

               

              $user = 'root'

              $pswd = Read-Host -Prompt "Please enter your Password:"


              $report = @()

              Import-Csv -Path C:\Scripts\Tags_patch\ressources\List_test.csv -UseCulture | %{

                  $srv = Connect-VIServer -Server $_.ESXName -User $user -Password $pswd

                  $vms = Get-VM -Server $srv

                  $report += $vms | Select @{N='VMHost';E={$_.VMHost.Name}},

                      @{N=“NumVM“;E={$vms.Count}},

                      Name,PowerState,

                      @{n="HardDiskSizeGB"; E={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum}},

                      @{N='GuestOS';E={$_.Guest.OSFullName}},

                      @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}},

                      @{N='IP';E={$_.Guest.IPAddress -join '|'}}

                  Disconnect-VIServer -Server $srv -Confirm:$false

              }


              $report | Export-Csv -Path C:\Scripts\Tags_patch\resultats\test.csv -NoTypeInformation -UseCulture

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: get the number of vms for each esx
                CG21 Enthusiast

                i have an error on result :

                 

                C:\Scripts\Tags_patch\programmes\CMDBTags> C:\Scripts\Tags_patch\programmes\CMDBTags\test.ps1

                At C:\Scripts\Tags_patch\programmes\CMDBTags\test.ps1:9 char:80

                + ... -Path C:\Scripts\Tags_patch\ressources\List_test.csv -UseCulture | %{

                +                                                                         ~

                Missing closing '}' in statement block or type definition.

                At C:\Scripts\Tags_patch\programmes\CMDBTags\test.ps1:27 char:51

                +         @{N='IP';E={$_.Guest.IPAddress -join '|'}})

                +                                                   ~

                Unexpected token ')' in expression or statement.

                At C:\Scripts\Tags_patch\programmes\CMDBTags\test.ps1:31 char:1

                + }

                + ~

                Unexpected token '}' in expression or statement.

                    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

                    + FullyQualifiedErrorId : MissingEndCurlyBrace

                • 5. Re: get the number of vms for each esx
                  LucD Guru
                  Community WarriorsUser ModeratorsvExpert

                  I had a typo, code above is ocrrected

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 6. Re: get the number of vms for each esx
                    CG21 Enthusiast

                    thank you so much