VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot

Issue Getting Disk UsedSpace(GB), FreeSpace(GB), and FreeSpace(% )

Hi,

I am unable to get the UsedSpace Freespace, and FreeSpace%  as I am getting incorrect values

Please help!!

function get-foldersummary() {
param(
[VMware.VimAutomation.ViCore.Types.V1.Inventory.Folder]$Folder,
[switch]$Recursion = $false
)
$report = @()
$vms = Get-VM -Location $Folder -NoRecursion:(-not $Recursion)
if ($vms.Count -gt 0) {
$report += Get-VM -Location $Folder -NoRecursion:(-not $Recursion) |
Select Folder, Name,
@{N = "IP Address"; E = {@($_.guest.IPAddress[0])}},
@{N = "VM PowerState"; E = {@($_.PowerState)}},
@{N = "OS"; E = {@($_.guest.OSFullName)}},
@{N = "CPU"; E = {@($_.NumCPU)}},
@{N = "Memory (GB)"; E = {@($_.MemoryGB)}},
@{N = "Capacity(GB)"; E = {[math]::Round($_.ProvisionedSpaceGB - $_.MemoryGB)}},
@{N = "FreeSpace(GB)"; E = {[math]::Round(($_.ProvisionedSpaceGB - $_.MemoryGB) - ([math]::Round(([math]::Round($_.ProvisionedSpaceGB - $_.MemoryGB)) - [math]::Round(($_.ProvisionedSpaceGB - $_.MemoryGB) - $_.UsedSpaceGB))))}},
@{N = "FreeSpace(%)"; E = {[math]::Round(($_.ProvisionedSpaceGB - $_.MemoryGB) - $_.UsedSpaceGB) / [math]::Round($_.ProvisionedSpaceGB - $_.MemoryGB)}},
@{N = "UsedSpace(GB)"; E = {[math]::Round(([math]::Round($_.ProvisionedSpaceGB - $_.MemoryGB)) - [math]::Round(($_.ProvisionedSpaceGB - $_.MemoryGB) - $_.UsedSpaceGB))}},
@{N='Notes';E={$_.Notes.replace("`n",', ')}}

$vmcount = $report | Measure-Object "Name"
$sum = $report | Measure-Object -sum "CPU", "Memory (GB)", "Capacity(GB)", "UsedSpace(GB)", "FreeSpace(GB)"
$row1 = "" | Select "Folder", "Name", "IP Address", "CPU", "Memory (GB)", "Capacity(GB)", "UsedSpace(GB)", "FreeSpace(GB)", "FreeSpace(%)", "VM PowerState", "OS", Notes
$report += $row1
$row2 = "" | Select "Folder", "Name", "IP Address", "CPU", "Memory (GB)", "Capacity(GB)", "UsedSpace(GB)", "FreeSpace(GB)", "FreeSpace(%)", "VM PowerState", "OS", Notes
$row2."CPU" = 'Total CPU'
$row2."Memory (GB)" = 'Total Memory (GB)'
$row2."Capacity(GB)" = 'Total Provisioned(GB)'
$row2."UsedSpace(GB)" = 'Total UsedSpace(GB)'
$row2."FreeSpace(GB)" = 'Total FreeSpace(GB)'
$report += $row2
$row3 = "" | Select "Folder", "Name", "IP Address", "CPU", "Memory (GB)", "Capacity(GB)", "UsedSpace(GB)", "FreeSpace(GB)", "FreeSpace(%)", "VM PowerState", "OS"
$row3.CPU = $sum | where {$_.Property -eq 'CPU'} | select -ExpandProperty Sum
$row3."Memory (GB)" = $sum | where {$_.Property -eq "Memory (GB)"} | select -ExpandProperty Sum
$row3."Capacity(GB)" = $sum | where {$_.Property -eq "Capacity(GB)"} | select -ExpandProperty Sum
$row3."UsedSpace(GB)" = $sum | where {$_.Property -eq "UsedSpace(GB)"} | select -ExpandProperty Sum
$row3."FreeSpace(GB)" = $sum | where {$_.Property -eq "FreeSpace(GB)"} | select -ExpandProperty Sum
$report += $row3
$row4 = "" | Select "Folder", "Name"
$row4."Folder" = 'Total VMs'
$row4.Name = $vmcount | where {$_.Property -eq 'Name'} | select -ExpandProperty Count
$report += $row4
$row5 = "" | Select "Folder", "Name"
$row5."Folder" = 'Powered On VMs'
$row5.Name = (Get-VM -Location $Folder | where {$_.PowerState -eq 'PoweredOn'}).count
$report += $row5
$row6 = "" | Select "Folder", "Name"
$row6."Folder" = 'Powered Off VMs'
$row6.Name = (Get-VM -Location $Folder | where {$_.PowerState -eq 'PoweredOff'}).count
$report += $row6
$row7 = "" | Select "Folder", "Name"
$row7."Folder" = 'Windows VMs'
$row7.Name = (Get-VM -Location $Folder | Get-View | where {$_.summary.config.guestFullName -match 'Windows'}).count
$report += $row7
$row8 = "" | Select "Folder", "Name"
$row8."Folder" = 'CentOS VMs'
$row8.Name = (Get-VM -Location $Folder | Get-View | where {$_.summary.config.guestFullName -match 'CentOS' -or $_.summary.config.guestFullName -match 'Red Hat'}).count
$report += $row8
$report
}
}

