VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to add CPU to VM after validation

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
}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Did you check that this VM has hot-adding CPUs enabled?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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

Tags (1)
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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.

ganapa2000_0-1656316582561.png

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

the script should fail if the output is zero or negative. which I am unable to achieve. 😞

Please help!!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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
}

 

 

0 Kudos