More Powershell - Get-Datastore Sizes in three easy steps...

1. Connect to your vCenter servers.

Connect-ViServer <ServerName>

2. Define your Datastores you want to get the sizes of

$DS = get-datastore | where {$_.Name -notmatch "Local" -and $_.Name -notmatch "iSCSI"}

3. Run the view script to see the appropriate Datastores and their sizes in GBs!

$DS | Select Name, @{N="FreeSpaceGB";E={[System.Math]::Round($_.FreeSpaceMB / 1024, 2)}} , @{N="CapacityGB";E={$_.CapacityMB / 1024}}, @{N="UsedGB";E={[System
.Math]::Round(($_.CapacityMB - $_.FreeSpaceMB) / 1024, 2)}}


Yes, this is on one line, and here is the explanation:

- $DS is an object created in step 2 capturing all the information outputted from the remainder of the code line.  So in this case is gets objects that don't contain text Local or iSCSI within the Name.
- If you just type $DS it outputs similar type information to what Step 3's uber command but what the command in step 3 does is massage the data to a more useful form.
- The | Select sends the objects output of $DS to a select-object command.  (huh?) (Select-object allows you to pick the portions of the output you want)
     - Name outputs the Name of the Datastore (pretty obvious)
     - Ok, what is the next portion?  It looks like random characters.  Well it is a statement manipulating the output.
       
      @{N="FreeSpaceGB";E={[System.Name]::Round($_.FreeSpaceMB / 1024, 2)}}

     - The @ shows it is an Array, the "N" actually stands for "Name" and the "E" stands for expression. Yes, you can write the whole words out but N and E are faster :) 
     - So Put the title of FreeSpaceGB and figure out what FreeSpaceMB / 1024 equals and round it to 2 decimal places.
     - Pretty simple, eh?

This is only a basic example but it gives you a starting place for developing your own one-liners!

Alternate Listing the Freespace, Capacity, Provisioned and Over-Provisioned Storage:

$DS = Get-Datastore | Get-View | where {$_.Name -notmatch "Local" -and $_.Name -notmatch "NFS" -and $_.Name -notmatch "iSCSI"}

$GB = "1073741824"

$Disk = $DS | Select -ExpandProperty Summary | Select Name, @{N="FreeSpaceInGB";E={[System.Math]::Round($_.FreeSpace / $GB, 2)}}, @{N="CapacityInGB";E={$_.Capacity / $GB}}, @{N="UsedGB";E={[System.Math]::Round(($_.Capacity - $_.FreeSpace) / $GB, 2)}}, @{N="PercentageFree";E={[System.Math]::Round(($_.FreeSpace / $_.Capacity) * 100)}}, @{N="ProvisionedInGB";E={[System.Math]::Round(($_.Capacity - $_.FreeSpace + $_.Uncommitted) / $GB, 2)}}, @{N="OverProvisionedByInGB";E={[System.Math]::Round((($_.Capacity - $_.FreeSpace + $_.Uncommitted) - $_.Capacity) / $GB, 2)}}


$Disk

Comments

DAA0307 said…
Very nice script. I'm by no minds a Powershell expert - how can you export the data to a HTML or CSV format?
virtualvelcro said…
for all powershell commands you can end the statement with a pipe "|" and an export-csv \

This exports into a csv format.

example:

$ds | export-csv c:\scripts\output.csv