I'm doing it wrong - calculation not working

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 :frowning:

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

Well, you’re only multiplying your Free Space by 1.05, instead of your Total Space. Other than that, your code looks fine.

You know that feeling when you haven’t had enough coffee and just posted a stupid question on the Internet? Sigh.

Thanks, Dave!

No problem! :slight_smile: BTW, I don’t know if you’re aware of this, but you don’t need to hard-code the value 1073741824. PowerShell gives you a more readable version of that: 1GB

$totalSpaceGB = $wmiDriveSize.Size / 1GB
$freeSpaceGB = $wmiDriveSize.FreeSpace / 1GB