Restore Virtual Machine snapshots

Emma AEmma A Posts: 42
Script to stop virtual machines, restore a snap shot and restart the virtual machine

Brief notes on how to use this script:
    Add the $vms variable to the deployment Manager project and give it the name of a machine, or a comma separated list of machines Add the $vmhostname varaiable and give the name of the host of the virtual machines

Brief Explanation
It takes the list of machines to restore, stops the virtual machine, restores the latest snapshot and then restarts the machine
#Inputs:
# you will need add the variable $vms, a list of the vms to restorepowershell treats any comma separated list as an array.
# you will need to declare the Virtual machine host computer name $VMhostname

# This stops a set of virtual machines on a particular host and restires tge latest snapshot and then restarts the machines. 

$errors = ""
if ($null -eq $vms) {
   $errors = $errors + ", Variable vms"
}
if ($null -eq $VMhostname) {
   $errors = $errors + ", Variable vmhostname"
}

if (0 -ne $errors.length) {
# Throw an error and stop deployment
   Throw ($errors.trim(", ") + " must be set to restore any machines")
} 

Write-Host "vms: $vms"
Write-Host "VMhost name: $VMhostname" 

$ScriptBlock=
{
  param($vm) #Gets the value of the parameter which gets passed in by -argumentlist at the end of the script block.
        # Need to import module here because start-job spawns a new powershell process.
        Import-Module -name virtualmachinemanager -ErrorVariable scvmmrestoreerror
        if(!$?)
        {
            Write-EventLog -LogName Application -Source SCVMMAutoRestore -EventId 3000 -EntryType Information -Message "There was 

an error in Import-Module in the script :( `r`n $scvmmrestoreerror "
            continue
        }

        
        do {
            [int]$retry="0"
            $stoploop = $false
        
            try{
            Get-SCVMMServer -ComputerName $VMhostname -SetAsDefault -ErrorVariable scvmmrestoreerror
            $stoploop= $true
            } 
             catch { 
            if ($retry -gt 3){ $stoploop= $true} 
            else {
                Start-Sleep -Seconds 10 
                $retry= $retry+1
                } 
            }
        }
        while($stoploop -eq $false)
	    if(!$?)
        {
            Write-EventLog -LogName Application -Source SCVMMAutoRestore -EventId 3000 -EntryType Information -Message "There was 

an error in Get-SCVMMServer in the script :( `r`n $scvmmrestoreerror "
            continue
        } 
        do {
            [int]$retry="0"
            $stoploop = $false
        
            try{
                Stop-SCVirtualMachine -VM $vm -ErrorVariable scvmmrestoreerror
                $stoploop= $true
                } 
            catch { 
            if ($retry -gt 3){ $stoploop= $true} 
            else {
                Start-Sleep -Seconds 10 
                $retry= $retry+1
                } 
            }
        }
        while($stoploop -eq $false)
	    if(!$?)
        {
            Write-EventLog -LogName Application -Source SCVMMAutoRestore -EventId 3000 -EntryType Information -Message "There was 

an error in Stop-SCVirtualMachine in the script :( `r`n $scvmmrestoreerror "
            continue
        } 
        
        do {
            [int]$retry="0"
            $stoploop = $false
        
            try{Get-SCVMCheckpoint -VMMServer $VMhostname -VM $vm -MostRecent | Restore-SCVMCheckpoint -ErrorVariable 

scvmmrestoreerror
            $stoploop= $true
            } 
             catch { 
            if ($retry -gt 3){ $stoploop= $true} 
            else {
                Start-Sleep -Seconds 10 
                $retry= $retry+1
                } 
            }
        }
        while($stoploop -eq $false)
        if(!$?)
        {
            Write-EventLog -LogName Application -Source SCVMMAutoRestore -EventId 3000 -EntryType Information -Message "There was 

an error in Restore-SCVMCheckpoint in the script :( `r`n $scvmmrestoreerror "
            continue
        }
        
        

        do {
            [int]$retry="0"
            $stoploop = $false
        
            try{Start-SCVirtualMachine -VM $vm -ErrorVariable scvmmrestoreerror
             $stoploop= $true
            } 
             catch { 
            if ($retry -gt 3){ $stoploop= $true} 
            else {
                Start-Sleep -Seconds 10 
                $retry= $retry+1
                } 
            }
        }
        while($stoploop -eq $false)
        if(!$?)
        {
            Write-EventLog -LogName Application -Source SCVMMAutoRestore -EventId 3000 -EntryType Information -Message "There was 

an error in Start-SCVirtualMachine in the script :( `r`n $scvmmrestoreerror "
            continue
        }             
        
  
}

$jobIDs = @()

# Cycles through all VMs in the list.
# VMM cmdlets use the -ErrorVariable option to store errors in a specific var. 
# $? is tested after each command to see if it executed successfully, if not, the error is written to the event log and 

processing stops for that VM
foreach ($vm in $vms)
{
    $jobIDs += Start-Job -ScriptBlock $ScriptBlock -argumentlist $vm 
   

    
    
}  
 #Now we have to wait for all jobs to complete otherwise they will be killed when the script exits :(
while(($jobIDs |where {$_.State -eq 'Running'}).count){
   # $jobIDs | ? {$_.State -eq 'Completed' -and $_.HasMoreData} | % {Receive-Job $_}
    start-sleep -seconds 30
    }




Sign In or Register to comment.