Red Gate forums :: View topic - Restore Virtual Machine snapshots
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation

Deployment Manager PowerShell Scripts forum

Restore Virtual Machine snapshots

Search in Deployment Manager PowerShell Scripts forum
Post new topic   Reply to topic
Jump to:  
Author Message
Emma A



Joined: 30 Mar 2009
Posts: 40

PostPosted: Wed Sep 18, 2013 12:02 pm    Post subject: Restore Virtual Machine snapshots Reply with quote

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

Code:

#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
    }




Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group