VMware Cloud Community
AlbertWT
Virtuoso
Virtuoso
Jump to solution

How to get Datastore Status for each VM [to identify overprovisioning] ?

Hi people,

Could you please assist me in generating the report of VM and its Datastore location (with Data store Free Space, Provisioned Size and Usage). ?

Ideally it should go like this:

VM Name | Datastore Name | Datastore Size | DataStore Total Provisioned size

Thanks

/* Please feel free to provide any comments or input you may have. */
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The following script adds the ProvisionedSpaceGB property to the output of Luc's script:

Get-VM | ForEach-Object {
  if ($_)
  {
    $vm = $_
    $vm | Get-Datastore | ForEach-Object {
      if ($_)
      {
        $Datastore = $_
        "" | Select-Object -Property @{N="VM name";E={$vm.Name}},
        @{N="ProvisionedSpaceGB";E={$vm.ProvisionedSpaceGB}},
        @{N="Datastore Name";E={$Datastore.Name}},
        @{N="Datastore Size (GB)";E={[Math]::Round($Datastore.CapacityMB/1KB,0)}},
        @{N="Datastore Total Provisioned size (GB)";E={[Math]::Round(($Datastore.CapacityMB-$Datastore.FreeSpaceMB)/1KB+$Datastore.ExtensionData.Summary.Uncommitted/1GB,0)}},
        @{N="Datastore Used Space (GB)";E={[Math]::Round(($Datastore.CapacityMB-$Datastore.FreeSpaceMB)/1KB,0)}}
      }
    }
  }
}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
16 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Albert,

the next PowerCLI script will give you the desired VM and datastore information:

Get-VM |
ForEach-Object {
  if ($_)
  {
    $vm = $_
    $vm |
    Get-Datastore |
    ForEach-Object {
      if ($_)
      {
        $Datastore = $_
        "" |
        Select-Object -Property @{N="VM name";E={$vm.Name}},
          @{N="Datastore Name";E={$Datastore.Name}},
          @{N="Datastore Size (GB)";E={[Math]::Round($Datastore.CapacityGB,0)}},
          @{N="Datastore Total Provisioned size (GB)";E={[Math]::Round($Datastore.CapacityGB-$Datastore.FreeSpaceGB+$Datastore.ExtensionData.Summary.Uncommitted/1GB,0)}},
          @{N="Datastore Used Space (GB)";E={[Math]::Round($Datastore.CapacityGB-$Datastore.FreeSpaceGB,0)}}
      }
    }
  }
}

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
AlbertWT
Virtuoso
Virtuoso
Jump to solution

Thanks for the reply Rob, just came back from the long weekend 😉

two things, is there any reason why the result is always showing zero in the DS Size and used space ?

VM name                               : PRODDB1
Datastore Name                        : VMFS_13_NOT_REPLICATED
Datastore Size (GB)                   : 0
Datastore Total Provisioned size (GB) : 159
Datastore Used Space (GB)             : 0

also I need to get the result in CSV format so that I can sort it based on the most over provisoned DS if possible.

Cheers !

/* Please feel free to provide any comments or input you may have. */
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which PowerCLI version are you using ?

Get-PowerCLIVersion

The CapacityGB and FreeSpaceGB were introduced only recently.

To get the result in a CSV file, change the last line to

} | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
AlbertWT
Virtuoso
Virtuoso
Jump to solution

Here it is Luc:

PowerCLI Version
----------------
   VMware vSphere PowerCLI 4.1 U1 build 332441
---------------
Snapin Versions
---------------
   VMWare vSphere PowerCLI 4.1 U1 build 332441

Thanks for the suggestion.

/* Please feel free to provide any comments or input you may have. */
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Since you're on a pre-5 version, do it like this

Get-VM | ForEach-Object {
  if ($_)
  {
    $vm = $_
    $vm | Get-Datastore | ForEach-Object {       if ($_)       {         $Datastore = $_
       
"" | Select-Object -Property @{N="VM name";E={$vm.Name}},
          @{N="Datastore Name";E={$Datastore.Name}},
         
@{N="Datastore Size (GB)";E={[Math]::Round($Datastore.CapacityMB/1KB,0)}},
         
@{N="Datastore Total Provisioned size (GB)";E={[Math]::Round(($Datastore.CapacityMB-$Datastore.FreeSpaceMB)/1KB+$Datastore.ExtensionData.Summary.Uncommitted/1GB,0)}},
         
@{N="Datastore Used Space (GB)";E={[Math]::Round(($Datastore.CapacityMB-$Datastore.FreeSpaceMB)/1KB,0)}}       }     }   } } | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

