Friday, December 13, 2013

How to suspend and resume VMWare Workstation VMs using PowerShell

If you have ever made a lab in your home environment, you have probably used VMWare Workstation as preferred choice for running your virtual machines. However, in such a small all-in-a-host configuration, you could face the need to shutdown and restart your main computer (i.e. if you have just added some patches or some heavyweight applications), and this could be very unpractical if you have say 5 or 6 VMs (or more!) running on top of it.

What you would like is to have the ability to suspend the state of these VMs so you can pick up where you left off after rebooting your host. And since we are in modern times, what you would like also is the ability to perform this operation with a Powershell script.

Unfortunately there is no API for Powershell in VMWare Workstation, but this program comes with another command which your Powershell script could be based on: vmrun.exe.

This utility can be used to control virtual machines and allows the following operations: start (power on), stop (power off), reset (reboot), suspend (but allow local work to resume), pause (without interrupting), and unpause (continue).

I had the idea to write two scripts. The first one is good for suspending VMs and should be using prior to rebooting your host (right click, open with Windows Powershell ISE):
cd "C:\Program Files (x86)\VMware\VMware Workstation"

$RunningVMs = .\vmrun list | select-object -skip 1
 
Foreach ($RunningVM in $RunningVMs)
{
    "Suspending $RunningVM..."
    .\vmrun suspend "$RunningVM"
}
The second one is good for resuming your VMs to the exact state they were before the host rebooted:
cd "C:\Program Files (x86)\VMware\VMware Workstation"

$SuspendedVMs = gci -Include *.vmx -Recurse -Path C:\VMs
 
Foreach ($SuspendedVM in $SuspendedVMs)
{
.\vmrun start "$SuspendedVM"
}
As you can understand the state of a VM is kept persistent across reboot since it's dumped to a .vmss file (whose name will look like this: vm001-82cc0131.vmss) and the RAM content is dumped to a .vmem file (such as vm001-82cc0131.vmem).

I hope this helps with your labs, and if you have the occasion, check out the new Hyper-V 2012 R2 role as a good alternative to VMWare Workstation.

4 comments:

  1. I'm loving this article. Thank a ton for putting this on Web.

    ReplyDelete
    Replies
    1. Glad you found this piece of information useful!
      Carlo

      Delete
  2. Hi,

    cool script, but it isn't working with shared Machines, becaus /list finds none of them. Any Idea?

    Thanks
    Ali

    ReplyDelete
    Replies
    1. You have to use "vmrun -T ws-shared list" *but* that command is broken in WS12. Okay in WS11. Also, VIX (which is what vmrun uses) uses might be end of line?

      Delete

Related Posts Plugin for WordPress, Blogger...