VMware Cloud Community
jeffreywmcclain
Enthusiast
Enthusiast
Jump to solution

Create Report of ONLY non-Thin VM Disks and their amount and percentage of Free Space?

I found these 2 threads that seem promising:

However, neither does precisely what I want (report on non-thin disks only).

I tried combining elements from both of these posts into my own script:

$report = foreach ($vm in Get-VM | Where-Object{$_.Name -notlike 'stCtlVM-*'}) {

    $disks = Get-HardDisk -VM $vm

    foreach ($disk in $disks) {

        if ($disk.StorageFormat -ne 'Thin') {

            [PSCustomObject]@{

                Server = $vm.Uid.Split(":")[0].Split("@")[1]

                VMName = $vm.Name

                VMId = $vm.Id

                FileName = $disk.FileName

                CapacityGB = $disk.CapacityGB

                FreeSpaceGB = $disk.CapacityGB - (($vm.extensiondata.layoutex.file|?{$_.name -contains $disk.filename.replace(".","-flat.")}).size/1GB)

            } 

        }

    }

}

$report | Sort-Object Server, VMName | Export-Csv -LiteralPath $file -NoTypeInformation -UseCulture -Force

The issue is "FreeSpaceGB" is typically either identical to "CapacityGB" or a negative number, indicating my math isn't correct.

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I'm afraid that you don't find the free space for Thick HD like that.
You are in fact subtracting the Capacity from the Capacity, which is zero (as you noticed).

The only way to get what I think you are after is by mapping the VMDK to a partition in the guest OS.
And there is currently no fool-proof way to do that I'm afraid.

That is also the reason why my function in Yadr – A Vdisk Reporter, reports the usage% for Thick disks as 'na'.

Btw, there is a potential issue in your script when the VM has snpashots.

Then the name suffix will not always be '-flat'.

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

Was it helpful? Let us know by completing this short survey here.


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

View solution in original post

6 Replies
LucD
Leadership
Leadership
Jump to solution

I'm afraid that you don't find the free space for Thick HD like that.
You are in fact subtracting the Capacity from the Capacity, which is zero (as you noticed).

The only way to get what I think you are after is by mapping the VMDK to a partition in the guest OS.
And there is currently no fool-proof way to do that I'm afraid.

That is also the reason why my function in Yadr – A Vdisk Reporter, reports the usage% for Thick disks as 'na'.

Btw, there is a potential issue in your script when the VM has snpashots.

Then the name suffix will not always be '-flat'.

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

Was it helpful? Let us know by completing this short survey here.


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

jeffreywmcclain
Enthusiast
Enthusiast
Jump to solution

Thanks for the fast reply!

Yeah, I figured it wasn't possible based on my research, but always nice to have it confirmed by an expert like you.

I'll mention this to my manager, I think the actual objective of this report is to show evidence that people are needlessly creating VMs with thick disks and then wasting space (e.g., VM with 500 GB thick disk, whereas when you log in only ~100GB are actually being used).

My current alternative plan is to use:

[math]::Round(($vm.guest.disks | measure-object freespacegb -sum | select -ExpandProperty sum),2)

which should at least calculate freespace for VMs with only a single thick disk.

I already have:

UsedSpaceGB = $vm.UsedSpaceGB

ProvisionedSpaceGB = $vm.ProvisionedSpaceGB

in a different report, but for thick disks these values are always the same.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can also total the size of the guest OS partions, provided VMware Tools are installed on the VM and the VM was powered on.
That also shows, on a high level, how much space the guest OS is actually using.

Get-VM |

Select Name,

@{N = 'ProvisionedSpaceGB'; E = { [math]::Round($_.ProvisionedSpaceGB, 1) } },

@{N = 'UsedSpaceGB'; E = { [math]::Round($_.UsedSpaceGB, 1) } },

@{N = 'CapacityOSGB'; E = { [math]::Round(($_.Guest.Disks | % { $_.CapacityGB } | Measure-Object -Sum).Sum, 1) } },

@{N = 'UsedOSGB'; E = { [math]::Round(($_.Guest.Disks | % { $_.CapacityGB - $_.FreeSpaceGB } | Measure-Object -Sum).Sum, 1) } }

 


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

jeffreywmcclain
Enthusiast
Enthusiast
Jump to solution

Thanks, I'll definitely keep that in mind.

My only concern with using "Guest.Disks" is that is requires VMWare tools to be installed and running, as you stated.

afaik the whole point of a "compliance audit" is by definition to make sure everyone is complying with department / company policies (not using thick disks unless absolutely necessary, not taking an excessive amount of thick disk space if it's not actually being used, etc). It seems like if someone simply powering off their VM or removing VM tools allows them to escape the audit, then it might be better to not even include those fields or it could be misleading to management (e.g. "look, only 5 VMs are using a bunch of thick space, that's not too bad" when in actuality there are 45 other VMs that are either powered off or don't have VM tools for some reason).

I understand there's no perfect solution, just weighing the different options. What I might do is simply report any VM that has at least 1 thick disk, then add those 4 columns from your last reply but with a caveat in the email that "disk space values might not be accurate if VM is powered off, doesn't have VM Tools, and/or has multiple disks".

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is indeed true.

The other alternative is to use a 3th party tool, which most of the time will also require an agent to be installed inside the guest OS.

That could give you the data for all disk space, independent if VMware Tools are installed or not.

But then again, this just moves the problem to another SW to rely upon :smileygrin:


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

0 Kudos
jeffreywmcclain
Enthusiast
Enthusiast
Jump to solution

For reference, I wound up going with this:

<#

Note1: ProvisionedSpaceGB, UsedSpaceGB, CapacityGB, and FreeSpaceGB calculate a sum total for all disks on a VM, both thick and thin.

Note2: For VMs with only thick disk(s), ProvisionedSpaceGB and UsedSpaceGB are identical.

Note3: CapacityGB and FreeSpaceGB are GuestOS properties, requiring a VM to be powered on and have VMWare tools running.

Note4: CapacityGB and FreeSpaceGB relate to the File Explorer disk usage view, but are most useful for VMs with a single disk (otherwise they will calculate a sum for all disks, thick and thin).

#>

$file = 'C:\Temp\test.csv'

$report = foreach ($vm in Get-VM | Where-Object{$_.Name -notlike 'stCtlVM-*'}) {

$infraction = $false

$disks = Get-HardDisk -VM $vm

foreach ($disk in $disks) {

        if ($disk.StorageFormat -ne 'Thin') {

        $infraction = $true

        }

}

if ($infraction) {

        [PSCustomObject]@{

Server = $vm.Uid.Split(":")[0].Split("@")[1]

                       VMHost = $vm.VMHost.Name

                       VMHostId = $vm.VMHost.Id

                       Name = $vm.Name

                       Id = $vm.Id

                       Storage = $disks.StorageFormat -join ' | '

                       ProvisionedSpaceGB = $vm.ProvisionedSpaceGB

                       UsedSpaceGB = $vm.UsedSpaceGB

                       CapacityGB = $vm.Guest.Disks | Measure-Object CapacityGB -Sum | Select-Object -ExpandProperty Sum

                       FreeSpaceGB = $vm.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select-Object -ExpandProperty Sum

        }

}

}

$report | Sort-Object Server, VMName | Export-Csv -LiteralPath $file -NoTypeInformation -UseCulture -Force

0 Kudos