Share via


Hyper-V: Script for Reverting Snapshots VMRevert

A tester with 2 different VM environments (but on the same VMM Admin server) needs to revert back to baseline every day. He shares the following script.

Script Text:

 

# ==============================================================================================

#    NAME: VMRevert.ps1

#  AUTHOR: Scott Hanline

#     EMAIL: v-schanl@microsoft.com

#    DATE: 6/27/2011

# COMMENT: Reverts Virtual Machine Snapshots

#         

# ==============================================================================================

 

# Load VMM PoSh module

Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager

 

# Explain to user what script will do

Write-Host " "

Write-Host -ForegroundColor Green "This script will allow you to choose which environment(s) to revert VM Snapshots on."

Write-Host " "

 

# Function for Select Environment MENU

function mainMenu() {

      $Env1 = "SERVER1"

      $Env2 = "SERVER2"

      $EnvALL = $Env1, $Env2

      Write-Host -ForegroundColor Green "============================"

      Write-Host -ForegroundColor Green "= Choose your environment: ="

      Write-Host -ForegroundColor Green "============================"

      Write-Host " "

      Write-Host -ForegroundColor Green "1: TEST1 ($Env1)"

      Write-Host -ForegroundColor Green "2: TEST2 ($Env2)"

      Write-Host -ForegroundColor Green "3: Both TEST1 & TEST2 ($Env1, $Env2)"

      [Console]::ForegroundColor = "Yellow"

      Write-Host " "

      $VMEnv = Read-Host "Enter 1, 2, 3 to initiate snapshot revert, or quit to exit: "

     

      if ($VMEnv -eq "quit"){

            [Console]::ResetColor()

            exit

      } else {

            if ($VMEnv -eq "exit"){

                  [Console]::ResetColor()

                  exit

            } else {

                  if ($VMEnv -eq 1){

                        $vmm = $Env1

                        Write-Host " "

                        Write-Host -ForegroundColor Yellow "============================================"

                        Write-Host -ForegroundColor Yellow "= Working... This will take a few minutes: ="

                        Write-Host -ForegroundColor Yellow "============================================"

                        Write-Host " "

                        RecycleVM $vmm

                  } else {

                        if ($VMEnv -eq 2){

                              $vmm = $Env2

                              Write-Host " "

                              Write-Host -ForegroundColor Yellow "============================================"

                              Write-Host -ForegroundColor Yellow "= Working... This will take a few minutes: ="

                              Write-Host -ForegroundColor Yellow "============================================"

                              Write-Host " "

                              RecycleVM $vmm

                        } else {

                              if ($VMEnv -eq 3){

                                    $vmm = $EnvALL

                                    Write-Host " "

                                    Write-Host -ForegroundColor Yellow "============================================"

                                    Write-Host -ForegroundColor Yellow "= Working... This will take a few minutes: ="

                                    Write-Host -ForegroundColor Yellow "============================================"

                                    Write-Host " "

                                    RecycleVM $vmm

                              } else {

                                    if ($VMEnv -ne 1 -or 2 -or 3){

                                          Write-Host " "

                                          Write-Host -ForegroundColor Red "================================================="

                                          Write-Host -ForegroundColor Red "= Please enter a numerical value of 1, 2, or 3! ="

                                          Write-Host -ForegroundColor Red "================================================="

                                          Write-Host " "

                                          mainMenu

                                    }

                              }

                        }

                  }

            }

      }

[Console]::ResetColor()

}

 

 

# Function for Recycling VM

#Call SCVMM PowellShell module to retrieved the last snap shot taken, and revert them.

Function RecycleVM() {

      try {

            #Connect to SCVMM server via Powershell Module

      $VMHOST = Get-VMMserver -ComputerName VMHOSTNAME -Port PortNUMBER

   

            #get the vm instance, get the last check point of this vm instance, and restore the last checkpoint.

            foreach ($svrname in $vmm){

                  Get-VM $svrname | Get-VMCheckPoint -MostRecent | Restore-VMCheckPoint

      }   

      } catch {

      Write-Error $Error[0]

            exit 1

      }

}

Helpful Resources