Virtual Machine – Create Snapshot, Apply Windows Updates, Reboot, Remove Snapshot (Basic Multi-Row Queue Sequence Example)

Recently we were asked if BatchPatch can be used to automate a process to snapshot a virtual machine, apply Windows Updates to the virtual machine, reboot it, verify the VM is back online after reboot, and then finally remove the snapshot. Something like this is actually very doable with BatchPatch. The key thing to keep in mind here is that we will execute the commands to create and remove the snapshot on the virtual machine host computer, but we will be executing the download/install/reboot process on the actual guest virtual machine. Below I will show you how we are able to link this all together into a single one-click process using the BatchPatch feature called ‘Basic Multi-Row Queue Sequence‘. Note, we also have an Advanced Multi-Row Queue Sequence that enables the administrator to accomplish everything that the basic sequence offers as well as much more complex tasks that involve multiple computers with online/offline dependencies etc.

  1. Let’s start by adding the commands for both creating and removing Hyper-V virtual machine snapshots. Select ‘Actions > Execute remote process/command > Create/modify remote commands (logged output)’
  2. We are going to use the following two PowerShell commands for creating and removing the snapshot:
    checkpoint-vm -Name VMGUEST -Snapshotname MyCheckpointTest
    get-vm VMGUEST | remove-vmsnapshot -Name 'MyCheckpointTest'

    However, to execute these PowerShell command in BatchPatch we have to modify them slightly, as follows:

    cmd.exe /c echo . | powershell.exe -ExecutionPolicy Bypass -command "checkpoint-vm -Name VMGUEST -Snapshotname MyCheckpointTest"
    cmd.exe /c echo . | powershell.exe -ExecutionPolicy Bypass -command "get-vm VMGUEST | remove-vmsnapshot -Name 'MyCheckpointTest'"

    In the ‘Remote Process’ User-Defined Commands (logged output) window we add those commands. See screenshot for example. Note, the title can be whatever you want it to be, but make sure the command is formatted as shown above. However, of course you will substitute your own VM names and snapshot names. In this example my virtual machine is called VMGUEST. The snapshot will be titled MyCheckpointTest.

  3. Now that we have created the VM snapshot creation and removal commands in BatchPatch, we can go ahead and setup the queue. Let me first outline the process clearly so that you can see exactly the steps that we are going to take to make this happen. We are going to setup a basic multi-row queue sequence, which enables us to tie together multiple hosts into a single sequence. For the sake of this example our VM host will be called VMHOST. Our VM guest will be called VMGUEST. Our snapshot will be called MyCheckpointTest.
    Step 1: Execute the command to create the VM snapshot.  This command will be executed remotely on VMHOST.
    Step 2: Execute the command to download and install updates + reboot the VM guest machine.  This command will be executed remotely on VMGUEST.
    Step 3: Wait a few minutes
    Step 4: Verify that VMGUEST is online
    Step 5: Execute the command to remove the VM snapshot.  This command will be executed remotely on VMHOST.
  4. To start, I have added our VMHOST and VMGUEST computers to the BatchPatch grid. Note that I have added the VMHOST computer two times, and the order of the hosts is:
    VMHOST
    VMGUEST
    VMHOST
    

    The reason for this is because our first command will be executed on the VMHOST, our second command (or set of commands) will be executed on the VMGUEST, and our third command will be executed on the VMHOST. When using the Basic Multi-Row Queue Sequence, the order of the hosts matters. When we actually execute the sequence, it will be executed in the order that we select/highlight the hosts in the grid. So for ease of operation we put the hosts in order so that we can easily select them in order. However, if the hosts were not in the grid in the correct order, we could still execute the Basic Multi-Row Queue Sequence in the correct/desired order by first selecting them in the desired order. However, putting them in the grid in the desired order just makes it a bit simpler. Also note, you could alternatively use the Advanced Multi-Row Queue Sequence to handle this task, and in that case the ordering would matter less.

  5. Next we need to apply a Job Queue to each row in the grid. Select the first host VMHOST and click ‘Actions > Job queue > Create/modify job queue’ In the job queue window use the ‘Saved User-Defined Commands and Deployments’ grid in the lower-left to select the command we input earlier to create the VM checkpoint.

    Then click ‘Apply queue to row(s) without executing.’ What we have done here is the first entry for VMHOST now has a job queue configured with just a single command, and this command will be responsible for creating the VM snapshot of our VMGUEST. Your BatchPatch window should look like this:

  6. Next select VMGUEST in the BatchPatch grid, and then click ‘Actions > Job queue > Create/modify job queue’ For this VMGUEST we need 3 steps as shown in the screenshot below.
    Download and install updates + reboot always
    Wait 3 minutes
    Wait for host to be detected online
    

    Once again click ‘Apply queue to row(s) without executing.’ Your BatchPatch grid should now look like this:

  7. Next select the third row in the BatchPatch grid (this is the second entry for VMHOST). Then click ‘Actions > Job queue > Create/modify job queue’ For this host our job queue will again contain just a single step to execute the command to remove the snapshot that we created earlier. Then click ‘Apply queue to row(s) without executing.’
  8. At this point the BatchPatch grid is ready for final execution of the entire process. Your grid should look like mine does in the screenshot below:
  9. We’re finally read to begin execution. Select/highlight the hosts in the grid in the order that the queue is to be executed. Since we deliberately created this grid to execute in sequential order from top to bottom, make sure to select the hosts from top to bottom. Once selected, choose ‘Actions > Job queue > Execute basic multi-row queue sequence’ When you do this what will happen is as follows:
    Step 1: Execute the command to create the VM snapshot.  This command will be executed remotely on VMHOST.
    Step 2: Execute the command to download and install updates + reboot the VM guest machine.  This command will be executed remotely on VMGUEST.
    Step 3: Wait a few minutes
    Step 4: Verify that VMGUEST is online.
    Step 5: Execute the command to remove the VM snapshot.  This command will be executed remotely on VMHOST.
This entry was posted in Blog, General, Tutorials and tagged , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.