VMware Cloud Community
kageuss
Contributor
Contributor

Script for VM and datastore information

I am working on a script that will list the datastore information i.e total space, used space, and percent free.

also need to have the VM name (s) that those datastores have on it as well.

this script has all of the datastore information that I am looking for however I can't seem to figure out how to get the VM names listed as well. 

any help would be appreciated.

$VIServer = "vCenter Name"
Connect-VIServer $VIServer -WarningAction SilentlyContinue
start-sleep -s 3
#connect-viserver -Server $vcenter -WarningAction SilentlyContinue
Function Percentcal {
param(
[parameter(Mandatory = $true)]
[int]$InputNum1,
[parameter(Mandatory = $true)]
[int]$InputNum2)
$InputNum1 / $InputNum2*100
}

Write-Host "Please Wait while your datastore query is running" -ForegroundColor Cyan

$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:\Support\Reports\Datastore\Datastore_Usage-$VIServer$(get-date -f MM-dd-yyyy).csv

0 Kudos
1 Reply
scott28tt
VMware Employee
VMware Employee

I have reported your post to the moderators, asking them to move it to the area for PowerCLI.

 


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
0 Kudos