MartinTor
Contributor
Contributor
Jump to solution

This script is awesome :)...anyone knows how to also include the "provisionedspacegb" property into the output ? meaning the vmdk total space besides the VM name ?

i tried the harddisks (capacityGB) attribute, but im having a hardtime adding that into the output..

Thanks

Martin

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The following script adds the ProvisionedSpaceGB property to the output of Luc's script:

Get-VM | ForEach-Object {
  if ($_)
  {
    $vm = $_
    $vm | Get-Datastore | ForEach-Object {
      if ($_)
      {
        $Datastore = $_
        "" | Select-Object -Property @{N="VM name";E={$vm.Name}},
        @{N="ProvisionedSpaceGB";E={$vm.ProvisionedSpaceGB}},
        @{N="Datastore Name";E={$Datastore.Name}},
        @{N="Datastore Size (GB)";E={[Math]::Round($Datastore.CapacityMB/1KB,0)}},
        @{N="Datastore Total Provisioned size (GB)";E={[Math]::Round(($Datastore.CapacityMB-$Datastore.FreeSpaceMB)/1KB+$Datastore.ExtensionData.Summary.Uncommitted/1GB,0)}},
        @{N="Datastore Used Space (GB)";E={[Math]::Round(($Datastore.CapacityMB-$Datastore.FreeSpaceMB)/1KB,0)}}
      }
    }
  }
}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
MartinTor
Contributor
Contributor
Jump to solution

Thanks !...i got it working after some more coffee Smiley Happy

i have to tweak the output a bit, so i can feed Cacti for proper reporting

Thanks again for the quick reply

0 Kudos
AlbertWT
Virtuoso
Virtuoso
Jump to solution

thanks for the assistance guys !

/* Please feel free to provide any comments or input you may have. */
0 Kudos
King_Julien
Contributor
Contributor
Jump to solution

This is awesome, I'm trying to -format table but I'm not able to get it right, I got errors, is it possible to "format table" this kind of report?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Foreach loop doesn't place anything on the pipeline.

You can use the Call operator (&) as a bypass.

&{

#

# The original script

#

} | Format-Table -AutoSize


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

King_Julien
Contributor
Contributor
Jump to solution

Thank you LucD. If i don't put the & at the beginning of the script it seems to run but cannot see three columns.

If I put the & at the beggining of the scripts I get an error.

Get-VM | ForEach-Object &{

  if ($_)

  {

    $vm = $_

    $vm | Get-Datastore | ForEach-Object {

      if ($_)

      {

        $Datastore = $_

        "" | Select-Object -Property @{N="VM name";E={$vm.Name}},

        @{N="ProvisionedSpaceGB";E={$vm.ProvisionedSpaceGB}},

        @{N="Datastore Name";E={$Datastore.Name}},

        @{N="Datastore Size (GB)";E={[Math]::Round($Datastore.CapacityMB/1KB,0)}},

        @{N="Datastore Total Provisioned size (GB)";E={[Math]::Round(($Datastore.CapacityMB-$Datastore.FreeSpaceMB)/1KB+$Datastore.ExtensionData.Summary.Uncommitted/1GB,0)}},

        @{N="Datastore Used Space (GB)";E={[Math]::Round(($Datastore.CapacityMB-$Datastore.FreeSpaceMB)/1KB,0)}}

      }

    }

  }

} | format-table -autosize

I get an "Ampersand not allowed: The & operator is reserved"...

If I ignore the &, and just type | format-table -autosize  > c:\something.txt, the file is generated but there's a legend at the beginning of the file telling me WARNING: 3 columns do not fit into the display and were removed.

No luck trying to exporting to csv.

Thanks once again!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That first line should be

&{ Get-VM | ForEach-Object


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

King_Julien
Contributor
Contributor
Jump to solution

Ah.

Now we are talking :smileygrin:

Good, now I'm going to do an attempt of troubleshooting to see why the other 3 columns are not exported to csv.

Thanks!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you want to export the results to a file, there is no need to use the Format-Table, which is intended for console output.

Try using an Export-Csv, and check in there if all the columns are present


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

King_Julien
Contributor
Contributor
Jump to solution

lol, I was going to update the post with that, no need of format-table when exporting to csv.

Thanks!

0 Kudos