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