vmware-cmd Examples – Working with vMA and Virtual Machines

by admin

VMware-cmd is a command line utility that can be run from a vMA, which allows you to perform various virtual machine operations.

There are a number of options available when running the command:

vi-admin@vma:~> vmware-cmd
   Usage: vmware-cmd    
          vmware-cmd -s   

       Connection Options:
          -H or --server             specifies an ESX host or a vCenter Server
          -h or --vihost      specifies a target host if host is a virtual center
          -O                         specifies an alternative port
          -Q                     specifies an alternative protocol
          -U or --username       specifies a username
          -P or --password       specifies a password
          --sessionfile                    specifies a sessionfile path
          --passthroughauth                specifies a login by sspi option
          --credstore                      specifies to fetch Credential store information
          --encoding                       specifies encoding option
       General Options:
          -h More detailed help.
          -q Quiet. Minimal output
          -v Verbose.

     Server Operations:
       vmware-cmd -l
       vmware-cmd -s register <config_file_path>  
       vmware-cmd -s unregister <config_file_path>

     VM Operations:
       vmware-cmd  getstate
       vmware-cmd  start <powerop_mode>
       vmware-cmd  stop <powerop_mode>
       vmware-cmd  reset <powerop_mode>
       vmware-cmd  suspend <powerop_mode>
       vmware-cmd  setguestinfo  
       vmware-cmd  getguestinfo 
       vmware-cmd  getproductinfo 
       vmware-cmd  connectdevice <device_name>
       vmware-cmd  disconnectdevice <device_name>
       vmware-cmd  getconfigfile
       vmware-cmd  getuptime
       vmware-cmd  answer
       vmware-cmd  gettoolslastactive
       vmware-cmd  hassnapshot
       vmware-cmd  createsnapshot    
       vmware-cmd  revertsnapshot
       vmware-cmd  removesnapshots

Synopsis: /usr/bin/vmware-cmd OPTIONS

Common VI options:
   --config (variable VI_CONFIG)
      Location of the VI Perl configuration file
   --credstore (variable VI_CREDSTORE)
      Name of the credential store file defaults to /.vmware/credstore/vicredentials.xml on Linux and /VMware/credstore/vicredent                             ials.xml on Windows
   --encoding (variable VI_ENCODING, default 'utf8')
      Encoding: utf8, cp936 (Simplified Chinese), iso-8859-1 (German), shiftjis (Japanese)
      Display usage information for the script
   --passthroughauth (variable VI_PASSTHROUGHAUTH)
      Attempt to use pass-through authentication
   --passthroughauthpackage (variable VI_PASSTHROUGHAUTHPACKAGE, default 'Negotiate')
      Pass-through authentication negotiation package
   --password (variable VI_PASSWORD)
   --portnumber (variable VI_PORTNUMBER)
      Port used to connect to server
   --protocol (variable VI_PROTOCOL, default 'https')
      Protocol used to connect to server
   --savesessionfile (variable VI_SAVESESSIONFILE)
      File to save session ID/cookie to utilize
   --server (variable VI_SERVER, default 'localhost')
      VI server to connect to. Required if url is not present
   --servicepath (variable VI_SERVICEPATH, default '/sdk/webService')
      Service path used to connect to server
   --sessionfile (variable VI_SESSIONFILE)
      File containing session ID/cookie to utilize
   --url (variable VI_URL)
      VI SDK URL to connect to. Required if server is not present
   --username (variable VI_USERNAME)
   --verbose (variable VI_VERBOSE)
      Display additional debugging information
      Display version information for the script

You can run the command, specifying connection and authentication details for a host each time, or you can make use of the vifptarget command on the vMA, so that you don’t need to enter credentials each time you run the command. The examples I use here will make use of vi-fastpass.

Listing Virtual Machines Registered on a Host

To list the VMs registered on a given host, we can use the VMware-cmd -l command:

vi-admin@vma:~> vifptarget --set esxi1.vmlab.loc
vi-admin@vma:~[esxi1.vmlab.loc]> vmware-cmd -l


Registering and Unregistering Virtual Machines

If we wish to unregister a virtual machine from a host we can run the following:

vi-admin@vma:~[esxi1.vmlab.loc]> vmware-cmd -s unregister /vmfs/volumes/5221687f-72547f8e-a71d-005056af71fa/XP-Temp/XP-Temp.vmx

unregister() = 1

If we list the VMs registered on the host again, it shows the following:

