使用实例Hyper-V V2
本文关键字:V2 Hyper-V 实例 | 更新日期: 2023-09-27 18:12:02
我正在尝试使用c#和ApplySnapshot方法将快照应用到hyper V虚拟机。
ApplySnapshot方法但我似乎在挣扎,因为没有该方法的样例类。如果有人能帮助提供一个样本或样本项目,我将不胜感激。
多谢比利
本例将应用虚拟机最近的快照。这可以很容易地更改为应用选定的快照,通过将'lastSnapshot'替换为代表您想要应用的快照的Msvm_VirtualSystemSettingData实例。
public static class VirtualSystemSnapshot
{
public static object Revert(VirtualMachine vm)
{
ManagementScope scope = new ManagementScope("''''" + ServerName + "''Root''Virtualization''V2", Options);
using (ManagementObject virtualMachine = WmiUtilities.GetVirtualMachine(vmElementName, scope)) {
using (ManagementObject virtualSystemSettingData = WmiUtilities.GetVirtualSystemSettingData(scope, virtualMachine)) {
using (ManagementObject virtualSystemSnapshotService = WmiUtilities.GetVirtualSystemSnapshotService(scope)) {
using (ManagementObject lastSnapshot = WmiUtilities.GetFirstObjectFromCollection(virtualSystemSettingData.GetRelated("Msvm_VirtualSystemSettingData", "Msvm_ParentChildSettingData", null, null, null, null, false, null))) {
using (ManagementBaseObject inParams = virtualSystemSnapshotService.GetMethodParameters("ApplySnapshot")) {
inParams("Snapshot") = lastSnapshot;
// In order to apply a snapshot, the virtual machine must first be saved
RequestStateChange.Main(vm, RequestStateChange.RequestedState.Save, false);
using (ManagementBaseObject outParams = virtualSystemSnapshotService.InvokeMethod("ApplySnapshot", inParams, null)) {
WmiUtilities.ValidateOutput(outParams, scope);
// Now that the snapshot has been applied, start the VM back up
RequestStateChange.Main(vm, RequestStateChange.RequestedState.Start, false);
}
}
}
}
}
}
}
}
如果你有任何问题或遇到任何问题,请告诉我。