foreach($dc in Get-Datacenter){
$fld = Get-Folder -Name vm -Type VM -Location $dc
$recursion = $true
$wsName = "$($dc.Name)-All-VMs"
$report = get-foldersummary -Folder $fld -Recursion:$recursion
if($report){
$report | select "Folder", "Name", "IP Address", "CPU", "Memory (GB)", "Capacity(GB)", "UsedSpace(GB)", "FreeSpace(GB)", "FreeSpace(%)", "VM PowerState", "OS", Notes |
Export-Excel -Path $reportlocation -AutoFilter -AutoSize -WorksheetName $wsName
}
$recursion = $false
foreach ($fld in Get-Folder -Type VM -Location $dc) {
$wsName = $fld.Name
$report = get-foldersummary -Folder $fld -Recursion:$recursion
if($report){
$report | select "Folder", "Name", "IP Address", "CPU", "Memory (GB)", "Capacity(GB)", "UsedSpace(GB)", "FreeSpace(GB)", "FreeSpace(%)", "VM PowerState", "OS", Notes |
Export-Excel -Path $reportlocation -AutoFilter -AutoSize -WorksheetName $wsName
}
}
}

0 Kudos
7 Replies
LucD
Leadership
Leadership

What do you mean by "incorrect values"?
Against which values did you compare?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot

Hi LucD,

The calculation for UsedSpace(GB), FreeSpace(GB), and FreeSpace(% ) of these shows negative or wrong values in the output

0 Kudos
LucD
Leadership
Leadership

I just tested against several of my VMs, and besides an issue with the FreeSpace% (you forgot to multiply by 100), all returned results are correct.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot

LucD,

I made few changes but still not getting correct info. I have attached the script for your reference.

Also we have few CentOS Server where docker is running for those, I see the Capacity is calculated on number of containers running. If the docker has 10 containers and my disk size is 40 GB it would be calculated as 40 GB * 10 Nos 😞

ganapa2000_0-1654757978540.png

 

0 Kudos
LucD
Leadership
Leadership

You are using the Guest OS view of disk usage.
If docker, or whatever app, maps filesystems on harddisks, you can not use those filesystem sizes to calculate total capacity and usage.
In your case, docker maps a filesystem of a specific size on a harddisk, but you will never be able to use all those filesystems to their full extent (since the physical size is limited by the harddisk size).

You are counting overcommited storage as actual storage.
That is incorrect


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot

LucD,

then what is the best way to get the disk details for non docker VMs

0 Kudos
LucD
Leadership
Leadership

Depends what exactly you want to capture.

Is it the view from the outside?
Then Get-Harddisk is the way to go.

Is it what the Guest OS sees?
Then the $vm.Guest.Disks (when available) is the way to go.

But be aware that there is no foolproof way to map one to the other for all situations.
As the docker experience has shown you


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos