I’m going to be publishing some of the scripts I have written for VMM.  These are each based on specific customer asks or conversations that lead me to go sleepless in trying to solve the problems myself.  I am very interested in your feedback, especially if you would like to offer a change to improve efficiency or effectiveness.

QuickChange

The idea behind this script is to accelerate changing the configuration of a VM so the amount of downtime required is minimized.  The script focuses on Memory and Processor Count since those are the two values that require reboot.  Performing this change “manually” meant multiple clicks and dialogue navigations.  In automating the change via script I can typically reduce downtime to under 60 seconds.

After a lot of experimentation, I also implemented a choice menu in the script so before committing the change and starting the process.  You will be prompted to either continue, continue with intelligent placement, or stop.  The idea is if you are adjusting the amount of memory in a VM to a significantly higher amount, there may not be available memory on the same host to start it.  Rather than waiting for the cluster to figure this out and move a VM, the script does a quick check for the best host across the cluster, moves it, and then starts it.  Note – the “best host” may still not have enough memory available, so some common-sense qualification of the memory amount is recommended!

Notes

In each of the scripts for this series, I have noted any manually assigned variables at the top of the script just under the header.  For this script you will want to update your VMM server name and the Host Group name.

In each of the scripts for this series, I have added a line to load the VMM snap-in.  This way it can be run from any machine, including a workstation, where the VMM console is installed by right clicking on the file and choosing “run”.  If you run the script from within VMM you will get a few lines of error text that the snap-in is already loaded.  You can either ignore this, or comment out the line if it bothers you.

Screen Shots

image

Script starts by giving you a list of VMs.  No error checking on this value, if you fat-finger the name the script would try to run but end in error that a VM by that name does not exist.  See the snap-in error at the top?  Since I launch from VMM, that is expected.  I just ignore it, feel free to # it out.

image

The next screen gives you the current values for the VM and asks you what you would like to change them to.  In this case I kept the values the same.  The script doesn’t care, it assumes the person running the script is smart enough to know and still wants to make the “change”.  The way I see it, the script should never argue with me.

image

Script prompting for confirmation.  Just to be fancy, I did put in text for the help values, as demonstrated above.

image

The script finishes by returning the status of the VM as each of the jobs run.  Nothing pretty.  If you want pretty, switch over to the VMM console.  You can see the shutdown, change, and start.  If a move had occurred you would see that listed as well.  In this case the VM was already on the best host.  Not too bad, from shutdown to startup completing it took 46 seconds.

Script

disclaimer: this script is not supported in any way.  I have posted the code rather than the .ps1 file so that you can review it, modify it to make it your own, and test it before trusting it in a production environment.  now this is your code, I am not responsible for its use.

(Modified Script on 10/7 on line 22, avoiding move to same host)

   1: # ------------------------------------------------------------------------------
   2: # Quick Change
   3: # ------------------------------------------------------------------------------
   4: # blogs.technet.com/offcampus
   5: # version 1.0
   6: #
   7: # Description
   8: #  Useful for quickly adjusting CPU count and Memory of a VM
   9: #
  10: # ------------------------------------------------------------------------------
  11:  
  12: # All variables in the script with customizable values are mapped here for convenience
  13: $VMMServerName = "v-vmm-01"
  14: $HostGroupName = "All Hosts"
  15:  
  16: # Functions
  17: function SetVmProperties ($CPUCount, $Memory, $MoveChoice)
  18: {
  19:     Shutdown-VM $Name
  20:     Set-VM -vm $Name -CPUCount $CPUCount -MemoryMB $Memory
  21:     if ($Move -eq 1) {
  22:         $VMHostRating = Get-VMHostRating -VM $Name -VMHostGroup $HostGroupName -ismigration | ? {$_.Name -ne $Name.VMHost} | sort -property rating -descending
  23:         Move-VM -vm $Name -vmhost $VMHostRating[0].name -RunAsynchronously -StartVMOnTarget
  24:         }
  25:     Else {Start-VM $Name}
  26: }
  27:  
  28: # Load Snap-Ins
  29: Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager 
  30:  
  31: # Begin Script
  32: Get-VMMServer $VMMServerName | out-null
  33: Get-VM | select Name | Format-Table
  34: $Name = Read-Host "Which VM would you like to modify?"
  35: cls
  36:  
  37: Write-Host "That VM currently has the following properties:"
  38: Get-VM $Name | select cpucount, memory | Format-List
  39: $CPUCount = Read-Host "How many CPU's would you like?"
  40: $MemoryMB = Read-Host "How much Memory would you like? (MB)"
  41: cls
  42:  
  43: $Caption = "Confirm Reboot"
  44: $Message = "Please confirm you would like to reboot $Name to make the change?"
  45: $Move = 0
  46: $Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&1. Yes", `
  47:     "Shutdown the VM, set the CPU Count and Memory values, and then attempt to start on the same host."
  48: $Move = New-Object System.Management.Automation.Host.ChoiceDescription "&2. Yes, and move it to the best host", `
  49:     "Shutdown the VM, set the CPU Count and Memory values, query VMM for intelligent placement, move the VM to the best host, and then attempt to start the VM."
  50: $No = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
  51:     "Do nothing.  Exit Script."
  52: $Choices = [System.Management.Automation.Host.ChoiceDescription[]] @($Yes,$Move,$No)
  53:     $choiceRTN = $host.ui.PromptForChoice($Caption,$Message,$Choices,2)
  54: switch($choiceRTN)
  55: {
  56:  0    { SetVMProperties $CPUcount $MemoryMB }
  57:  1    { $Move = 0
  58:         SetVMProperties $CPUcount $MemoryMB }
  59:  2    { Write-Host "Exiting script.  No changes were made."
  60:         Start-Sleep 5
  61:         Exit }
  62: }