Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 130
  • Last Modified:

PowerShell to calculate Datastore Free Space in Percentage

We always maintain at-least 15% Free Space in VMware Datastores. Sometimes requirements come to allocate additional storage space to servers from the existing datastores. In that case we get confused about how much space should we allocate so that we can maintain more than 15% datastore space.

I am preparing following script to calculate VMware Datastore Free Space in Percentage. The script is in initial stage and running fine as of now. It will prompt to input datastore Capacity, Free Space and Space that needs to be reclaimed. It will then calculate whether 15% free space will be maintained or not after reclaiming the required storage space.

Can you please help me to add the the following conditions in the script as well-

1. It will force user to input only numbers.
2. It will not accept '0' (zero) in the input field.
3. User cannot left the input fields blank. It will force user to input.
Calculator
0
cbinayak
Asked:
cbinayak
  • 3
  • 3
  • 2
1 Solution
 
oBdACommented:
Use this instead of Read-Host:
Function Read-Integer([string]$Prompt) {
	Do {
		$InputOK = $False
		$Input = Read-Host -Prompt $Prompt
		$Input = $Input.Trim()
		If ([string]::IsNullOrEmpty($Input)) {
			Write-Host "Empty input is not allowed!" -ForegroundColor Red
			Continue
		}
		$Integer = 0
		If (-Not [uint64]::TryParse($Input, [ref]$Integer)) {
			Write-Host "Input '$($Input)' is not a positive integer!" -ForegroundColor Red
			Continue
		}
		If ($Integer -eq 0) {
			Write-Host "Input '0' is not allowed!" -ForegroundColor Red
			Continue
		}
		$InputOK = $True
	} Until ($InputOK)
	Return $Integer
}

Open in new window

0
 
cbinayakAuthor Commented:
I am not that much expert in PowerShell. Can you please let me know where to include this code more specifically?
0
 
oBdACommented:
For example starting in the current line 10, below the end of the CalcPercent function.
Then wherever you want a positive integer (currently lines 11, 12, 13), replace the Read-Host with the new Read-Integer call.

