C#ApplySnapshot示例Hyper-V V2 [英] C# ApplySnapshot Example Hyper-V V2

查看:130
本文介绍了C#ApplySnapshot示例Hyper-V V2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#并使用ApplySnapshot方法将快照应用于超V(v2)虚拟机。



https://msdn.microsoft.com/en-us/library/hh850022%28v=vs.85%29.aspx



但我似乎因为那个方法没有样本而挣扎。如果有人可以提供样品或示例项目,我将不胜感激。



非常感谢

解决方案

< blockquote>嗨MJ999,



这个例子是在VB.NET中,但它很容易被翻译成C#。在此示例中,我将应用虚拟机的最新快照。要应用特定快照,请将lastSnapshot替换为要应用的快照的Msvm_VirtualSystemSettingData实例。



希望这会让你顺利,但如果你还有任何问题,请告诉我。



 Imports System.Management 

公共类VirtualSystemSnapshot
公共共享功能应用(vm作为ManagementObject, snapName As String)
Dim Options As New ConnectionOptions
Options.Username = strUserName
Options.Password = strPassword

'现在我们可以应用快照
昏暗的范围作为新的ManagementScope(\\& ServerName&\ Root \ Virtualization\V2,选项)
使用virtualMachine作为ManagementObject = WmiUtilities.GetVirtualMachine(vmElementName,scope)
使用virtualSystemSettingData作为ManagementObject = WmiUtilities.GetVirtualSystemSettingData(scope,virtualMachine)
使用virtualSystemSnapshotService作为ManagementObject = WmiUtilities.GetVirtualSystemSnapshotService(范围)
使用lastSnapshot作为ManagementObject = WmiUtilities.GetFirstObjectFromCollection(
virtualSystemSettingData.GetRelated(Msvm_VirtualSystemSettingData,Msvm_ParentChildSettingData,Nothing,Nothing,Nothing,Nothing,False,Nothing))
使用inParams作为ManagementBaseObject = virtualSystemSnapshotService.GetMethodParameters(ApplySnapshot)
inParams(Snapshot )= lastSnapshot

'为了应用快照,必须首先保存虚拟机
RequestStateChange.Main(vm,RequestedState.Save)

使用outParams As ManagementBaseObject = virtualSystemSnapshotService.InvokeMethod(ApplySnapshot,inParams,Nothing)
WmiUtilities.ValidateOutput(outParams,scope)

'现在已经应用了快照,启动VM备份
RequestStateChange.Main(vm,Request edState.Start)
结束使用
结束使用
结束使用
结束使用
结束使用
结束使用
结束函数
结束等级


I am trying to apply a snapshot to a hyper V (v2)virtual machine using C# and using the ApplySnapshot Method.

https://msdn.microsoft.com/en-us/library/hh850022%28v=vs.85%29.aspx

But I seem to be struggling as there no sample for that method. I would be grateful if someone could help in providing a sample or a sample project.

Many thanks

解决方案

Hi MJ999,

This example is in VB.NET, but it can easily be translated into C#. In this example, I am applying the most recent snapshot of the virtual machine. To apply a specific snapshot, replace the 'lastSnapshot' with the Msvm_VirtualSystemSettingData instance of the snapshot you wish to apply.

Hopefully this will get you going, but please let me know if you still have any issues.

Imports System.Management

Public Class VirtualSystemSnapshot
    Public Shared Function Apply(vm As ManagementObject, snapName As String)
        Dim Options As New ConnectionOptions
        Options.Username = strUserName
        Options.Password = strPassword

        ' Now we can apply the snapshot
        Dim scope As New ManagementScope("\\" & ServerName & "\Root\Virtualization\V2", Options)
        Using virtualMachine As ManagementObject = WmiUtilities.GetVirtualMachine(vmElementName, scope)
            Using virtualSystemSettingData As ManagementObject = WmiUtilities.GetVirtualSystemSettingData(scope, virtualMachine)
                Using virtualSystemSnapshotService As ManagementObject = WmiUtilities.GetVirtualSystemSnapshotService(scope)
                    Using lastSnapshot As ManagementObject = WmiUtilities.GetFirstObjectFromCollection(
                            virtualSystemSettingData.GetRelated("Msvm_VirtualSystemSettingData", "Msvm_ParentChildSettingData", Nothing, Nothing, Nothing, Nothing, False, Nothing))
                        Using inParams As ManagementBaseObject = virtualSystemSnapshotService.GetMethodParameters("ApplySnapshot")
                            inParams("Snapshot") = lastSnapshot

                            ' In order to apply a snapshot, the virtual machine must first be saved
                            RequestStateChange.Main(vm, RequestedState.Save)

                            Using outParams As ManagementBaseObject = virtualSystemSnapshotService.InvokeMethod("ApplySnapshot", inParams, Nothing)
                                WmiUtilities.ValidateOutput(outParams, scope)

                                ' Now that the snapshot has been applied, start the VM back up
                                RequestStateChange.Main(vm, RequestedState.Start)
                            End Using
                        End Using
                    End Using
                End Using
            End Using
        End Using
    End Function
End Class


这篇关于C#ApplySnapshot示例Hyper-V V2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