以编程方式将VHD附加到远程Hyper-V VM [英] Programmatically attaching a VHD to a remote Hyper-V VM

查看:146
本文介绍了以编程方式将VHD附加到远程Hyper-V VM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Hyper-V管理器,我可以连接到远程VM主机,转到VM的设置,然后将现有的.VHD文件添加为新硬盘.如果VM主机正在运行Server 2008 R2,并且磁盘已连接到SCSI控制器,则我甚至可以在VM运行时执行此操作(请参阅

Using Hyper-V Manager, I can connect to a remote VM host, go to the settings of a VM, and add an existing .VHD file as a new hard disk. If the VM host is running Server 2008 R2, and the disk is being attached to a SCSI controller, I can even do this while the VM is running (see What's new in Hyper-V R2).

手动执行此操作,一切正常.问题是,现在我想使其自动化,以便在进行一些自动化测试时可以即时连接不同的VHD.

Doing this manually, everything works great. The trouble is, now I want to automate it so I can attach different VHDs on-the-fly during some automated tests.

我已经有C#代码,可以通过WMI连接到远程VM主机,并通过调用 RequestStateChange ,我想对其进行扩展,使其能够说这是VHD的路径,将其作为SCSI驱动器连接到此VM".但是请查看 WMI虚拟化类列表 ,我不知道该怎么做.

I already have C# code that connects to the remote VM host over WMI and starts/stops VMs by calling RequestStateChange, and I'd like to extend it to be able to say "here's the path to a VHD, attach it as a SCSI drive to this VM". But looking at the list of WMI virtualization classes, I can't figure out how to do this.

我找到的最接近的是安装方法 Msvm_ImageManagementService ,但这似乎在当前操作系统中安装了VHD,这不是我想要的.

The closest I've found is the Mount method of Msvm_ImageManagementService, but this appears to mount a VHD inside the current OS, which isn't what I want.

推荐答案

有必要添加合成磁盘(ResourceType. Disk ,ResourceSubType. DiskSynthetic )使用Msvm_VirtualSystemManagementService.AddVirtualSystemResources.父级= SCSI控制器的WMI路径.

It is necessary to add synthetic disk (ResourceType.Disk, ResourceSubType.DiskSynthetic) using Msvm_VirtualSystemManagementService.AddVirtualSystemResources. Parent = SCSI controller's WMI path.

ManagementObject synthetic = Utilities.GetResourceAllocationSettingData(scope,
    ResourceType.Disk, ResourceSubType.DiskSynthetic);
synthetic["Parent"] = <ideControllerPath>; //or SCSI controller path (WMI path)
synthetic["Address"] = <diskDriveAddress>; //0 or 1 for IDE
string[] RASDs = new string[1];
RASDs[0] = synthetic.GetText(TextFormat.CimDtd20);

然后使用Msvm_VirtualSystemManagementService.AddVirtualSystemResources附加虚拟硬盘(ResourceType. StorageExtent ,ResourceSubType. VHD ).父=合成磁盘的WMI路径,连接 = * .vhd文件路径.

Then to attach virtual hard disk (ResourceType.StorageExtent, ResourceSubType.VHD) using Msvm_VirtualSystemManagementService.AddVirtualSystemResources. Parent = Synthetic disk's WMI path, Connection = *.vhd file path.

ManagementObject hardDisk = Utilities.GetResourceAllocationSettingData(scope,  
    ResourceType.StorageExtent, ResourceSubType.VHD);
hardDisk["Parent"] = <syntheticPath>; //WMI path
string[] connection = { <vhdPath> }; //Path to *.vhd file
hardDisk["Connection"] = connection;
string[] RASDs = new string[1];
RASDs[0] = hardDisk.GetText(TextFormat.CimDtd20);

使用虚拟化示例的通用实用程序 WMI资源管理器.

这篇关于以编程方式将VHD附加到远程Hyper-V VM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