Options

VMware View 6 Power CLI

DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
Hi,

I have a script to run to change CPU/Memory and so forth on guest desktops in vmware view, but one of the things I need to happen is to first place the desktops in to "maintenance mode" in vmware view. I know View has its own power cli on the connections servers but can any one tell me if there is a simple straight forward way in script to put a machine in maintenance mode and bring it back out?

Our view is set to always power on machines so unless it is disabled the desktops reboot before the script is complete. and I only want to run the script on a sub set of desktops and don't want to make configuration changed on the pool.
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)
}
 
## csv with header line vmname,cpu,mem  (memory in GBytes) 
$vmlist = Import-CSV c:\temp\csvfile.csv


 
 ## initiate guest shut down, shutdown returnes imideatly does not wait for conformation. conformation set to false to prevent user interaction requirments
 write-host ("Shutting Down VM's")
foreach ($item in $vmlist) {
    
    $vmname = $item.vmname
    Shutdown-VMGuest -VM $vmname -Confirm:$false #-RunAsync 
    }
    
    write-host ("Checking")
    
    ## all machines need to be powered off before script can continue, run through list of machines every 2 seconds and only continue once all showing powered off
    do {
    Start-Sleep -s 2
    $power="off"
    foreach ($item in $vmlist) {
    
      #Check the power status
      $MyVM = Get-VM -Name $item.vmname
      $status = $MyVM.PowerState
      write-host ($item.vmname +" " + $status)
      if ($status -eq "PoweredOn") { $power = "ON" }
      }
      }until($power -eq "off")
   
    write-host ("All machines shut down, Waiting 20 seconds")
   
  ## had issues when directly continuining so give VCenter 20 seconds to complete all operations 
   Start-Sleep -s 20
      
    
    write-host ("Changing CPU and Memory")
    
    ##call function to update CPU cores and memory size
foreach ($item in $vmlist) {


    $vmname = $item.vmname
    $cpu = $item.cpu
    $mem = [int]$item.mem * 1024
    Enable-MemHotAdd $vmname
    Enable-vCpuHotAdd $vmname
    Set-VM -VM $vmname -NumCpu $cpu -MemoryMB $mem -RunAsync -Confirm:$false
    }


## remove existing NIC and replace with vmxnet3 
write-host ("Changing NIC")
foreach ($item in $vmlist) {
write-host ("Setting VMXnet3 Network for " + $item.vmname)
   Get-NetworkAdapter -vm $item.vmname | remove-networkadapter -confirm:$False
   New-NetworkAdapter -vm $item.vmname -Type vmxnet3 -Portgroup "Production LAN" -StartConnected | Out-Null


    }
    
    
# dont restart machines, need to take out of maintence mode on view for the graphic memory to correctly get updated. 






#foreach ($item in $vmlist) {
#    $vmname = $item.vmname
#    Start-VM -VM $vmname -RunAsync 
#    }
    


  
  • If you can't explain it simply, you don't understand it well enough. Albert Einstein
  • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
Sign In or Register to comment.