vi-admin@vma:~[esxi1.vmlab.loc]> vmware-cmd -l
No virtual machine found.

If you are doing this on a host that is connected to vCenter you will see that the VM becomes orphaned:


To register a VM we can run the following:

vi-admin@vma:~[esxi1.vmlab.loc]> vmware-cmd -s register /vmfs/volumes/5221687f-72547f8e-a71d-005056af71fa/XP-Temp/XP-Temp.vmx
register() =1
vi-admin@vma:~[esxi1.vmlab.loc]> vmware-cmd -l


Now, as seen above, when running the list command, we can see that the virtual machine is registered once again. The virtual machine will also no longer be orphaned in VirtualCenter.

Deleting a Virtual Machine

I’ve created another VM to demonstrate deleting a virtual machine using VMware-cmd. We’ll start by running the list command:

vi-admin@vma:~[esxi1.vmlab.loc]> vmware-cmd -l


We will delete the VM named ‘XP’ by running the following, first to unregister the VM:

vmware-cmd -s unregister /vmfs/volumes/5221687f-72547f8e-a71d-005056af71fa/XP/XP.vmx

Then to delete the virtual machines files from the datastore, using the vifs command:

vi-admin@vma:~[esxi1.vmlab.loc]> vifs --rm "/vmfs/volumes/5221687f-72547f8e-a71d-005056af71fa/XP"
Remove '/vmfs/volumes/5221687f-72547f8e-a71d-005056af71fa/XP'? (y/n) : y
Deleted file '[] /vmfs/volumes/5221687f-72547f8e-a71d-005056af71fa/XP' successfully.

Working with Snapshots using vmware-cmd

To determine whether a virtual machine has any snapshots, we can run the following:

vi-admin@vma:/tmp[]> vmware-cmd /vmfs/volumes/4f27b82e-3fc1540e-bf6b-000c295da2d9/XP/XP.vmx hassnapshot
hassnapshot () = 1

We can see that this VM has a snapshot. We can use vmware-cmd to delete the snapshot by running:

vi-admin@vma:/tmp[]> vmware-cmd /vmfs/volumes/4f27b82e-3fc1540e-bf6b-000c295da2d9/XP/XP.vmx removesnapshots
removesnapshot () = 1

To create a snapshot run:

vi-admin@vma:/tmp[]> vmware-cmd /vmfs/volumes/4f27b82e-3fc1540e-bf6b-000c295da2d9/XP/XP.vmx createsnapshot snap1
For creating a snapshot you must specify    .
vi-admin@vma:/tmp[]> vmware-cmd /vmfs/volumes/4f27b82e-3fc1540e-bf6b-000c295da2d9/XP/XP.vmx createsnapshot snap1 snapdesc 0 0
createsnapshot (snap1 snapdesc 0 0) = 1

Switching to the vSphere client, we can see the tasks that we have just been running, with the user being vi-admin:


Virtual Machine Power Operations using vmware-cmd

To power on a virtual machine you can run the following:

vi-admin@vma:~[esxi1.vmlab.loc]> vmware-cmd /vmfs/volumes/522258e8-aca71415-31a4-000c293cc34e/XP-Temp/XP-Temp.vmx start
start() = 1

To shutdown a virtual machine gracefully, provided VMtools is installed, run:

vi-admin@vma:~[esxi1.vmlab.loc]> vmware-cmd /vmfs/volumes/522258e8-aca71415-31a4-000c293cc34e/XP-Temp/XP-Temp.vmx stop soft
stop() = 1

You can power off, rather than shutdown by using ‘stop hard’ rather than ‘stop soft’. If you want to reboot a VM or reset it you can use ‘reset soft’ and ‘reset hard’ respectively.

To check the power state of a virtual machine using VMware-cmd you can run:

vi-admin@vma:~[esxi1.vmlab.loc]> vmware-cmd /vmfs/volumes/522258e8-aca71415-31a4-000c293cc34e/XP-Temp/XP-Temp.vmx getstate
getstate() = on

And to check the uptime:

vi-admin@vma:~[esxi1.vmlab.loc]> vmware-cmd /vmfs/volumes/522258e8-aca71415-31a4-000c293cc34e/XP-Temp/XP-Temp.vmx getuptime
getuptime() = 120

Useful Links and Resources


Keep up to date with new posts on Buildvirtual.net - Follow us on Twitter:
Be Sociable, Share!

Leave a Comment


Previous post:

Next post: