3 Replies Latest reply on Nov 11, 2019 3:31 AM by LucD

    VM & hosting datastore size report

    AhmadAfsar Lurker

      Hello,

       

      I am a newbie to PowerCLI and  strong will to learn. Struggling since a month but no luck.

      Would be great if someone can help with a script which fulfills below need.

       

      1. VM Name

      2. VM Size

      3. Hosting datastore

      4. Hosting datastore total space in gb

      5. Hosting datastore used space in gb

      6. Hosting datastore free space in gb

       

      Thanks everyone.

        • 1. Re: VM & hosting datastore size report
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          First, did you already do a search in this community?

          There are quite a few example scripts in here doing exactly what you are asking for.

           

          Try like this.
          Note that

           

          • The VMHost information is available on the object returned by Get-VM under the VMHost property
          • The same VirtualMachine object also contains information about the datastore(s) on which the VM lives, under DatastoreIdList property.
          • With the Get-View cmdlet you can access the vSphere objects. In this case the Datastore object. Note that PowerCLI cmdlets return what are called .NET Objects. These are not the same as vSphere objects.
          • On a Select-Object you can use what is called a calculated property. These are hash tables with a Name and Expression entry. In the Expression part a script can manipulate data. They are in fact script blocks.
          • In a PowerShell script one can use the .NET methods. That is what [Math]::Round is doing.
          • The -join operator combines elements of an array with the character specified on the right side of the -join operator.

           

          Get-VM |

          Select Name,

              @{N='UsedSpaceGB';E={[math]::Round($_.UsedSpaceGB,1)}},

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

              @{N='Datastore';E={

                  $script:ds= Get-View -Id $_.DatastoreIdList

                  $script:ds.Name -join '|'}},

              @{N='DatastoreCapacityGB';E={($script:ds.Summary | %{[math]::Round($_.Capacity/1GB,0)}) -join '|'}},

              @{N='DatastoreUsedGB';E={($script:ds.Summary | %{[math]::Round(($_.Capacity - $_.FreeSpace)/1GB,0)}) -join '|'}},

              @{N='DatastoreFreeGB';E={($script:ds.Summary | %{[math]::Round($_.FreeSpace/1GB,0)}) -join '|'}}

          1 person found this helpful
          • 2. Re: VM & hosting datastore size report
            AhmadAfsar Lurker

            Hello LucD,

             

            A big thank you. This is what my needs was. Yes, I already searched before posting this and came across some scripts but those were not really what I was looking for.

            I am very thankful for the explanation that helped me to understand the things and I was able to pull the free datastore space in Percent as well.

             

            Just one thing that I did not get is $script:ds part.

             

            Thank you once again.

            • 3. Re: VM & hosting datastore size report
              LucD Guru
              User ModeratorsCommunity WarriorsvExpert

              In PowerShell, a variable is always in what is called a scope.

              There are 3 of those scopes:

               

              • local: within one code block (the default)
              • script: for the entire script
              • global: for your PS session

               

              Since the Expressions in a calculated property are code blocks, all variables defined in there are only known within that code block.

              Since I wanted to avoid having to fetch the datastores multiple times, I defined that variables in the script scope.

              That way the variable is known in the Expression blocks of the other calculated expressions.