I’m trying to script the detection of freespace of dedicated virtual desktops in our VMware View environment. Based on the freespace percentage returned, I’m hoping to resize the disk in vSphere via PowerCLI cmdlets, then within the guest OS itself.

However, the Set-HardDisk cmdlet requires a static value in capacity parameter. I’m trying to use my 7th grade math skills here to simply grow the disk by 5% (I’m not sure if that’s what I’ll end up using yet), but the math doesn’t return correctly.

For example, CONTOSOVDIIMJK1 should resolve to a optimal size of a bigger number, not smaller

Any ideas?

CONTOSOVDIIMJK1 has 4.531363378868 percent disk space free! Optimal Size on CONTOSOVDIIMJK1 is 3.54681161499 GBs CONTOSOVDIIMPO1 has 4.89884197741 percent disk space free! Optimal Size on CONTOSOVDIIMPO1 is .1484657876 GBs CONTOSOVDIDOMP1 has 3.57813656517171 percent disk space free! Optimal Size on CONTOSOVDIDOMP1 is 1.87841148376465 GBs CONTOSOVDIIMSG1 has 4.88974833 percent disk space free! Optimal Size on CONTOSOVDIIMSG1 is .14666919971 GBs CONTOSOVDIDVBS1 has 2.755413333 percent disk space free! Optimal Size on CONTOSOVDIDVBS1 is 1.863451376 GBs

$colVDMs = Get-ADComputer -SearchBase "ou=computers,dc=contoso,dc=com" -Filter * | Where-Object {$_.name -like "*VDI*"} | Select-Object -ExpandProperty Name foreach ($vdm in $colVDMs) { $wmiDriveSize = Get-WmiObject win32_logicaldisk -ComputerName $vdm | Where-Object {$_.drivetype -eq 3 -and $_.deviceID -eq "C:"} $totalSpaceGB= ($wmiDriveSize.Size/1073741824) $freeSpaceGB= ($wmiDriveSize.FreeSpace/1073741824) $freePercent = ($freeSpaceGB/$totalSpaceGB)*100 if ($freePercent -lt 5) { Write-Host "$vdm has $freePercent percent disk space free!"$optimalGB = (($freeSpaceGB) * 1.05)Write-Host "Optimal Size on $vdm is $optimalGB GBs" #Get-HardDisk -VM $vdm | Set-HardDisk -CapacityGB }#if }#foreach