Question

I am looking to replace a Virtual Box solution with MS Hyper-V since I have had many problems with non-Reproducible issues in my automated test-suite using Virtual Box. I have a Windows 8.1 computer I will be using to run the tests on.

The current Virtual Box flow:

  1. Start a VM
  2. Reset snapshot
  3. Use C# to transfer files to Guest OS through the network
  4. Use Virtual Box to trigger the transferred .exe file to start automated tests.

I see people using Powershell Scripts and WMI to start and stop their Hyper-V VMs, but I don't see any way to trigger the transferred files on the Guest OS.

Am I missing an API that I can use? Otherwise how could I trigger the EXE on the guest OS programmatically?

Was it helpful?

Solution

I ended up using System.Management.Automation.PowerShell. I will share the main code chunk I used to do each step so future users can get help.

The Main Code Chunk

    var ps = PowerShell.Create();
    //Restore Snapshots
    ps.AddCommand("Restore-VMSnapshot");
    ps.AddParameter("Name", snapshot);
    ps.AddParameter("VMName", vmName);
    ps.AddParameter("Confirm", false);
    ps.Invoke();
    ps.Commands.Clear();

    //Start VM
    ps.AddCommand("Start-VM");
    ps.AddParameter("Name", vmName);
    ps.Invoke();
    ps.Commands.Clear();

    //Get IP
    string[] ipValues = null;
    do
    {
        ps.AddCommand("Get-VMNetworkAdapter");
        ps.AddParameter("VMName", vmName);
        var ips = ps.Invoke();
        ps.Commands.Clear();

        if (ips.Count > 0)
        {
            ipValues = (string[])ips[0].Members["IPAddresses"].Value;
        }
    } while (ipValues.Length ==0);
    string ip = ipValues[0];

    //Move Exe to VM
    File.Copy(@"...", "\\\\" + ip + "\\Users\\Public\\Documents\\...", true);

    //Run Program
    ps.AddScript("$Username = '...'; $Password = '...' ;$ComputerName = '"+ip+"' ;"+
    "$Script = {Start-Process C:\\Users\\Public\\Documents\\....exe} ;$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force ;"+
    "$mycreds = New-Object System.Management.Automation.PSCredential ($Username, $secpasswd) ;"+
    " $Session = New-PSSession -ComputerName $ComputerName -credential $mycreds ; Invoke-Command -Session $Session -Scriptblock $Script");
    var passwords = ps.Invoke();
    ps.Commands.Clear();

Notes

The //GetIP section is a do{}while() cause the IP takes a while to be query-able.

There is alot of pre-work required with the host computer and VMs to make this system function, which I will not get into here as google explains those parts better than me.

The flow is designed to match another system which uses Virtual Box, so it may seems a bit inefficient. This obviously needs to be modified to fit each situation, but should be a good starting point for Hyper-V Automation.

OTHER TIPS

A very usefull PowerShell CmdLet to transfert files to VM is Copy-VMFile.

Syntax is explained here :

http://technet.microsoft.com/en-us/library/dn464282.aspx

Hope this helps !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top