And the next time you post code, please use "Style Code" tags (see tool bar above the Comment block), not a screenshot.
0
Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

 
David Johnson, CD, MVPOwnerCommented:
Why do you have to input the values? Can you not get the total drive space and space used by querying the drive itself?
$VMhost = HostSystem-host-8
$disks = Get-Datastore -VMHost (Get-VMHost -Id $VMHost)
$capacity = $disks.capacityMB
$freespace = $disks.FreeSpaceMB
if ($freespace -le ($capacity *0.15)) { # less than 15% available) }; else { #more than 15% available }

Open in new window

0
 
cbinayakAuthor Commented:
@oBdA

I have slightly modified the existing code. It would be very helpful if you can help me in modifying my code with yours. Here is the code -
cls

Write-Host "*******************************************************************************"
Write-Host -ForegroundColor Green "CALCULATOR to calculate FREE SPACE PERCENTAGE after reclaiming storage space"
Write-Host "*******************************************************************************"
Write-Host ""

function CalcPercent {
    param(
    [parameter(Mandatory = $true)]
    [int]$InputNum1,
    [parameter(Mandatory = $true)]
    [int]$InputNum2)
    $InputNum1 / $InputNum2*100
}

$CapacityMB = Read-Host -Prompt 'Input Datastore CAPACITY in MB '
$A = [Math]::Round($CapacityMB)

$CurrentFreeSpaceMB = Read-Host -Prompt 'Input Datastore CURRENT FREE SPACE in MB '
$B = [Math]::Round($CurrentFreeSpaceMB)

$SpaceToReclaimMB = Read-Host -Prompt 'Input Space that you want to RECLAIM in MB '
$C = [Math]::Round($SpaceToReclaimMB)

$FreeSpaceMB = [float]$B-$C
$D = [Math]::Round($FreeSpaceMB)

$PercentFree = CalcPercent $D $A
$PercentFree = "{0:N2}" -f $PercentFree
$E = [Math]::Round($PercentFree)

Write-Host ""
Write-Host -ForegroundColor Yellow "Datastore Free Space will be $PercentFree % after reclaiming $SpaceToReclaimMB MB storage"

if ($E -lt 15)
{
	Write-Host -BackgroundColor Red "Free space is less than 15%. You have to add additional storage before you reclaim $SpaceToReclaimMB MB."
}
else
{
	Write-Host -ForegroundColor Green "YOU CAN GO AHEAD WITH RECLAIMING $SpaceToReclaimMB MB OF SPACE."
}

Open in new window

0
 
oBdACommented:
Maybe some comments:
- You start out "correctly", by assigning the input to variables with meaningful names. Why do you then continue with using $A, $B, etc, when two lines below, you probably can't remember yourself anymore which variable holds which value?
- The same is true for function arguments - make them self-explaining; "InputNum" isn't.
- Line 14: Careful when calculating with integers. Powershell is flexible enough to make it work, but if you were working with real integers (for example in a batch script), "FreeSpaceMB / CapacityMB * 100" would always return 0, because the division would be calculated first and return 0.
- Line 29: your function will return an integer or a float, depending on the values passed. You then turn that into a string with two decimal places, and then turn that string back into a float and finally round it back to an integer. Aside from that being overly complicated: if the free percentage was 14.51, Round() will turn that into a full 15, so your script might return incorrect results.
- Powershell functions should follow the Cmdlet naming rules, that is, <verb>-<noun>. <verb> should be from the list obtained with Get-Verb.

That said, here's the pimped version:
cls

Write-Host "*******************************************************************************"
Write-Host "CALCULATOR to calculate FREE SPACE PERCENTAGE after reclaiming storage space" -ForegroundColor Green 
Write-Host "*******************************************************************************"
Write-Host ""

Function ConvertTo-Percent {
Param(
	[parameter(Mandatory = $true)][Alias('Numerator')]
	[int]$Antecedent,
	[parameter(Mandatory = $true)][Alias('Denominator')]
	[int]$Consequent,
	[int]$DecimalPlaces = 0
)
	Return [math]::Round((100 * $Antecedent) / $Consequent, $DecimalPlaces)
}

Function Read-Integer([string]$Prompt) {
	Do {
		$InputOK = $False
		$Input = Read-Host -Prompt $Prompt
		$Input = $Input.Trim()
		If ([string]::IsNullOrEmpty($Input)) {
			Write-Host "Empty input is not allowed!" -ForegroundColor Red
			Continue
		}
		$Integer = 0
		If (-Not [uint64]::TryParse($Input, [ref]$Integer)) {
			Write-Host "Input '$($Input)' is not a positive integer!" -ForegroundColor Red
			Continue
		}
		If ($Integer -eq 0) {
			Write-Host "Input '0' is not allowed!" -ForegroundColor Red
			Continue
		}
		$InputOK = $True
	} Until ($InputOK)
	Return $Integer
}

$CapacityMB = Read-Integer -Prompt 'Input Datastore CAPACITY in MB (no decimal places) '

$CurrentFreeSpaceMB = Read-Integer -Prompt 'Input Datastore CURRENT FREE SPACE in MB (no decimal places) '

$SpaceToReclaimMB = Read-Integer -Prompt 'Input Space that you want to RECLAIM in MB (no decimal places) '

$FreeSpaceMB = $CurrentFreeSpaceMB - $SpaceToReclaimMB

$PercentFree = ConvertTo-Percent $FreeSpaceMB $CapacityMB -DecimalPlaces 2

Write-Host ""
Write-Host -ForegroundColor Yellow "Datastore Free Space will be $PercentFree % after reclaiming $SpaceToReclaimMB MB storage"

if ($PercentFree -lt 15)
{
	Write-Host -BackgroundColor Red "Free space is less than 15%. You have to add additional storage before you reclaim $SpaceToReclaimMB MB."
}
else
{
	Write-Host -ForegroundColor Green "YOU CAN GO AHEAD WITH RECLAIMING $SpaceToReclaimMB MB OF SPACE."
}

Open in new window

0
 
cbinayakAuthor Commented:
Excellent oBdA. Many many thanks for your suggestion and specially for the code. I will try that and let you know the status.
0
 
David Johnson, CD, MVPOwnerCommented:
Asker liked code then disappeared
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now