Using PowerCLI to Shutdown a VM resize vCPU and Memory and Power back on!

You ever been in that situation of “Oh no, I need to modify a VM to increase/decrease the number of vCPUs and Memory, but I can’t do it during normal business hours” Or whatever the situation may be?  Well, fellow #vExpert @BoBolander and I were discussing this recently, and worked together to create this!  This also will apply if you’re doing this to multiple virtual machines!

Well, look no further. :) You can script this, as in to execute it as a script to happen while you’re at home asleep during your maintenance window, but also even if you’re modifying a simple VM and want the LEAST amount of time and effort possible, minimizing the downtime of the VM this will work also!

Shutting down your Virtual Machine via PowerCLI

This will seem simple, that is because it frankly is.  For the purpose of these descriptions I’m going to use a long VM name so you know this works if a VM has a name with quotes or spaces in it.  So for these purposes, the VM in question will be named, “VMware vCenter Log Insight 1.0.4”

Shutdown-VMGuest –VM “VMware vCenter Log Insight 1.0.4” –Confirm:$False

Wow, that was simple! So I run that and my virtual machine is shut down? Yes. Yes it is.

Changing your Virtual Machines vCPU and Memory

There you are, your VM is currently rocking out at 4GB of Memory and 1vCPU and you’re all “I really want this to be 8GB of memory and 2vCPUs! You know, because I did the analysis and notice that it’ll perform better, no I’m not just blindly following some vendors ‘best practices’” Ok, if you’re serious about this, here you go!

Set-VM “VMware vCenter Log Insight 1.0.4” –MemoryGB 8 –NumCPU 2 –Confirm:$False

Whoa, that was it? My VM now has its resources resized just like that? Yes. Just like that. You’re done!

Please Power my VM Back on! I have work to do!

Alright alright, you want to power your VM back on, you’ve made your change and you’re set! Well, here you go just as simply…

Start-VM –VM “VMware vCenter Log Insight 1.0.4”

And poof your VM will start back up and you’ll be set!

You can combine The Set-VM and Start-VM ya know!

Yea, that’s correct! You can instead run the last two components as a single one-liner command like;

Get-VM “VMware vCenter Log Insight 1.0.4” | Set-VM –MemoryGB 8 –NumCPU 2 –Confirm:$False | Start-VM

Unfortunately I’ve yet to find a way to combine the VM Shutdown component and the Set-VM Component without it breaking, thus…

Hey but wait, I thought you said this was scriptable

You’re all like, “I can’t just copy and paste these in sequence because my VM hasn’t shut down in time and I get an error trying to use CPU hot plug, and I can’t power the VM back on because it’s still shutting down!!!”    Yep, you got it, you’re absolutely correct! The initial steps were more to provide the steps of how you can go about modifying all of this from the PowerCLI command line, now if we’re really serious about this, here’s a multi-liner which will perform all of these tasks in sequence! Will this work if you don’t have VMware Tools installed? No. No it will not. Get your tools installed, ya’here! :)

  • $vm_name = Get-VM “VMware vCenter Log Insight 1.0.4”
  • ForEach ($vm in $vm_name){
  • $vm_name | Shutdown-VMGuest –Confirm:$False
  • Sleep 60
  • $vm_name | Set-VM –MemoryGB 8 –NumCpu 2 –Confirm:$False
  • $vm_name | Start-VM
  • }

A few things… I had to do this in a bulleted list because otherwise the spacing between it was ridiculous, and secondly, am I proud of the utter lack of elegance of this code? No. Not in the least. I think this is the ugliest thing I’ve ever seen. Does it work? Yes. It works QUITE well.  I can equally execute this as an Import-CSV to perform this against a whole series of Virtual Machines but that is outside the scope of this particular blog post. :)

If you happen to have a more elegant want to execute a VMGuest Shutdown while ALSO executing a Set-VM within the same command-set, preferably within one line, feel free to include it in the comments, otherwise this will work and can be scripted whether you’re performing against one virtual machine or more.   I personally will use this to script/execute easy modification of resources when I need to grow or shrink a virtual machines allocations without having to go through and ‘right click – wait’ over and over again, and especially so when I’m performing this against more than one virtual machine.

Good luck, I look forward to your comments, thoughts, and any improvements you may have! Hopefully this helps improve your own management!