PowerShell script exchange
Post Reply
mikedimunno
Novice
Posts: 7
Liked: never
Joined: Aug 12, 2015 3:21 pm
Full Name: Michael DiMunno
Contact:

Veeam Powershell Scripting Iterations

Post by mikedimunno »

Hi,
For some time we have been developing a Veeam powershell script that will perform an automatic test restore to test reliability of backups in our customer's environments. At this moment in time, we have a script which will use a seed file and dependent upon what is specified in that seed file, will go through each job name specified in the seed file and restore a specific file also specified within the seed file. It also emails a report following completion of the test restore.

We would like to modify this script so that it uses the name of backed up servers in the seed file instead of job names. So, we want the script to look at the seed file, determine the name of the server that we wish to test restore for based upon what is entered in that file, find the server in the list of jobs, and perform the specified test restore.

Can anyone Veeam powershell proficient help us out with this?

Relevant portion of the script below:

Code: Select all

[string[]]$EMailTo = "testuser@test.com"
$EmailFrom = "testadmin@test.com"
$SMTPServer = "mail.test.com"
$EmailPassword = ConvertTo-SecureString -String Veeam123! -AsPlainText -force
$Credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $EmailFrom,$EmailPassword

$MonthlySessions = Get-VBRBackupSession | where {$_.creationtime -ge (Get-Date).AddDays(-30)}
$ReviewDate = Get-Date -format "MMM-dd-yyyy"
$Collection = @()
$logLocation = "c:\Test\" + $ReviewDate + "\"
$seedfile = import-csv "c:\scripts\seed.txt" -Header @("Job","Restore")

Foreach ($line in $seedfile){

$job = get-vbrjob -Name $line.Job 
$origfile = $line.Restore

$Details = New-Object PSObject 

# Puts Job Name in the report
$Details | Add-Member -Name "Veeam Job Name" -MemberType NoteProperty -Value $job.name
# Puts List of Servers in each job into report 
$Details | Add-Member -Name "Servers" -MemberType NoteProperty -Value (($job.GetObjectsInJob() | foreach { $_.Name }) -join "," ) 

# Pulls Monthly Results Failed / Warning / Success
$MonthlyJobSessions = $MonthlySessions | where {$_.origJobname -eq $job.name}
$FailedJobSessions = $MonthlyJobSessions | where {($_.result -eq "Failed") -and ($_.isretrymode -eq $False)}
$WarningJobSessions = $MonthlyJobSessions | where {($_.result -eq "Warning")}
$SuccessfulJobSessions = $MonthlyJobSessions | where {($_.result -eq "Success")}

# Puts review date in report
$Details | Add-Member -Name "Date of Review" -MemberType NoteProperty -Value $ReviewDate

# Adds Success / Warning / Fail to report
$Details | Add-Member -Name "Number of Successful Sessions" -MemberType NoteProperty -Value $SuccessfulJobSessions.count
$Details | Add-Member -Name "Number of Warning Sessions" -MemberType NoteProperty -Value $WarningJobSessions.count
$Details | Add-Member -Name "Number of Failed Sessions" -MemberType NoteProperty -Value $FailedJobSessions.count

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ restoration~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#pulls VM Name
$vmName  = ($job.GetObjectsInJob() | foreach { $_.Name })

#initiates restoration
$result = Get-VBRBackup | Get-VBRRestorePoint | sort CreationTime -Descending | where {$_.VMName -eq "$vmName"} | select -First 1 | Start-VBRWindowsFileRestore

$driveLetter = (Split-Path $origfile -Qualifier)
$mountpoint = $($result.mountsession.mounteddevices | ? { $_.Driveletter -eq $driveLetter }).MountPoint

#points to mounted restore point, need to pull location of C drive volume automatically instead of static
#$file = "C:\VeeamFLR\" + $vmName + "\Volume0" + (Split-Path $origfile -NoQualifier)

#points to mounted restore point
$file = $mountpoint + (Split-Path $origfile -NoQualifier)

#copies specified file

new-item -path $logLocation -ItemType directory -force | Out-Null
$destPath = $logLocation + $vmName + "\"
If (Test-Path $destPath)
	{
	Copy-Item $file $destPath -Force
	}
	Else
	{
	new-item -path $destPath -ItemType directory -force | Out-Null
	Copy-Item $file $destPath -Force
	}
#Copy-Item $file $destPath -Force
#write-host "Server = " $vmName "Restored File Path = " $file "Destination= " $destPath

#adds details to the report
$Details | Add-Member -Name "Restored Server" -MemberType NoteProperty -Value $vmName
$Details | Add-Member -Name "Restored Path" -MemberType NoteProperty -Value $origfile
$Details | Add-Member -Name "Restored Destination" -MemberType NoteProperty -Value $destPath

$filerestored = $logLocation + $vmName + "\" + (Split-Path $origfile -leaf)
if (Test-Path $filerestored)
	{
	$Details | Add-Member -Name "Restoration Test" -MemberType NoteProperty -Value "Successful"
	}
	Else
	{
	$Details | Add-Member -Name "Restoration Test" -MemberType NoteProperty -Value "Failed"
	}
	
Stop-VBRWindowsFileRestore $result

$Collection += $Details

}
$logfile = $logLocation + "BackupReview.html"
$report = $Collection | Convertto-html | Set-Content $logFile


$EmailBody = (Get-Content $logFile) -join "`n"

$EmailSubject = "$ReviewDate - Test Restoration"

Send-MailMessage -To $EMailTo `
    -Subject $EmailSubject `
    -Body $EmailBody `
	-BodyAsHtml `
    -SmtpServer $SMTPServer `
    -From $EmailFrom `
    -Credential $credentials `
    -Port 587
Sample seed file below, named "seed.txt", which includes the job name followed by the test file that we wish to restore - this is what we would like to change to reference the name of a specific server in the list of jobs instead of a job name:

Code: Select all

BackupJob1,C:\Program Files\EZClaim\EZData.mdb
BackupJob2,C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\ERRORLOG
BackupJob3,E:\Users\adonato\2013 expense checklist.xls
Thanks in advance for any assistance!
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Veeam Powershell Scripting Iterations

Post by veremin »

First, you should write VM names to the said file. Then, it's just a matter of finding necessary restore points, like:

Code: Select all

$seedfile = import-csv "c:\scripts\seed.txt" -Header @("VM","Restore")

Foreach ($line in $seedfile){
$origfile = $line.Restore
...............................................................................
$vmName  = $line.VM
$result = Get-VBRBackup | Get-VBRRestorePoint | sort CreationTime -Descending | where {$_.VMName -eq "$vmName"} | select -First 1 | Start-VBRWindowsFileRestore
Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests