【问题标题】:Hyper-V unable to attach existing vhd file to newly created virtual machine?Hyper-V 无法将现有的 vhd 文件附加到新创建的虚拟机?
【发布时间】:2016-08-26 05:17:14
【问题描述】:

我的目标是以编程方式创建一个新的虚拟机并将现有的 vhdx 文件附加到该机器。我的程序的唯一输入是 VM 名称和 VHDX 路径。我正在使用 Hyper-V WMI Provider 和 Virtualization V2 命名空间来实现这一点。基本上,我正在将 this 文章中给出的内容作为脚本来实现。正如this 链接中提到的,我首先添加了一个合成驱动器,然后尝试附加 vhdx 文件。

我能够成功创建虚拟机并向机器添加合成驱动器。但是,当我尝试添加硬盘 vhdx 文件时,出现以下错误。 “新虚拟机”未能添加资源。(虚拟机 ID ...)。作业错误代码为 32768,作业状态为 10,仅对应“失败”。

这是我用来连接 vhdx 文件的代码。

//Add VHD
    ManagementObject^ hardDisk = GetResourceAllocationsettingDataDefault(scope, 31, "Microsoft:Hyper-V:Virtual Hard Disk", "-");
    hardDisk = (ManagementObject^)hardDisk->Clone();
    array<String^>^ connection = gcnew array<String^>(1);
    connection[0]="E:\\test.vhdx"; //Path to *.vhd file     

    hardDisk["Parent"] = synthetic->Path->Path; //WMI path-> This is the path of the synthetic drive that I added in the previous step.
    hardDisk["HostResource"] = connection;

    array<String^> ^ HDs = gcnew array<String^>(1);
    HDs[0] = hardDisk->GetText(TextFormat::WmiDtd20);

    ManagementBaseObject^ VHDinParams = vmtoCreate->GetMethodParameters("AddResourceSettings");
    VHDinParams["AffectedConfiguration"] = settings->Path->Path;
    VHDinParams["ResourceSettings"] = HDs;

    ManagementBaseObject^ VHDoutParams = vmtoCreate->InvokeMethod("AddResourceSettings", VHDinParams, nullptr);

GetResourceAllocationsettingDataDefault 方法请参考this 链接。当我分析 VHDoutParams 变量的错误消息和错误代码时,我只得到以下详细信息。 错误描述:“新虚拟机”添加资源失败。(虚拟机 ID ...)。 错误代码:32768(失败) 工作状态:10

我不知道我错过了什么。我以管理员身份以提升模式运行程序。我没有在 Hyper-V 主机中安装防病毒软件。我也彻底搜索了 Internet,但找不到任何帮助。我错过了什么或做错了什么?

【问题讨论】:

    标签: c# c++ virtual-machine wmi hyper-v


    【解决方案1】:

    vhdx 文件仅适用于第 2 代 VM。请检查是否也可能是未识别的错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      相关资源
      最近更新 更多