How-To
Automate VMware VMs Using vmrun
In a recent article I discussed VMware Workstation's newest command line tool, vmcli, released with WorkStation 17.6, and described it as a command-line tool that can create and manage virtual machines (VMs). Unfortunately, I didn't have much luck with this tool, so I gave up after working with it for a few hours. As it is still in its infancy, I plan on working with it as it matures. After working with vmcli got me thinking of Workstation's OG command line tool-vmrun. In this article, I will discuss vmrun and how to use it.
What Is vmrun"
Vmrun has been a built-in command shipped with the VMware workstation since at least version 15 when I first used it. Although Workstation has a GUI interface that is easy to use and very powerful, sometimes it is more useful to perform an action using the command line or a script than using a GUI. It should be noted that this command is associated with the VMware API libraries.
Like vmcli, vmrun can be used to create and manage VMs. However, it has been around long enough to be relatively bug-free and has been tested over time by the VMware community.
Using vmrun to Start Machines
Vmrun is located in C:\Program Files (x86)\VMware\VMware Workstation. The basic syntax for vmrun is vmrun [AUTHENTICATION-FLAGS] COMMAND [PARAMETERS].
If you change to the directory where it is located and enter vmrun, it will display its version, usage, and commands and parameters that you can use with it.
I couldn't find a command to list all the VMs on my system but did find that I could start a VM by entering vmrun -T ws start "C:\Users\user01\Documents\Virtual Machines\Win11_C\Win11_C.vmx", which started it.
The -T switch said it was a WorkStation VM (vmrun also supports Fusion and Player). Start powers on the VM; the last part is the path to the vmx file.
I then listed the snapshots using the vmrun -T ws ListSnapshots, which showed no snapshots were associated with the VM, which was correct.
The Power of vmrun
I have found that the real power of vmrun, which I tend to use it for, is the management of the applications and files on the VMs running on the Workstation.
To list the running process on a VM, enter vmrun -T ws listProcessesInGuest <location of vmx file>
To launch notepad within a VM, I entered:
vmrun -T ws runProgramInGuest <location of vmx file> -activeWindow "c:\windows\system32\notepad.exe"
Looking at the VM in the Workstation GUI I could see that it started.
You can stop (kill) an application in a guest by getting its PID using the killProcessInGuest operation.
You can also work with the files and directories on the VMs. This includes transferring files to them.
VMware has several examples of using the vmrun command. These can be found here.
Final Thoughts
One of the thoughts that crossed my mind while looking at the vmcli command is that it may replace the vmrun command. However, after looking at their operations, I think that vmcli is currently far too different in scope to be its replacement. That said, vmcli has yet to be at version 1.0, so more functionalities could easily be in its future.
So, I will continue using vmrun for my daily activities and keeping an eye on vmcli to see how it develops.
About the Author
Tom Fenton has a wealth of hands-on IT experience gained over the past 30 years in a variety of technologies, with the past 20 years focusing on virtualization and storage. He previously worked as a Technical Marketing Manager for ControlUp. He also previously worked at VMware in Staff and Senior level positions. He has also worked as a Senior Validation Engineer with The Taneja Group, where he headed the Validation Service Lab and was instrumental in starting up its vSphere Virtual Volumes practice. He's on X @vDoppler.