【问题标题】:Power up Hyper_V client in C# using WMI使用 WMI 在 C# 中启动 Hyper_V 客户端
【发布时间】:2019-03-12 21:35:44
【问题描述】:

我是在 WMI 中编写 Hyper-V 的新手,并且随时欢迎在这方面学习。

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

我下面的代码几乎可以启动或停止任何特定的虚拟机。但是,它不会启动 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】:

    经过一些研究,启动 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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      • 2015-06-24
      相关资源
      最近更新 更多