[英]ApplySnapshot Example Hyper-V V2
我正在嘗試使用C#和ApplySnapshot方法將快照應用於Hyper V虛擬機。
但是我似乎很掙扎,因為該方法沒有示例類。 如果有人可以幫助提供示例或示例項目,我將不勝感激。
非常感謝
比利
本示例將為VM應用最新快照。 通過使用代表您要應用的快照的Msvm_VirtualSystemSettingData實例替換“ lastSnapshot”,可以輕松地將其更改為應用選定的快照。
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);
}
}
}
}
}
}
}
}
如果您有任何疑問或遇到任何問題,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.