We have a training VCenter which we run courses for students.
We have a datacenter containing resource pools for each course we run. Each resource pool contains multiple VMs relevant to the course.
We have another datacenter which contains the hosts we deploy student environments to. Each student environment is contained in a resource pool.
I need to create a Powercli script which would calculate the percentage of free storage, cpu and memory left on each host if we deployed a course.
For example.
Course is starting with 6 students and I have provisioned 3 hosts for this course. 2 student environments per host.
Prior to deploying the VMs I want to check if my hosts can accommodate all the VMs by making sure there is atleast x percentage of Storage, Memory and CPU
I have started writing a script to do a storage check however I' am not getting accurate results and when deploying all student environments to a single host I get the error below the script.
SCRIPT
param($folder, $resourcePool, $students)
Function Percentcal {
param(
[parameter(Mandatory = $true)]
[int]$InputNum1,
[parameter(Mandatory = $true)]
[int]$InputNum2)
$size = (get-vm -location $resourcePool | get-harddisk | measure-object -property capacityGB -sum | select-object -ExpandProperty sum)
$realsize = [math]::Round($size*1024)
$totalsize = $realsize*$students
$totalvmhosts = $hosts.count
$sizeperhost = $totalsize / $totalvmhosts
$realsizeperhost = [math]::Round($sizeperhost)
$actualfreespace = $InputNum1-$realsizeperhost
$actualfreespace / $InputNum2*100
}
$hosts = Get-VMHost -Location $folder
ForEach ($vhost in $hosts)
{
$ds = Get-Datastore -VMHost $vhost | Where-Object {$_.Name -like "datastore*"} | Sort Name
if ($ds.Name)
{
$PercentFree = Percentcal $ds.FreeSpaceMB $ds.CapacityMB
$PercentFree = "{0:N2}" -f $PercentFree
$ds | Add-Member -type NoteProperty -name PercentFree -value $PercentFree
}
if([int]$PercentFree -gt 5){
Write-Host -ForegroundColor "Yellow" ("Storage on $vhost is OK ")
}
else {
Write-Host -ForegroundColor "Red" ("Storage on $vhost is Insufficient ")
}
}
$ds | Select Name,FreeSpaceMB,CapacityMB,PercentFree | Export-Csv c:\Scripts\Dev\datastorereport.csv -NoTypeInformation
ERROR
Cannot convert value "-Infinity" to type "System.Int32". Error: "Input string was not in a correct
format."
At C:\Scripts\Dev\StorageCheck.ps1:29 char:23
+ if([int]$PercentFree <<<< -gt 5){
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException