ApplySnapshot示例Hyper-V V2
问题描述:
我想将快照应用于使用C#的超V虚拟机并使用ApplySnapshot方法。ApplySnapshot示例Hyper-V V2
但我似乎因为没有挣扎样品类为该方法。如果有人能够帮助提供样本或样本项目,我会很乐意。
非常感谢
比利
答
这个例子将应用最新的快照虚拟机。通过将'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);
}
}
}
}
}
}
}
}
如果您有任何问题或遇到任何问题,请让我知道。
对不起,我忘了提及它的Hyper-V(V2).. – BillyDay