Last active
January 26, 2022 17:14
-
-
Save meoso/1b3b45a904d4fb8c77d31f21bcafc252 to your computer and use it in GitHub Desktop.
VMWare DataStore Statistics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$True,Position=0,HelpMessage="The script will output to .CSV datastore statistics for the provided vCenter Server hostname or FQDN")] | |
[string]$vCenter | |
) | |
Import-Module VMware.VimAutomation.Core | Out-Null | |
try { | |
Connect-VIServer -Server $vCenter -Protocol https -Force -ErrorVariable err -ErrorAction SilentlyContinue | Out-Null | |
} catch { | |
Out-Null | |
} finally { | |
if ($err.Count -eq 0) { | |
Write-Output "Fetching DataStore statistics..." | |
$datastores = get-datastore | |
$report = foreach ($store in $datastores) { | |
$store | Select @{N='Datacenter';E={$_.Datacenter.Name}}, | |
@{N='DSCluster';E={Get-DatastoreCluster -Datastore $_ | Select -ExpandProperty Name}}, | |
Name,CapacityGB,@{N='FreespaceGB';E={[math]::Round($_.FreespaceGB,2)}}, | |
@{N='VMCount';E={$_.ExtensionData.VM.Count}}, | |
@{N='ProvisionedSpaceGB';E={[math]::Round(($_.ExtensionData.Summary.Capacity - $_.Extensiondata.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/1GB,2)}}, | |
@{N='UnCommittedGB';E={[math]::Round($_.ExtensionData.Summary.Uncommitted/1GB,2)}}, | |
@{N='FreeGB/CapacityGB %';E={[math]::Round(($_.FreespaceGB/$_.CapacityGB)*100,2 -f 000.00)}}, | |
@{N='ProvisionedGB/CapacityGB %';E={[math]::Round(($_.ExtensionData.Summary.Capacity - $_.Extensiondata.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/$_.CapacityGB/1GB*100,2 -f 000.00)}} | |
} | |
$report | Sort-Object -Property 'ProvisionedGB/CapacityGB %' | Export-Csv datastore-stats.CSV -NoTypeInformation -UseCulture | |
ls datastore-stats.CSV | |
disconnect-viserver -Confirm:$false | |
} else { | |
Write-Output "failed to connect to ${vCenter}. Exiting." | |
Exit | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Big thanks to LucD, and this post which essentially the entire solution: https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-to-get-DatastoreCluster-Datastores-and-the-size/td-p/1301104