How to Power On Virtual Machines Using the ESXi Console

Now that we are successfully consoled in, we can search our list of VMs with the command line. We will use the vim-cmd command, which utilizes the vSphere API to manage ESXi. We will also use the vmsvc subcommand to manipulate our VMs with the various options. Let’s get a list of all our VMs that are domain controllers so we can power them on. We will need the VMID in order to specify which VMs we want to run commands against, ESXi wants the ID number, not the name. So let’s get the VMID for our two DNS servers, the syntax is below. I’m searching for any VMs with the name “LukeLabDC” in them:

vim-cmd vmsvc/getallvms | grep LukeLabDC

The VMID is the number displayed on the first column on the left. So it’s 8 and 9. Now that we have the VMIDs let’s check each VM to see if they are on or off. The syntax looks like the below:

NOTE: you must replace “8” with whatever VMID your VM is assigned to:

vim-cmd vmsvc/power.getstate 8

Now that we know our VM(s) are powered off right now, let’s go ahead and power them on:

vim-cmd vmsvc/power.on 8

If we wanted to perform a graceful shutdown of the VM, then we would use power.shutdown. If we want to force the VM off, swap out the power.shutdown subcommand with power.off. Also, note that VMware tools must be installed on the VM for power.shutdown to work since it’s essentially using VMware tools to perform the graceful shutdown:

vim-cmd vmsvc/power.shutdown 8

Now, to build on this case further, what if we have an ESXi host that is completely hung? I can’t manage it through vSphere, and PowerCLI will not connect to the host either. To limit the amount of damage to the VMs, I want to shut them down before I attempt to reboot the ESXi host. A possible solution is to console into the ESXi host and get into a console session and use vim-cmd to power off each VM, similarly to what we’ve just done above.

If you have many VMs on the host, you can use a for loop to get all the VMIDs on the host and power off each one with vim-cmd. The Syntax would look like the below, just type in each line and hit enter after each one, once you type “done” the loop will run. Also, if you want to gracefully attempt to shut down each VM, use power.shutdown instead of power.off:

for i in $(vim-cmd vmsvc/getallvms |grep LukeLabDC | awk '{ print $1 }')
do
vim-cmd vmsvc/power.off $i
done

 

 

 

 

 

Метаданные статьи

Идентификатор статьи:
95
Категория:
Дата добавления:
2025-05-19 12:09:25