Hi,
I am unable to add the CPU to the VMs after the successful validation. I am getting the Validation Success but CPU is not getting added.
Please help
$folder = 'L&D Team'
$serv = "POCbuild12"
$TotalCPU = 12
$UsedCPU = (Get-Folder $folder | Get-VM | Measure-Object -Property numcpu -Sum).Sum
$cpu = $TotalCPU - $UsedCPU
$assigned = 2
$avail = $cpu - $assigned
$currcpu = (Get-Folder $folder | Get-VM $serv | Measure-Object -Property numcpu -Sum).Sum
$addcpu = $currcpu+$assigned
if (($avail) -le $cpu) {
Write-Host "Validation Failed as budget not available $avail"
return
}
else {
Write-Host "Validation Success, Current available CPU Budget after $assigned CPU assignment is $avail"
Get-VM -name $serv | Set-VM -NumCpu $addcpu -confirm:$false
}
Then you should adapt your test accordingly.
SOmething like this perhaps?
if ($avail -le $cpu -and ($cpu - $avail) -gt 0){
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Did you check that this VM has hot-adding CPUs enabled?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Yes, CPU Hotplug is enabled.
When I run the below script directly, it works. but using the above validation it is not working 😞
Get-VM -name myvm | Set-VM -NumCpu 2 -confirm:$false
Then you will have to check what is in all the variables involved.
Also, are these Write-Host cmdlets showing output?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I made changes as below putting write-host in every step where ever needed. the validation is not working as expected.
even when the quota exceed, I am still able to assign the CPU
$folder = 'L&D Team'
$serv = "POCbuild12"
$TotalCPU = 14
$UsedCPU = (Get-Folder $folder | Get-VM | Measure-Object -Property numcpu -Sum).Sum
$cpu = $TotalCPU - $UsedCPU
Write-Host "Available CPU Budget is $cpu"
$assigned = 2
$avail = $cpu - $assigned
Write-Host "After Assigning $assigned CPU, Available CPU Budget is $avail"
$currcpu = (Get-Folder $folder | Get-VM $serv | Measure-Object -Property numcpu -Sum).Sum
$addcpu = $currcpu + $assigned
if (($avail) -le $cpu) {
Write-Host "Validation Success, Current available CPU Budget after $assigned CPU assignment is $avail"
Get-VM -name $serv | Set-VM -NumCpu $addcpu -confirm:$false
}
else {
Write-Host "Validation Failed as budget not available $avail"
return
}
When the quota is zero or negative, ideally the script should fail but it is assigning the CPU.
I'm not sure what the issue is.
In fact, 2 is less than 4 and -2 is less than 0, so your test is returning $true in both cases.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
the script should fail if the output is zero or negative. which I am unable to achieve. 😞
Please help!!
Then you should adapt your test accordingly.
SOmething like this perhaps?
if ($avail -le $cpu -and ($cpu - $avail) -gt 0){
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Thanks for your help and support. I tried as below and that worked.
$folder = 'L&D Team'
$serv = "POCbuild12"
$TotalCPU = 11
$UsedCPU = (Get-Folder $folder | Get-VM | Measure-Object -Property numcpu -Sum).Sum
$cpu = $TotalCPU - $UsedCPU
Write-Host "Available CPU Budget is $cpu"
$assigned = 1
$avail = $cpu - $assigned
Write-Host "After Assigning $assigned CPU, Available CPU Budget is $avail"
$currcpu = (Get-Folder $folder | Get-VM $serv | Measure-Object -Property numcpu -Sum).Sum
$addcpu = $currcpu + $assigned
if (($avail) -lt 0) {
Write-Host "Validation Failed as budget not available $avail"
return
}
else {
Write-Host "Validation Success, Current available CPU Budget after $assigned CPU assignment is $avail"
Get-VM -name $serv | Set-VM -NumCpu $addcpu -confirm:$false
}