Programmatically attaching a VHD to a remote Hyper-V VM

大憨熊 提交于 2019-12-01 06:26:49

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

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

Use Common Utilities for the Virtualization Samples and WMI Explorer.

Also take a look at http://hypervlib.codeplex.com for an example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!