How to use vim-cmd with Virtual Machines

1

vim-cmd is a vSphere CLI tool available on every ESXi host and can be used to perform various activities in a VMware environment.

If you are familiar with vSphere API, you already know VIM is the original name for vSphere management (Virtual Infrastructure Management).

Where Is It Installed?

On ESXi, the vim-cmd is at /bin/vim-cmd, which is actually a symbolic link to a host executable as shown in the following.

ls -l /bin/vim-cmd

How to use vim-cmd with Virtual Machines

Now that we know what is vim-cmd and where it is installed, let us see a few examples of how it can be used with Virtual Machines.

List registered Virtual Machines

vim-cmd vmsvc/getallvms

How to use vim-cmd with Virtual Machines

As you can see, I have three VMs registeredvS on this ESXi host.

Unregister a Virtual Machine

In the below example, let us go ahead and unregister TestDSL VM from the inventory.

The syntax for unregistering the Virtual Machine is as seen below:

vim-cmd vmsvc/unregistervm vmid

Therefore, the actual command will be:

vim-cmd vmsvc/unregistervm 10

vmid  is the Virtual Machine ID as seen in the first command.

Register a Virtual Machine

I have unregistered the TestDSL VM from the inventory, and we will now see the command to register the VM using vim-cmd.

vim-cmd solo/registervm /vmfs/volumes/iSCSI-1/TestDSL/TestDSL.vmx

How to use vim-cmd with Virtual Machines

Get Virtual Machine Power State

Moving forward, we will look at how to find the Virtual Machine Power state.

vim-cmd vmsvc/power.getstate 12

where 12 is the vmid of the Virtual Machine that we are trying to query.

How to use vim-cmd with Virtual Machines

Power ON a Virtual Machine

In this example, we will power ON the TestDSL Virtual Machine.

vim-cmd vmsvc.power.on 12

How to use vim-cmd with Virtual Machines

You can notice, I ran the previous command to get the state of the VM, and it is now powered ON.

Shutdown a Virtual Machine

In the below, example we will shut down the Guest OS.

 vim-cmd vmsvc/power.shutdown 12

We received the below error message because the VMware Tools is not installed on the VM.

How to use vim-cmd with Virtual Machines

Power off a Virtual Machine

This is not a clean shutdown of the Virtual Machine since we do not have the VMware Tools installed on the above VM, we will try and power OFF the VM.

vim-cmd vmsvc/power.off 12

How to use vim-cmd with Virtual Machines

Reset a Virtual Machine

The below command can be used when you want to reset the Virtual Machine.

vim-cmd vmsvc/power.reset 12

How to use vim-cmd with Virtual Machines

Suspend a Virtual Machine

In some cases, you might want to suspend the Virtual Machine, use the below command.

vim-cmd vmsvc/power.suspend 12

How to use vim-cmd with Virtual Machines

Get Virtual Machine Uptime

The below command will list you the uptime for the Virtual Machine

vim-cmd vmsvc/get.summary 12 | grep uptimeSeconds

Get VMware Tools Status

vim-cmd vmsvc/get.summary 8 | grep -i tools

How to use vim-cmd with Virtual Machines

 

Create Virtual Machine Snapshot

 vim-cmd vmsvc/snapshot.create 12 Test_Snapshot

Test_Snapshot is the name of the snapshot.

How to use vim-cmd with Virtual Machines

 

Revert to Virtual Machine snapshot

vim-cmd vmsvc/snapshot.revert 12 1 suppressPowerOff

The syntax for reverting to a snapshot as follows:

vim-cmd vmsvc/snapshot.revert vmid snapshotId suppressPowerOff

To list the snapshot ID, you can run the below command:

vim-cmd vmsvc/snapshot.get 12

How to use vim-cmd with Virtual Machines

Remove a snapshot of a VM

vim-cmd vmsvc/snapshot.remove 12 1

1 is the snapshot Id for the Virtual Machine.

If you have multiple snapshots and you want to consolidate all of the, run the below command:

 vim-cmd vmsvc/snapshot.removeall 12

Well, that is all I have for today, I will try and add more commands to this list as and when I explore more options in the future.

I hope this has been informative and thank you for reading!

Share.

About Author

I am Adil Arif, working as a Senior Technical Support Engineer at Rubrik as well as an independent blogger and founder of Enterprise Daddy. In my current role, I am supporting infrastructure related to Windows and VMware datacenters.

1 Comment

Leave A Reply