简体   繁体   English

来宾上的Windows 8 Hyper-v运行脚本

[英]Windows 8 Hyper-v run script on guest

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. 我正在寻求用MS Hyper-V替换Virtual Box解决方案,因为在使用Virtual Box的自动测试套件中,我遇到了很多不可重现的问题。 I have a Windows 8.1 computer I will be using to run the tests on. 我有一台Windows 8.1计算机,将用于在其上运行测试。

The current Virtual Box flow: 当前的虚拟盒子流程:

  1. Start a VM 启动虚拟机
  2. Reset snapshot 重设快照
  3. Use C# to transfer files to Guest OS through the network 使用C#通过网络将文件传输到Guest OS
  4. Use Virtual Box to trigger the transferred .exe file to start automated tests. 使用Virtual Box触发传输的.exe文件以开始自动测试。

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. 我看到有人使用Powershell脚本和WMI启动和停止他们的Hyper-V VM,但是我看不到任何在Guest OS上触发已传输文件的方法。

Am I missing an API that I can use? 我是否缺少可以使用的API? Otherwise how could I trigger the EXE on the guest OS programmatically? 否则,我如何以编程方式触发来宾OS上的EXE?

I ended up using System.Management.Automation.PowerShell . 我最终使用了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. //GetIP部分是一个do{}while()导致IP需要一段时间才能查询。

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. 要使该系统正常运行,主机和VM需要进行大量的准备工作,由于google比我更好地介绍了这些部分,因此我在这里不做介绍。

The flow is designed to match another system which uses Virtual Box, so it may seems a bit inefficient. 该流程旨在与使用Virtual Box的另一个系统匹配,因此似乎效率低下。 This obviously needs to be modified to fit each situation, but should be a good starting point for Hyper-V Automation. 显然,需要对此进行修改以适合每种情况,但是对于Hyper-V Automation,它应该是一个很好的起点。

A very usefull PowerShell CmdLet to transfert files to VM is Copy-VMFile. 将文件传输到VM的一个非常有用的PowerShell CmdLet是Copy-VMFile。

Syntax is explained here : 语法在这里说明:

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

Hope this helps ! 希望这可以帮助 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM