How i can change any VM vCpu number?

Well read below!

Easy with Powercli!

The script below, will shut down the VM, change vCpu number and start the VM.

You can see the result:

image-01

You must enter a VM Name!

image-02

The script check if a VM is already Powered Off.

image-03

And now the script:

clear-host

function CheckState
{
$vm = get-vm $vm
if($vm.PowerState -eq "PoweredOn")
{
write-host "VM Still Up, waiting for 10 seconds before new check" -foregroundcolor Yellow
write-host ""
sleep 10
CheckState
}
Else
{
write-host "VM $VM is Powered Off" -foregroundcolor green
}
}

#Shut Down VM and change vCpu Number
$vm = read-host "Enter VM Name"
write-host ""

if($vm -eq "$null")
{
write-host "No server entered, Exit script..." -foregroundcolor Yellow;
write-host " "
exit
}

write-host ""
write-host "VM $VM will be shutdown" -foregroundcolor yellow
write-host ""
$vm = get-vm $vm

#Check if VM is powered down"
if($vm.PowerState -eq "PoweredOn")
{
$Result = Shutdown-VMGuest -VM $vm -confirm:$False
}
Else
{
write-host "VM $vm is already Powered Off"
}

CheckState

write-host ""
$CpuCount = $vm.Numcpu
Write-host "Current VM $vm has: " -nonewline
Write-host $Cpucount -foregroundcolor Green -nonewline
write-host " vCpu" 
write-host ""

$vCpu = read-host "Enter vCpu Number : 2 vCpu for DataBase Server, 1 vCpu for others"
write-host ""
$NewCPU = Set-VM -VM $vm -NumCpu $vcpu -Confirm:$false
write-host ""
write-host "VM $vm has now: " -nonewline
write-host $vCpu -foregroundcolor green -nonewline
write-host " vCpu"
write-host ""

#Start VM
$Start = Start-VM $vm
Write-host "VM $vm is starting" -foregroundcolor Green
write-host ""