But what about input validation and error reporting?
Here are a ultimate sample script for "start job".
Please feel free to write feedback if you want to add someting or if you have questions.
You can use that script as an example script for error reporting and Veeam input validation as well.
Script (this one is an old Version - see answer Posts below for new Versions of this code):
#############################################################################################################
#
# Veeam
#
# Author: Andreas Neufert - Senior Systems Engineer Central EMEA (Germany)
#
# August 2012
#
# Version 1.04
#
# ./ultimatestartjobscript.ps1
#
# Compatibility tested with B&R V6.1
#
# !!! Attention please run the job you will use with this script at minimum once manually !!!!
#
################################################################################################################
#############################################################################################################
#Manual Input (With input validation). This values can also be set by an external script.
$Job = "yourjobname" #this is the job name
$maxtimeinminutes = "999" #amount of time in minute till the script was canceled (but not the job). Can be helpful if you want to do some actions if job didn´t finish in a specified time.
############################################################################################################
# Script
# Optical only - Empty lines to see every code in the powershellwindow (jump under Powershellprozessing bar)
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
#Set Starttimes
$StartTimecalc = get-date
$StartTime = get-date -format u
write-host $StartTime "Information: Start time:" $StartTime
# Load the Veeam Powershell snapin
Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
$ActualDate = get-date -format u
write-host $ActualDate "Information: Loading Veeam Powershell plugin"
#Reset Error Counter
$Resultcounter = 0
$error.clear()
$ActualDate = get-date -format u
write-host $ActualDate "Information: Error counter reseted"
#Check if Server is a Veeam backupserver
$BackupServer = Get-VBRlocalhost
IF ($BackupServer -eq $NULL)
{
$resultcounter++
$ActualDate = get-date -format u
write-host $ActualDate "Error: There is an issue with the local Veeam Backup instance or this server is no Veeam Backup & Replication server"
}
ELSE
{ #Point001
$ActualDate = get-date -format u
write-host $ActualDate "Information: Local Server is a Veeam Backup & Replication Server"
#Load Job Object
$JobObject = Get-VBRJob -Name $Job
$ActualDate = get-date -format u
write-host $ActualDate "Information: Loading Job Object for" $Job
#Check if Job exists
If ($JobObject -eq $Null){
$resultcounter++
$ActualDate = get-date -format u
write-host $ActualDate "Error: Job " $Job " does not exist"
}ELSE{ #Point002
$ActualDate = get-date -format u
write-host $ActualDate "Information: Jobname " $Job " was accepted"
#Check if Job still running
$1 = Get-VBRJob -name $Job
If ($1.Info.LatestStatus -ne "None") {#Point002a
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
# §§§§§§§ Start loops §§§§§§§§§§§§§
#Do Loop for Restart Job if Job Failed
Do{ #Point004
#Start Job (Backup/Replication)
Start-VBRJob $Jobobject –runasync
# With -runasync Powershell didn´t wait till job is finished and process the script imediately after sending the command to Veeam.
#Check if Start-VBRJob produced an Powershell error
If ($error.count -ge 1) {
$ActualDate = get-date -format u
write-host $ActualDate "Error: Job can not be startet or other powershell error"
}ELSE{ #Point003
$ActualDate = get-date -format u
write-host $ActualDate "Information: Job " $Job " started"
#Wait for Interface actualization
wait-event -timeout 15
#Now we have to check if there are problems with the job or the max run time is over.
#Check if Job still runs and post actual status every 30 seconds
Do{
wait-event -timeout 30
$1 = Get-VBRJob -name $Job
$ActualDate = get-date -format u
write-host $ActualDate "Information: Job " $Job " is running. Please wait..."
#Check if max time reached
$3 = ($StartTimecalc).AddMinutes($maxtimeinminutes)
$4 = get-date
$2 = "0"
If ($3 -le $4){
$2="timeisover"
$ActualDate = get-date -format u
write-host $ActualDate "Error: Max Time reached"
$resultcounter++
} ELSE {
$ActualDate = get-date -format u
write-host $ActualDate "Information: Max Time not reached"
}
} Until($1.Info.LatestStatus -ne "None" -or $2 -eq "timeisover")
$ActualDate = get-date -format u
write-host $ActualDate "Information: Job finished with result "$1.Info.LatestStatus
} #EndPoint003
} #EndPoint004
Until($1.Info.LatestStatus -eq "Failed" -or $1.Info.LatestStatus -eq "Success" -or $1.Info.LatestStatus -eq "Warning" -or $2 -eq "timeisover" )
# §§§§§§§ End loops §§§§§§§§§§§§§
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
#EndPoint002a
}ELSE{
$resultcounter++
$ActualDate = get-date -format u
write-host $ActualDate "Error: Job " $Job " already running or job was never started (please run job at minimum once manually before you are able to use that script)."
}
} #EndPoint002
} #EndPoint001
# If job failed, display a error message and increase error result counter
If ($1.Info.LatestStatus -eq "Failed"){
$resultcounter++
$ActualDate = get-date -format u
write-host $ActualDate "Error: " $Job " Job finished with an error"
} ELSE {}
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ Results §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ Results §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ Results §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
$Resultcounterend = $Resultcounter + $error.count
#Results
IF ($Resultcounterend -lt 1)
{
IF ($1.Info.LatestStatus -eq "Success"){
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
$ActualDate = get-date -format u
Write-host $ActualDate "Information: Script started at " $StartTime
Write-host $ActualDate "Information: Script finished at " $ActualDate
Write-host $ActualDate "Information: Started Job: " $Job
write-host $ActualDate "Information: Job finished successfully"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
} ELSE {
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
$ActualDate = get-date -format u
Write-host $ActualDate "Information: Script started at " $StartTime
Write-host $ActualDate "Information: Script finished at " $ActualDate
Write-host $ActualDate "Information: Started Job: " $Job
write-host $ActualDate "Warning: Job finished with a warning"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
}
}
ELSE
{
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
$ActualDate = get-date -format u
Write-host $ActualDate "Information: Script started at " $StartTime
Write-host $ActualDate "Information: Script finished at " $ActualDate
Write-host $ActualDate "Information: ERROR ERROR ERROR ERROR ERROR ERROR ERROR"
Write-host $ActualDate "Error: There are " $Resultcounterend "errors"
Write-host $ActualDate "Error: Job finished with errors"
Write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
}
#End
looking that some of you have trouble with the script on B&R 6.5 (with the command Get-VBRlocalhost)
I will investigate this later this week. Till now use this workaround:
Andreas Neufert *deleted the code ... see next answer post for an update*
V1.05
-Added add-pssnapin into the script internal error Monitoring. Veeam Snapin will be only loaded if it is not already loaded. Thanks to @tdewin for your sample script.
-Added Monitoring if Job is a new one. You now do not need to start a new job once manually before you can use that script. Thanks to @pizzim13 for the idea.
- checked compatibillity with v6.5 - no error found. If you run into Trouble, that some commands do not work, please start C:\Program Files\Veeam\Backup and Replication\Install-VeeamToolkit.ps1 once in a PS Admin window.
#############################################################################################################
#
# Veeam
#
# Author: Andreas Neufert - Systems Architect Central EMEA (Germany)
#
# February 2013
#
# Version 1.05
#
# ./ultimatestartjobscript.ps1
#
# Compatibility tested with B&R V6.1/6.5
#
#
################################################################################################################
#############################################################################################################
#Manual Input (With input validation). This values can also be set by an external script.
$Job = "notrunned" #this is the job name
$maxtimeinminutes = "999" #amount of time in minute till the script was canceled (but not the job). Can be helpful if you want to do some actions if job didn´t finish in a specified time.
############################################################################################################
# Script
# Optical only - Empty lines to see every code in the powershellwindow (jump under Powershellprozessing bar)
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
#Set Starttimes
$StartTimecalc = get-date
$StartTime = get-date -format u
write-host $StartTime "Information: Start time:" $StartTime
#Reset Error Counter
$Resultcounter = 0
$error.clear()
$ActualDate = get-date -format u
write-host $ActualDate "Information: Error counter reseted"
# Load the Veeam Powershell snapin
# Checks if Veeam SnapIn already loaded and if not it loads it
if ((Get-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null)
{
$ActualDate = get-date -format u
write-host $ActualDate "Information: Load Veeam PS SnapIn"
Add-PsSnapin VeeamPSSnapIn
} ELSE {
$ActualDate = get-date -format u
write-host $ActualDate "Information: Veeam PS SnapIn already loaded"
}
#Check if Server is a Veeam backupserver
$BackupServer = Get-VBRlocalhost
IF ($BackupServer -eq $NULL)
{
$resultcounter++
$ActualDate = get-date -format u
write-host $ActualDate "Error: There is an issue with the local Veeam Backup instance or this server is no Veeam Backup & Replication server"
}
ELSE
{ #Point001
$ActualDate = get-date -format u
write-host $ActualDate "Information: Local Server is a Veeam Backup & Replication Server"
#Load Job Object
$JobObject = Get-VBRJob -Name $Job
$ActualDate = get-date -format u
write-host $ActualDate "Information: Loading Job Object for" $Job
#Check if Job exists
If ($JobObject -eq $Null){
$resultcounter++
$ActualDate = get-date -format u
write-host $ActualDate "Error: Job " $Job " does not exist"
}ELSE{ #Point002
$ActualDate = get-date -format u
write-host $ActualDate "Information: Jobname " $Job " was accepted"
#Check if Job never runned
If ($JobObject.FindLastSession() -eq $null)
{
$ActualDate = get-date -format u
write-host $ActualDate "Information: Jobname " $Job " never runned"
$NewJob = "1"
} ELSE {
$NewJob = "0"
}
#Check if Job still running
$1 = Get-VBRJob -name $Job
If (($1.Info.LatestStatus -ne "None") -or ($NewJob -eq "1")) {#Point002a
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
# §§§§§§§ Start loops §§§§§§§§§§§§§
#Do Loop for Restart Job if Job Failed
Do{ #Point004
#Start Job (Backup/Replication)
Start-VBRJob $Jobobject –runasync
# With -runasync Powershell didn´t wait till job is finished and process the script imediately after sending the command to Veeam.
#Check if Start-VBRJob produced an Powershell error
If ($error.count -ge 1) {
$ActualDate = get-date -format u
write-host $ActualDate "Error: Job can not be startet or other powershell error"
}ELSE{ #Point003
$ActualDate = get-date -format u
write-host $ActualDate "Information: Job " $Job " started"
#Wait for Interface actualization
wait-event -timeout 15
#Now we have to check if there are problems with the job or the max run time is over.
#Check if Job still runs and post actual status every 30 seconds
Do{
wait-event -timeout 30
$1 = Get-VBRJob -name $Job
$ActualDate = get-date -format u
write-host $ActualDate "Information: Job " $Job " is running. Please wait..."
#Check if max time reached
$3 = ($StartTimecalc).AddMinutes($maxtimeinminutes)
$4 = get-date
$2 = "0"
If ($3 -le $4){
$2="timeisover"
$ActualDate = get-date -format u
write-host $ActualDate "Error: Max Time reached"
$resultcounter++
} ELSE {
$ActualDate = get-date -format u
write-host $ActualDate "Information: Max Time not reached"
}
} Until($1.Info.LatestStatus -ne "None" -or $2 -eq "timeisover")
$ActualDate = get-date -format u
write-host $ActualDate "Information: Job finished with result "$1.Info.LatestStatus
} #EndPoint003
} #EndPoint004
Until($1.Info.LatestStatus -eq "Failed" -or $1.Info.LatestStatus -eq "Success" -or $1.Info.LatestStatus -eq "Warning" -or $2 -eq "timeisover" )
# §§§§§§§ End loops §§§§§§§§§§§§§
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
#EndPoint002a
}ELSE{
$resultcounter++
$ActualDate = get-date -format u
write-host $ActualDate "Error: Job " $Job " is already running."
}
} #EndPoint002
} #EndPoint001
# If job failed, display a error message and increase error result counter
If ($1.Info.LatestStatus -eq "Failed"){
$resultcounter++
$ActualDate = get-date -format u
write-host $ActualDate "Error: " $Job " Job finished with an error"
} ELSE {}
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ Results §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ Results §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ Results §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
$Resultcounterend = $Resultcounter + $error.count
#Results
IF ($Resultcounterend -lt 1)
{
IF ($1.Info.LatestStatus -eq "Success"){
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
$ActualDate = get-date -format u
Write-host $ActualDate "Information: Script started at " $StartTime
Write-host $ActualDate "Information: Script finished at " $ActualDate
Write-host $ActualDate "Information: Started Job: " $Job
write-host $ActualDate "Information: Job finished successfully"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
} ELSE {
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
$ActualDate = get-date -format u
Write-host $ActualDate "Information: Script started at " $StartTime
Write-host $ActualDate "Information: Script finished at " $ActualDate
Write-host $ActualDate "Information: Started Job: " $Job
write-host $ActualDate "Warning: Job finished with a warning"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
}
}
ELSE
{
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
$ActualDate = get-date -format u
Write-host $ActualDate "Information: Script started at " $StartTime
Write-host $ActualDate "Information: Script finished at " $ActualDate
Write-host $ActualDate "Information: ERROR ERROR ERROR ERROR ERROR ERROR ERROR"
Write-host $ActualDate "Error: There are " $Resultcounterend "errors"
Write-host $ActualDate "Error: Job finished with errors"
Write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
}
#End
V1.06 Compatibility with actual Windows Versions and tested with v8.
This script need "Run as administrator" rights. If you use Remote Powershell with invoke command, make sure that you run the Source Powershell as well in "Run as Administrator" mode (the mode will be forwarded by invoke command).
#############################################################################################################
#
# Veeam
#
# Author: Andreas Neufert - Solutions Architect Central EMEA (Germany)
#
# March 2015
#
# Version 1.06
#
# ./ultimatestartjobscript.ps1
#
# Compatibility tested with B&R V6.1/6.5/v8
#
# This code need Powershell Administrator Mode to work correctly
#
#
################################################################################################################
#############################################################################################################
#Manual Input (With input validation). This values can also be set by an external script.
$Job = "test" #this is the job name
$maxtimeinminutes = "999" #amount of time in minute till the script was canceled (but not the job). Can be helpful if you want to do some actions if job didn´t finish in a specified time.
############################################################################################################
# Script
# Optical only - Empty lines to see every code in the powershellwindow (jump under Powershellprozessing bar)
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
Write-host ". "
#Set Starttimes
$StartTimecalc = get-date
$StartTime = get-date -format u
write-host $StartTime "Information: Start time:" $StartTime
#Reset Error Counter
$Resultcounter = 0
$error.clear()
$ActualDate = get-date -format u
write-host $ActualDate "Information: Error counter reseted"
# Load the Veeam Powershell snapin
# Checks if Veeam SnapIn already loaded and if not it loads it
# For old Powershell Versions you can use:
# if ((Get-PSSnapin -Name "VeeamPSSnapIn" -ErrorAction SilentlyContinue) -eq $null)
if ((Get-PSSnapin VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null)
{
$ActualDate = get-date -format u
write-host $ActualDate "Information: Load Veeam PS SnapIn"
$error.clear()
Add-PsSnapin VeeamPSSnapIn
} ELSE {
$ActualDate = get-date -format u
write-host $ActualDate "Information: Veeam PS SnapIn already loaded"
}
#Check if Server is a Veeam backupserver
$BackupServer = Get-VBRlocalhost
IF ($BackupServer -eq $NULL)
{
$resultcounter++
$ActualDate = get-date -format u
write-host $ActualDate "Error: There is an issue with the local Veeam Backup instance or this server is no Veeam Backup & Replication server"
}
ELSE
{ #Point001
$ActualDate = get-date -format u
write-host $ActualDate "Information: Local Server is a Veeam Backup & Replication Server"
#Load Job Object
$JobObject = Get-VBRJob -Name $Job
$ActualDate = get-date -format u
write-host $ActualDate "Information: Loading Job Object for" $Job
#Check if Job exists
If ($JobObject -eq $Null){
$resultcounter++
$ActualDate = get-date -format u
write-host $ActualDate "Error: Job " $Job " does not exist"
}ELSE{ #Point002
$ActualDate = get-date -format u
write-host $ActualDate "Information: Jobname " $Job " was accepted"
#Check if Job never runned
If ($JobObject.FindLastSession() -eq $null)
{
$ActualDate = get-date -format u
write-host $ActualDate "Information: Jobname " $Job " never runned"
$NewJob = "1"
} ELSE {
$NewJob = "0"
}
#Check if Job still running
$1 = Get-VBRJob -name $Job
If (($1.Info.LatestStatus -ne "None") -or ($NewJob -eq "1")) {#Point002a
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
# §§§§§§§ Start loops §§§§§§§§§§§§§
#Do Loop for Restart Job if Job Failed
Do{ #Point004
#Start Job (Backup/Replication)
Start-VBRJob $Jobobject –runasync
# With -runasync Powershell didn´t wait till job is finished and process the script imediately after sending the command to Veeam.
#Check if Start-VBRJob produced an Powershell error
If ($error.count -ge 1) {
$ActualDate = get-date -format u
write-host $ActualDate "Error: Job can not be startet or other powershell error"
}ELSE{ #Point003
$ActualDate = get-date -format u
write-host $ActualDate "Information: Job " $Job " started"
#Wait for Interface actualization
wait-event -timeout 15
#Now we have to check if there are problems with the job or the max run time is over.
#Check if Job still runs and post actual status every 30 seconds
Do{
wait-event -timeout 30
$1 = Get-VBRJob -name $Job
$ActualDate = get-date -format u
write-host $ActualDate "Information: Job " $Job " is running. Please wait..."
#Check if max time reached
$3 = ($StartTimecalc).AddMinutes($maxtimeinminutes)
$4 = get-date
$2 = "0"
If ($3 -le $4){
$2="timeisover"
$ActualDate = get-date -format u
write-host $ActualDate "Error: Max Time reached"
$resultcounter++
} ELSE {
$ActualDate = get-date -format u
write-host $ActualDate "Information: Max Time not reached"
}
} Until($1.Info.LatestStatus -ne "None" -or $2 -eq "timeisover")
$ActualDate = get-date -format u
write-host $ActualDate "Information: Job finished with result "$1.Info.LatestStatus
} #EndPoint003
} #EndPoint004
Until($1.Info.LatestStatus -eq "Failed" -or $1.Info.LatestStatus -eq "Success" -or $1.Info.LatestStatus -eq "Warning" -or $2 -eq "timeisover" )
# §§§§§§§ End loops §§§§§§§§§§§§§
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
#EndPoint002a
}ELSE{
$resultcounter++
$ActualDate = get-date -format u
write-host $ActualDate "Error: Job " $Job " is already running."
}
} #EndPoint002
} #EndPoint001
# If job failed, display a error message and increase error result counter
If ($1.Info.LatestStatus -eq "Failed"){
$resultcounter++
$ActualDate = get-date -format u
write-host $ActualDate "Error: " $Job " Job finished with an error"
} ELSE {}
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ Results §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ Results §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
# §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ Results §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
$Resultcounterend = $Resultcounter + $error.count
#Results
IF ($Resultcounterend -lt 1)
{
IF ($1.Info.LatestStatus -eq "Success"){
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
$ActualDate = get-date -format u
Write-host $ActualDate "Information: Script started at " $StartTime
Write-host $ActualDate "Information: Script finished at " $ActualDate
Write-host $ActualDate "Information: Started Job: " $Job
write-host $ActualDate "Information: Job finished successfully"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
} ELSE {
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
$ActualDate = get-date -format u
Write-host $ActualDate "Information: Script started at " $StartTime
Write-host $ActualDate "Information: Script finished at " $ActualDate
Write-host $ActualDate "Information: Started Job: " $Job
write-host $ActualDate "Warning: Job finished with a warning"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
}
}
ELSE
{
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
$ActualDate = get-date -format u
Write-host $ActualDate "Information: Script started at " $StartTime
Write-host $ActualDate "Information: Script finished at " $ActualDate
Write-host $ActualDate "Information: ERROR ERROR ERROR ERROR ERROR ERROR ERROR"
Write-host $ActualDate "Error: There are " $Resultcounterend "errors"
Write-host $ActualDate "Error: Job finished with errors"
Write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
}
#End
Do I put the server ips of the vmhosts into write hosts sections?
or the ips of the virtual vmware machines?
Never used scripts or psscripts to ever automate a backup.
We are using the free veeam backup and replication.
Since I'm not an experienced scripter, I'm trying to make sense about how to edit this script to backup my two vmhosts each with two vmware servers each. total of 4.
This script "only" start existing jobs. You do not need to use any scripting in your situation.
Add the vcenter if you have one or all esxi hosts to managed servers. Then create jobs with 10-40 VMs in it and schedlue them at last tab of the job wizard.
You can find at veeam.com-products- backup&replication-ressouces(bottom menue)
all needed guides.
At this script you only need to define the $job variable at manual input section to start the job.
If you really want to start a job by external scheduler
Free Edition do not support any Veeam own scheduling.
I recommend to buy Backup & Replication or Essentials Bundle.
However with v8 Update 2 there is a possibility to external schedule Veeam-ZIP (ad Hock Backup of a Single VM) by PowerShell. http://forums.veeam.com/post148234.html ... IP#p148234
I do not recommend to use this in production with 5+ VMs as it will be hard to monitor and there are only very basic settings available with free edition. For example no SQL Single DB restore with logfile rollforward available in free edition.
Veeam-ZIP will produce in any run a full backup, while all paid versions do change block tracking based incremental forever backups.