Link to home
Start Free TrialLog in
Avatar of stressedout2004
stressedout2004

asked on

Mas Decrease of CPU for VMware ESX Virtual Machines

Hello,
I am trying to reduce the number of CPU's on guest VM's to help stop CO Stop issues.   The goal is to read in a listing of VM Names and CPU size from a file.  
Get VMName from file
Power Off VM
Set VM with CPU change
Power on VM
Check if the VM was powered on.    

Below is some code I attempted to use, but its for CPU Hot ADD. Thanks

Connect-VIServer "vCenter Address"
Function Enable-MemHotAdd($vm){
    $vmview = Get-vm $vm | Get-View
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="mem.hotadd"
    $extra.Value="true"
    $vmConfigSpec.extraconfig += $extra

    $vmview.ReconfigVM($vmConfigSpec)
}

Function Enable-vCpuHotAdd($vm){
    $vmview = Get-vm $vm | Get-View
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="vcpu.hotadd"
    $extra.Value="true"
    $vmConfigSpec.extraconfig += $extra

    $vmview.ReconfigVM($vmConfigSpec)
}

$vmlist = Import-CSV C:\Input.csv

foreach ($item in $vmlist) {
    $vmname = $item.vmname
    Shutdown-VMGuest -VM $vmname -Confirm:$False
      do {
      Write-Host "Waiting On Shutdown For VM: " $vmname
      $vm = Get-VM -Name $vmname
      $Status = $vm.PowerState
      Write-Host "Powerstate For $vmname is: " $Status
      Sleep 10
      }until($status -eq "PoweredOff")
}

foreach ($item in $vmlist) {
    $vmname = $item.vmname
      $cpu = $item.cpu
      $cores = $item.cores
    $mem = [int]$item.mem * 1024
    $spec = New-Object -Type VMware.Vim.VirtualMachineConfigSpec -Property @{'NumCoresPerSocket' = $cores;"NumCPUs" = $cpu}
      (Get-VM $vmname).ExtensionData.ReconfigVM_Task($spec)
      Set-VM -VM $vmname -MemoryMB $mem -RunAsync -Confirm:$false
      Enable-MemHotAdd $vmname
    Enable-vCpuHotAdd $vmname
}

foreach ($item in $vmlist) {
    $vmname = $item.vmname
    Start-VM -VM $vmname -RunAsync
    }
Disconnect-VIServer *
Avatar of arnold
arnold
Flag of United States of America image

changing VCpus you risk impacting ...

how many did you allocate to the VMs from the begining?

Depending on what issue you are dealing with, it might be an issue with prioritization?
I.e. your current setup each system has equal weight access to resources, cpu, storage, memory
You can adjust the prioritization such that a higher utilization system has more time compared to others.

i.e. in a two VM setup web server, sql server, you would prioritize the sql server at a two to 1 ration with the web server.
instead of 1 to 1.
I.e. give more time/priority to the system doing more work.

i.e. if you have two people with one who depends on the other. Usually you would give the one on whom others depend more time and resources...

not familar with the scripting you are trying to run, are you accessing the host via vSphere client, or you have vcsa setup through which you manage hosts and vms?
ASKER CERTIFIED SOLUTION
Avatar of stressedout2004
stressedout2004

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial