Powerful Hacks With ESXi vim-cmd Command, Together With Shell Commands

If you have read my previous article on the vim-cmd, you may have realized how handy it is, especially when it comes to manage virtual machines. There is however a pretty challenging problem to use it – for most commands for a virtual machine, it requires vmid which is an integer that uniquely identifies the virtual machine in the context of an ESXi server. It’s like primary key in SQL database to locate a record (virtual machine instance) in a table (virtual machine type). For people who are familiar with vSphere APIs, the vmid is the same as the value of ManagedObjectReference value of a virtual machine in ESXi. Because most administrators who use commands are not necessarily familiar with vSphere API, it doesn’t help much.

Because we mostly refer a virtual machine with its name or even IP address, it’s not easy to get a virtual machine’s vmid. What we can do is to list all the virtual machines with the following command and search for a virtual machine’s name and write the vmid down:

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

~ # vim-cmd vmsvc/getallvms
Vmid     Name                           File                                   Guest OS    Version Annotation                                                                                                 
67     SteveTestVM                [datastore1] SteveTestVM/SteveTestVM.vmx    centos64Guest  vmx-08

From the list, I can search and find the vmid for my virtual machine so that I can take other actions on it, for example, powering it on/off. Since ESXi shell support other Linux commands, it’s natural to consider grep a virtual machine, just to save some eye search as follows:

~ # vim-cmd vmsvc/getallvms | grep SteveTestVM
67     SteveTestVM                [datastore1] SteveTestVM/SteveTestVM.vmx                             centos64Guest         vmx-08

With the above command, I don’t need to scroll down pages, which is a big plus, but not enough if you want to further automate it. Then, we can use cut command to get the vmid immediately: (Don’t try to get other items, say f 2 for the name. If you wan to get it, use sed to consolidate multiple spaces to one first).

~ # vim-cmd vmsvc/getallvms | grep SteveTestVM | cut -d ' ' -f 1

With the vmid in hand, we can now run almost all the command in the vmsvc namespace. Don’t try to pipeline directly as the vim-cmd command does not take standard input as its parameter. Here is where xargs command comes to help. The following commmand pass the vmid to the next vim-cmd command to suspend that virtual machine.

~ # vim-cmd vmsvc/getallvms | grep SteveTestVM | cut -d ' ' -f 1 | xargs vim-cmd vmsvc/power.suspend
Suspending VM:

Another interesting usage is to get a virtual machine’s IP address with the vim-cmd command. If you don’t have VMware Tools installed, there is no way for you to do it via the command. If you run the related command, you will get something like the following:

~ # vim-cmd vmsvc/getallvms | grep SteveTestVM | cut -d ' ' -f 1 | xargs vim-cmd vmsvc/get.guest
Guest information:
(vim.vm.GuestInfo) {
   dynamicType = <unset>,
   toolsStatus = "toolsNotRunning",
   toolsVersionStatus = "guestToolsCurrent",
   toolsVersionStatus2 = "guestToolsCurrent",
   toolsRunningStatus = "guestToolsNotRunning",
   toolsVersion = "9216",
   guestId = <unset>,
   guestFamily = <unset>,
   guestFullName = <unset>,
   hostName = <unset>,
   ipAddress = <unset>,
   screen = (vim.vm.GuestInfo.ScreenInfo) null,
   guestState = "notRunning",
   powerPolicy = (vim.vm.PowerPolicy) null,
   appHeartbeatStatus = "appStatusGray",
   guestOperationsReady = false,
   interactiveGuestOperationsReady = false,

But if you have a virtual machine with VMware Tools installed, you may have the ipAddress set. Then, you can get the IP as follows:

~ # vim-cmd vmsvc/getallvms | grep -i vCenter | cut -d ' ' -f 1 | xargs vim-cmd vmsvc/get.guest | grep ipAddress | sed -n 1p | cut -d '"' -f 2

With the above command, you get the IP address of a named virtual machine in one line. When you have the IP address of a virtual machine, you can do a lot of fun stuff, for example, ssh to it.

The point I want to make here is that vim-cmd is powerful, and even more so if combined with other shell commands in ESXi. What I just showed is limited use case. You can also list all the virtual machines on a particular datastore, get all the vmdk files used by a particular virtual machine, and so on. If you have interesting use cases and/or scripts, please feel free to share them in the comments.

Update: Got a tweet from William who mentioned his writing on vimsh which is very similar to vim-cmd. You may find many useful samples there too.

This entry was posted in Virtualization and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.


  1. bish
    Posted December 2, 2015 at 3:17 pm | Permalink

    you mean

    xargs -n1 vim-cmd vmsvc/power.suspend

    I think the other one could bail out, which may (or may not) be undesirable.

  2. Posted December 4, 2015 at 12:31 pm | Permalink

    Hi Bish, the -n1 option makes it cleaner. Thanks for the tip!


  3. Rajeev
    Posted February 19, 2016 at 5:52 pm | Permalink

    Hi Steve..

    Is there a way to get the snapshot ID based on the Snapshot name for this output

    –Snapshot Name :
    –Snapshot Id : 19
    –Snapshot Desciption :
    –Snapshot Created On : 2/9/2016 4:12:44
    –Snapshot State : powered off
    —-Snapshot Name :
    —-Snapshot Id : 20
    —-Snapshot Desciption : Testing
    —-Snapshot Created On : 2/9/2016 23:48:29
    —-Snapshot State : powered off


  4. Posted February 19, 2016 at 6:07 pm | Permalink

    I think you use grep and pipe the out to cut for all the snapshot IDs. Good luck.


  5. Fred Nielsen
    Posted August 31, 2016 at 2:27 pm | Permalink

    Best post ever, seriously. Who needs vCenter? 😉

  6. Posted August 31, 2016 at 10:57 pm | Permalink

    Thanks Fred, right on! You can also try our vSearch product as an alternative: http://www.doublecloud.net/prod_vsearch.php

  7. Posted September 19, 2016 at 4:40 pm | Permalink

    Thank you, I’ve just been looking for information about
    this subject for a while and yours is the greatest I’ve
    came upon till now. However, what in regards to the conclusion? Are you certain concerning the source?

  8. Posted October 12, 2016 at 12:52 am | Permalink

    Good article! We are linking to this particularly great post on our site.
    Keep up the good writing.

  9. Posted November 3, 2016 at 9:48 am | Permalink

    I read this article fully about the resemblance of hottest and earlier technologies,
    it’s remarkable article.

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.