In many situations, it is necessary to track the usage of datastore usage in our environment because if your datastore is full , it will cause a potential outage to your virtual machines in your environment. It is really in need of maintaining some threshold of free storage % always to avoid potential issues. This simple powershell script will helps you pull the usage and % of free space report of all the datastores in your vCenter server.
Enter your vCenter Server Name and Specify the output file name as per your wish. Both items are marked in yellow in the below script.
============================================================================================
Connect-VIServer vCENTER_SERVERNAME
Function Percentcal {
param(
[parameter(Mandatory = $true)]
[int]$InputNum1,
[parameter(Mandatory = $true)]
[int]$InputNum2)
$InputNum1 / $InputNum2*100
}
$datastores = Get-Datastore | Sort Name
ForEach ($ds in $datastores)
{
if (($ds.Name -match "Shared") -or ($ds.Name -match ""))
{
$PercentFree = Percentcal $ds.FreeSpaceMB $ds.CapacityMB
$PercentFree = "{0:N2}" -f $PercentFree
$ds | Add-Member -type NoteProperty -name PercentFree -value $PercentFree
}
}
$datastores | Select Name,@{N="UsedSpaceGB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace)/1GB,0)}},@{N="TotalSpaceGB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity)/1GB,0)}} ,PercentFree | Export-Csv c:\datastorereport.csv -NoTypeInformation
============================================================================================
Output of the above script will be similar to the below table. It will provide the Datastore name, UsedSpace in GB , TotalSpace in GB and % of free space on the datastore.
Enter your vCenter Server Name and Specify the output file name as per your wish. Both items are marked in yellow in the below script.
============================================================================================
Connect-VIServer vCENTER_SERVERNAME
Function Percentcal {
param(
[parameter(Mandatory = $true)]
[int]$InputNum1,
[parameter(Mandatory = $true)]
[int]$InputNum2)
$InputNum1 / $InputNum2*100
}
$datastores = Get-Datastore | Sort Name
ForEach ($ds in $datastores)
{
if (($ds.Name -match "Shared") -or ($ds.Name -match ""))
{
$PercentFree = Percentcal $ds.FreeSpaceMB $ds.CapacityMB
$PercentFree = "{0:N2}" -f $PercentFree
$ds | Add-Member -type NoteProperty -name PercentFree -value $PercentFree
}
}
$datastores | Select Name,@{N="UsedSpaceGB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace)/1GB,0)}},@{N="TotalSpaceGB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity)/1GB,0)}} ,PercentFree | Export-Csv c:\datastorereport.csv -NoTypeInformation
============================================================================================
Output of the above script will be similar to the below table. It will provide the Datastore name, UsedSpace in GB , TotalSpace in GB and % of free space on the datastore.
Name | UsedSpaceGB | TotalSpaceGB | PercentFree |
Prod_Datastore_T1_001 | 674 | 676 | 0.42 |
Prod_Datastore_T1_002 | 253 | 256 | 1.24 |
Prod_Datastore_T2_001 | 491 | 500 | 1.7 |
Prod_Datastore_T2_002 | 359 | 367 | 2.07 |
Dev_Datastore_001 | 250 | 256 | 2.18 |
Lab_Datastore_001 | 661 | 676 | 2.24 |
ISO_Datastore_001 | 2342 | 2397 | 2.28 |
I hope this script will makes your job easy by pulling the datastore usage report as per need and produce it your management about your storage usage on your virtualization environment. I hope this if informative for you and thanks for reading!!!!