6 Replies Latest reply on Nov 12, 2012 8:25 AM by LucD

    Sort Resource Pools by creation date

    ldelloca Master
    vExpert

      Hi all,

      First of all, complete PS newbie here...

       

      I'm trying to figure out a short PS code to extract these informations from our vCenter server.

      If I use:

       

      Get-ResourcePool | Sort-Object ID -descending

       

      I receive the list based on the first number, so for example:

       

      RP01          ResourcePool-resgroup-99

      RP02          ResourcePool-resgroup-881

       

      RP02 is listed after 01 even if it was created at a later time, simply because the first number (9) is bigger than 8.

      Is there a way to list resource pools by the real increasing number, so I can have at the end the list based on creation date?

       

      Thanks,

      Luca.

        • 1. Re: Sort Resource Pools by creation date
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          The only way I can see this possible is by looking at the Events and Tasks.

          In these you can find the creation event for each resourcepool (provided you keep the events long enough on your vCenter of course).

          Something like this

           

          $rpTab =@{}
           Get-VIEvent -Start (Get-Date).AddDays(-7) -MaxSamples ([int]::MaxValue) | 
          where {$_ -is [VMware.Vim.ResourcePoolCreatedEvent]} | %{   $rpTab.Add($_.ResourcePool.Name,$_) } Get-ResourcePool |
          Sort-Object
          -Property {$rpTab[$_.Name].CreatedTime} |
          Select
          Name,@{N="Date";E={$rpTab[$_.Name].CreatedTime}}

           

          The sample code only goes back 7 days in the events.

          If you need info on resourcepools that were created longer ago, you will have to adapt the code

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          1 person found this helpful
          • 2. Re: Sort Resource Pools by creation date
            RvdNieuwendijk Virtuoso
            vExpertUser Moderators

            Although I am not sure it will give you the resourcepools sorted by creation date, you can sort on the numbers in the resourcepool id's with:

            Get-ResourcePool | Sort-Object -Descending -Property {[int]$_.Id.Split("-")[-1]} | Select-Object -Property Name,Id

            • 3. Re: Sort Resource Pools by creation date
              ldelloca Master
              vExpert

              Hi Luc,

              thanks for the script, however I tried to run it and it stays in execution for hours without completing. It connects correctly to vcenter server, and I checked our configuration, events are kept for 365 days as per default configuration.

               

              Any way to try and debug what is blocking the script from completing?

               

              Thanks,

              Luca.

              • 4. Re: Sort Resource Pools by creation date
                LucD Guru
                Community WarriorsUser ModeratorsvExpert

                Did you change the interval, as specified in AddDays(-7) which means go back 7 days ?

                If you run the Get-VIEvent against a busy and big vCenter, the retrieval of the events can take some time.

                You could try with a couple of hours, AddHours(-2) for example, and see if any events are coming back.

                As an additional test you could create a test resourcepool and verify if the date is correctly indicated from the script.

                 

                Update: just noticed a typo in the script above. It's corrected now.

                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                • 5. Re: Sort Resource Pools by creation date
                  ldelloca Master
                  vExpert

                  Hi Guys,

                  the short code from Robert did the trick, thanks!

                   

                  Luc, yes I tried to shorten the interval even to 1 hour right after creating a new Resource Pools but the script was not closing after an hour. It could be our vCenter is loaded (50 hosts, more than 1000 VMs) anyway Robert script is fast even here.

                   

                  Thanks both,

                  Luca.

                  • 6. Re: Sort Resource Pools by creation date
                    LucD Guru
                    vExpertCommunity WarriorsUser Moderators

                    I'm not sure that the number guarantees a chronoligical order though.

                    Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz