How To Power a VM On or Off via SSH

Scotty-SSH-Power

Last week while in a pinch and needing to reboot a VM while I was out of the office, I quickly learned a few steps on how to do this via SSH.  Luckily for me I already have SSH enabled on my VMware ESXi hosts in the office. If not, here is a link on how to enable that feature.  I currently have an issue with my vCenter database because it’s running Microsoft SQL Server Express and not the full version of Microsoft SQL Server just yet.  I have outgrown the size requirements for using Microsoft SQL Server Express and currently in the process of building full version SQL to handle my needs.  Although not ideal my current quick-fix is a simple reboot of the vCenter Windows server. Hey, I got to do what I got to do sometimes just to get by, I know you have done the same!

Technology Scramble

Since I was out of the office taking my child to a doctor visit, I did not have a laptop with me so that was my first issue to overcome. I have an iPhone I thought so why is it not possible to make this happen from this device although small?  Well that was my only choice at that moment and my only way into my network remotely.  I got my daughter checked in for her appointment and quickly hid in the corner of the doctors office to begin my work.

Apps on Apps, on Apps

I used the iPhone’s VPN future to connect to my office VPN and get on that network.  Next I dug through my phone looking for an SSH client that I thought I had installed but was not there. It was actually installed on my Nexus 7 which was also not with me. HA!  So I jumped into the iTunes store and did a quick search for an SSH client. At this point I honestly didn’t care what I found is long as it was free and easy to navigate.  The first app that came up in my search was Server Auditor, it was Free, so I was on my way to install and use it.

Server Auditor-Combo

VM Process Kill

Next I needed to refresh my memory on how to use some of the advanced features of esxcli.  I found this VMware KB that outlined Powering off a VM over SSH (yes, I do know I shouldn’t have SSH enabled, but recently I have to force more command line learning) using esxcli commands.  So first you start up by finding a list of your VMs. This command below shows the World ID, UUID, Display Name, and path to the .vmx configuration file of each VM. (Note these commands shown are for ESXi 5.x. The VMware KB also goes over commands for ESXi 4.1 if needed).

esxcli vm process list

Find your required VM that needs to be shut down or rebooted and note it’s World ID number as you will need that in the next CLI command. Powering off the VM is done via this next command.

esxcli vm process kill -t [soft,hard,force] -w WorldNumber

SSH-VM-kill-process

**NOTES** Per VMware: Soft is the most graceful, hard performs an immediate shutdown, and force should be used as a last resort.

Next you should re-run this command to verify the VM is off.

esxcli vm process list

Powering Up the VM

Next I moved onto using the vim-cmd commands to power the VM back on. Here is a nice VMware KB that also outlines this process for you. Begin with this command to get a listing of all VMs. You can also use grep at the end of the command to narrow down your search. The first column of the output shows the vmid.

vim-cmd vmsvc/getallvms or vim-cmd vmsvc/getallvms |grep <vm name>

 Double check the power state of the VM:

vim-cmd vmsvc/power.getstate <vmid>

SSH-Power-On state

Power on the VM:

vim-cmd vmsvc/power.on <vmid>

SSH-GetPower-State

Thoughts

I definitely think that I need to brush up on my command line skills without having a serious issue force feed those commands to me. I do have to say I was feeling pretty nerdy after being able to reboot a VM over VPN from my iPhone!  I’m also thankful for the Server Auditor app being so easy to use without much issue.  This is definitely not the cleanest way to get VMs to reboot and restart as we all know, but in the situation that I had become stuck in, it sure was helpful. Feel free to give me your thoughts on this process and let me know if you have been in the same situation before. Thanks & Happy Rebooting!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.