Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式将VHD附加到远程Hyper-V虚拟机_C#_Hyper V_Windows Server 2008 R2_Vhd - Fatal编程技术网

C# 以编程方式将VHD附加到远程Hyper-V虚拟机

C# 以编程方式将VHD附加到远程Hyper-V虚拟机,c#,hyper-v,windows-server-2008-r2,vhd,C#,Hyper V,Windows Server 2008 R2,Vhd,使用Hyper-V管理器,我可以连接到远程VM主机,转到VM的设置,并将现有的.VHD文件添加为新硬盘。如果虚拟机主机运行Server 2008 R2,并且磁盘连接到SCSI控制器,我甚至可以在虚拟机运行时执行此操作(请参阅) 手工操作,一切都很好。问题是,现在我想自动化它,这样我就可以在一些自动化测试中动态地附加不同的VHD 我已经有了通过WMI连接到远程VM主机并通过调用启动/停止VM的C#代码,我想扩展它,以便能够说“这是VHD的路径,将其作为SCSI驱动器连接到此VM”。但是看看这个,我

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

手工操作,一切都很好。问题是,现在我想自动化它,这样我就可以在一些自动化测试中动态地附加不同的VHD

我已经有了通过WMI连接到远程VM主机并通过调用启动/停止VM的C#代码,我想扩展它,以便能够说“这是VHD的路径,将其作为SCSI驱动器连接到此VM”。但是看看这个,我不知道怎么做

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

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

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);
managementobjectsynthetic=Utilities.GetResourceAllocationSettingData(范围,
ResourceType.Disk,ResourceSubType.DiskSynthetic);
合成[“父”]=//或SCSI控制器路径(WMI路径)
合成[“地址”]=//IDE的0或1
字符串[]RASDs=新字符串[1];
RASDs[0]=synthetic.GetText(TextFormat.cimdd20);
然后使用Msvm_VirtualSystemManagementService.AddVirtualSystemResources连接虚拟硬盘(ResourceType.StorageExtent,ResourceSubType.VHD)。父级=合成磁盘的WMI路径,连接=*.vhd文件路径

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);
ManagementObject hardDisk=Utilities.GetResourceAllocationSettingData(范围,
ResourceType.StorageExtent、ResourceSubType.VHD);
硬盘[“父”]=//WMI路径
字符串[]连接={}//*.vhd文件的路径
硬盘[“连接”]=连接;
字符串[]RASDs=新字符串[1];
RASDs[0]=hardDisk.GetText(TextFormat.cimdd20);
使用和。

还可以查看一个示例