Applying a snapshot–via PowerShell [Hyper-V]

Applying a snapshot–via PowerShell [Hyper-V]

Rate This
  • Comments 2

Now that we know how to take a snapshot, and how to list snapshots – let’s start applying snapshots:

# Function for handling WMI jobs / return values
Function ProcessResult($result, $successString, $failureString)
   #Return success if the return value is "0"
   if ($result.ReturnValue -eq 0)
      {write-host $successString} 
   #If the return value is not "0" or "4096" then the operation failed
   ElseIf ($result.ReturnValue -ne 4096)
      {write-host $failureString " Error value:" $result.ReturnValue}
      {#Get the job object
      #Provide updates if the jobstate is "3" (starting) or "4" (running)
      while ($job.JobState -eq 3 -or $job.JobState -eq 4)
         {write-host $job.PercentComplete "% complete"
          start-sleep 1
          #Refresh the job object
       #A jobstate of "7" means success
       if ($job.JobState -eq 7)
          {write-host $successString
          return $true}
          {write-host $failureString
          write-host "ErrorCode:" $job.ErrorCode
          write-host "ErrorDescription" $job.ErrorDescription
          return $false}
# Prompt for the Hyper-V Server to use
$HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
# Prompt for the virtual machine to use
$VMName = Read-Host "Specify the name of the virtual machine"
# Prompt for the name of the snapshot to apply
$SnapshotName = Read-Host "Specify the name of the snapshot to apply"
# Get the management service
$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer
# Get the virtual machine object
$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $HyperVServer
# Find the snapshot that we want to apply
$Snapshot = gwmi -Namespace root\virtualization -Query "Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData" | where {$_.ElementName -eq $SnapshotName} | select -first 1
# Apply the snapshot
$result = $VMMS.ApplyVirtualSystemSnapshot($VM, $Snapshot)
# Check to make sure we succeeded
$applySucceeded = ProcessResult $result "Snapshot applied." "Failed to apply snapshot."

This is a fairly standard Hyper-V WMI script.  One thing to be aware of is that this operation will fail if the virtual machine is running when you try to apply the snapshot – the virtual machine needs to be turned off or put into a saved state first.


Leave a Comment
  • Please add 6 and 8 and type the answer here:
  • Post
  • Hi Ben,

    Thnks for the code,

    But when i run the code in my machine(Windows Server 2008)

    it gives following exceptions

    Get-WmiObject : Invalid parameter

    At C:\Users\v-diweer\Desktop\hello\ApplySnapshot.ps1:57 char:17

    + $Snapshot = gwmi <<<< -Namespace winmgmts:\\root\virtualization -Query "Associators Of {$VM} Where AssocClass=Msvm_E

    lementSettingData ResultClass=Msvm_VirtualSystemSettingData" | where {$_.ElementName -eq $SnapshotName} | select -first


    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException

    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    Exception calling "ApplyVirtualSystemSnapshot" : "Invalid method Parameter(s) "

    At C:\Users\dilz\Desktop\hello\ApplySnapshot.ps1:59 char:43

    + $result = $VMMS.ApplyVirtualSystemSnapshot <<<< ($VM, $Snapshot)

    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : WMIMethodException

    it will be great help if you can assit me on this


  • hi Ben

    I solved my issue

    Thnk you very much!

Page 1 of 1 (2 items)