Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 12-May-17 12:47pm

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900