使用WMI在C#中启动Hyper_V客户端

问题描述 投票:0回答:1

我是WMI内部编写Hyper-V的新手,并且总是欢迎在此领域学习机会。

我需要创建一个winform应用程序,列出计算机中可用的所有VM。当用户单击一个VM时,它将启动Hyper-V客户端窗口。

我的下面的代码几乎可以启动或停止任何特定的VM。但是,它不会启动hyper-v客户端窗口。

这是我的原型代码(现在在命令行中):

    using System;
using System.Management;

namespace HyperVSamples
{
    public class RequestStateChangeClass
    {
        public static void RequestStateChange(string vmName, string action)
        {
            ManagementScope scope = new ManagementScope(@"\\.\root\virtualization\v2", null);
            ManagementObject vm = Utility.GetTargetComputer(vmName, scope);

            if (null == vm)
            {
                throw new ArgumentException(
                    string.Format(
                    "The virtual machine '{0}' could not be found.", 
                    vmName));
            }

            ManagementBaseObject inParams = vm.GetMethodParameters("RequestStateChange");

            const int Enabled = 2;
            const int Disabled = 3;

            if (action.ToLower() == "start")
            {
                inParams["RequestedState"] = Enabled;
            }
            else if (action.ToLower() == "stop")
            {
                inParams["RequestedState"] = Disabled;
            }
            else
            {
                throw new Exception("Wrong action is specified");
            }

            ManagementBaseObject outParams = vm.InvokeMethod(
                "RequestStateChange", 
                inParams, 
                null);

            if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
            {
                if (Utility.JobCompleted(outParams, scope))
                {
                    Console.WriteLine(
                        "{0} state was changed successfully.", 
                        vmName);
                }
                else
                {
                    Console.WriteLine("Failed to change virtual system state");
                }
            }
            else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
            {
                Console.WriteLine(
                    "{0} state was changed successfully.", 
                    vmName);
            }
            else
            {
                Console.WriteLine(
                    "Change virtual system state failed with error {0}", 
                    outParams["ReturnValue"]);
            }

        }

        public static void Main(string[] args)
        {
            if (args != null && args.Length != 2)
            {
                Console.WriteLine("Usage: <application> vmName action");
                Console.WriteLine("action: start|stop");
                return;
            }

            RequestStateChange(args[0], args[1]);
        }

    }
}

鉴于:

计算机安装了Hyper-V管理器,其中包含多个预先填充的VM。

题:

如何从winform启动hyper-v客户端窗口?谢谢

c# wmi hyper-v
1个回答
0
投票

在进行了一些研究后,看起来启动hyper-v客户端非常简单....下面是完整的功能,以防万一有人在将来寻找它...

        public static string ConnectVM(string VMName)
        {
        var error = string.Empty;
        var runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();

        //create a pipeline
        var path = ConfigurationManager.AppSettings["VMConnectPath"];
        var pipeline = runspace.CreatePipeline();


        pipeline.Commands.AddScript($"& \"{path}\" localhost '{VMName}'");

        try
        {
            pipeline.Invoke();
        }
        catch (Exception e)
        {
            error = e.Message;
        }

        runspace.Close();
        return error;
    }
© www.soinside.com 2019 - 2024. All rights reserved.