February 28, 2021
Hot Topics:

Automating Software Testing with Microsoft Hyper-V

  • By Jani Järvinen
  • Send Email »
  • More Articles »

Keeping the previous five-step process in mind, you first need to launch the VMM PowerShell console (see Figure 5). This console can be found from the Start menu on the computer on which VMM has been installed. Although you could also fire up a regular PowerShell prompt, the VMM's own shortcut is convenient because it does not require you to load the VMM snap-in module manually.

Click here for a larger image.

Figure 5: Starting the PowerShell interface.

Although there is not yet much documentation available from the VMM's PowerShell commands ("cmdlets"), you can find a list of commands containing, for example, the letters "VM" by typing at the PowerShell prompt:

help *vm*

This would produce a list of commands the letters "vm" anywhere in the command name. Also, typing "help command-name" shows additional help text for the command. This is, at this point, the best way to learn about VMM's PowerShell commands.

To access a certain virtual machine using the PowerShell interface, you need to get an instance of the correct virtual machine object. Because you want to return the correct virtual machine to a correct state, you would restore it to a certain snapshot, or in System Center's parlance, a checkpoint (see Figure 6).

Click here for a larger image.

Figure 6: The Virtual Machine Manager console lets you manage your virtual machines and their checkpoints.

Here are the code lines needed to do this. Note that for simplicity, the code assumes that you have installed the VMM server and the console on the same machine.

$VM = Get-VM -VMMServer hvserver -Name "Windows XP Test"
$restore_to_checkpoint = Get-VMCheckpoint -vm $vm |
   where { $_.Name -eq "Testing Base" }
Restore-VMCheckpoint -VMCheckpoint $restore_to_checkpoint

Here, the first code line connects the Hyper-V server, called "hvserver", and looks for a virtual machine named "Windows XP Test". The resulting object is stored in the variable $VM (in PowerShell, variable names start with the letter $).

The second line uses the previously created virtual machine object $VM to find a checkpoint (snapshot) with the name "Testing Base". This is done with the Get-VMCheckpoint cmdlet, which returns a list of all checkpoints available for the given virtual machine. The "where" clause then finds the correct checkpoint based on the checkpoint's Name property.

The final step is to restore the virtual machine to the given checkpoint with the Restore-VMCheckpoint command. Assuming the virtual machine was running when the checkpoint was taken, the virtual machine will then continue to run from exactly that same point forward.

Filling In the Missing Steps

With the preceding PowerShell commands, you are able to complete the first step of the testing procedure outlined earlier. The following steps are even easier to complete, and they can be done basically with regular DOS commands.

The only obstacle is the capability to execute a process (or run a program) remotely on the virtual machine. Starting a command on a remote machine cannot be done with those old DOS-based commands, but you can use several different options to solve the problem. One is to use a WMI script; the other is to use some simple utility, such as PSExec from Technet SysInternals. The following code uses the free PSExec utility, which is very convenient to use.

Assuming the virtual machine responds in the IP address, and would have an accessible network share named "TestLocation", you could use the following PowerShell commands to copy, install, and run the application on the virtual machine:

copy "C:\Source Code\MyApplication\Setup.msi" \\\
& 'C:\Program Files (x86)\psexec.exe' \\ msiexec
   /i /passive C:\TestLocation\Setup.msi
& 'C:\Program Files (x86)\psexec.exe'
   \\ C:\TestLocation\MyApplication.exe

Page 3 of 4

This article was originally published on November 19, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date