【问题标题】:Object not resolving while using WMI for HyperV将 WMI 用于 HyperV 时对象未解析
【发布时间】:2023-03-29 08:59:01
【问题描述】:

我正在使用 Microsoft 文档中的官方示例来使用 WMI 启动和关闭虚拟机,但 UtilityReturnCode 对象没有得到解决。当我构建应用程序时,我得到了

CS0103  The name 'Utility' does not exist in the current context    

我一无所知

ManagementObject vm = Utility.GetTargetComputer(vmName, scope);

&

if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)

https://docs.microsoft.com/en-us/windows/win32/hyperv_v2/requeststatechange-msvm-computersystem 在 Hyper-V 正常运行的情况下在 Server 2019 上运行所有内容。

完整代码如下:

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]);
        }

    }
}

【问题讨论】:

    标签: c# wmi hyper-v


    【解决方案1】:

    您现在必须使用查询:

    ManagementScope scope = new ManagementScope(@"root\virtualization\v2");
    ObjectQuery query = new ObjectQuery(@"SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '" + vmName + "'");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    ManagementObjectCollection collection = searcher.Get();
    
    ManagementObject vm = null;
    foreach (ManagementObject obj in collection)
    {
        vm = obj;
        break;
    }
    

    对于 ReturnCode,请改用您自己的常量。你可以在这里查看它们:https://docs.microsoft.com/en-us/previous-versions/windows/desktop/virtual/requeststatechange-msvm-computersystem

    类似这样的:

    if ((UInt32)outParams["ReturnValue"] == 0)
        {
            ...
        }
    

    0 == 一切都好。使用 UInt32 而不是 UInt16!

    记得以管理员身份运行您的应用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多