PowerShell script exchange
Post Reply
Andreas Neufert
VP, Product Management
Posts: 6707
Liked: 1401 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Andys scripting corner - Ultimate start job script *update2*

Post by Andreas Neufert » 1 person likes this post

Hi everybody,

there are many ways to start a Veeam job.

The simplest I know is that 2 liner:

Code: Select all

Add-PSSnapin VeeamPSSnapIn
Get-VBRJob | where{$_.Name -eq "yourjobname"} | Start-VBRJob
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):

Code: Select all

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

Sample output (Jobname "leer"):

Code: Select all

PS E:\powershellscripts> E:\powershellscripts\ultimatestartjobscript.ps1
. 
. 
. 
. 
. 
. 
. 
. 
. 
. 
. 
2012-08-08 22:35:24Z Information: Start time: 2012-08-08 22:35:24Z
2012-08-08 22:35:24Z Information: Loading Veeam Powershell plugin
2012-08-08 22:35:24Z Information: Error counter reseted
2012-08-08 22:35:24Z Information: Local Server is a Veeam Backup & Replication Server
2012-08-08 22:35:25Z Information: Loading Job Object for leer
2012-08-08 22:35:25Z Information: Jobname  leer  was accepted


BottleneckMonitor : Veeam.Backup.Core.CJobPerfMon
Progress          : Veeam.Backup.Model.CProgressData
JobSourceType     : VDDK
CurrentPointId    : 00000000-0000-0000-0000-000000000000
OriginalSessionId : 5526fead-30bb-4d11-b959-089f383e430c
IsFullMode        : False
IsRetryMode       : False
Name              : leer
OrigJobName       : leer
BackupStats       : Veeam.Backup.Model.CBackupStats
Id                : 5526fead-30bb-4d11-b959-089f383e430c
JobType           : Backup
JobName           : leer
JobTypeString     : Other job type
Operation         : 
Description       : 
IsCompleted       : False
JobId             : 1d8d6613-73e9-4d36-a65b-2bf67da38cb5
Result            : None
State             : Starting
EndTime           : 01.01.1900 00:00:00
CreationTime      : 08.08.2012 22:35:26
AuxData           : 
JobSpec           : 
Logger            : Veeam.Backup.Core.XmlLogger
Tracer            : Veeam.Backup.Core.CSessionLogTracer

2012-08-08 22:35:27Z Information: Job  leer  started
2012-08-08 22:36:12Z Information: Job  leer  is running. Please wait...
2012-08-08 22:36:12Z Information: Max Time not reached
2012-08-08 22:36:42Z Information: Job  leer  is running. Please wait...
2012-08-08 22:36:42Z Information: Max Time not reached
2012-08-08 22:36:42Z Information: Job finished with result  Success
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2012-08-08 22:36:42Z Information: Script started at  2012-08-08 22:35:24Z
2012-08-08 22:36:42Z Information: Script finished at  2012-08-08 22:36:42Z
2012-08-08 22:36:42Z Information: Started Job:  leer
2012-08-08 22:36:42Z Information: Job finished successfully
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
CU Andy
Andreas Neufert
VP, Product Management
Posts: 6707
Liked: 1401 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Andys scripting corner - Ultimate start job script

Post by Andreas Neufert »

Hi everybody,

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*
Andreas Neufert
VP, Product Management
Posts: 6707
Liked: 1401 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Andys scripting corner - Ultimate start job script

Post by Andreas Neufert »

*UPDATE1*

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.

Code: Select all

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


Andreas Neufert
VP, Product Management
Posts: 6707
Liked: 1401 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Andys scripting corner - Ultimate start job script *upda

Post by Andreas Neufert »

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).

Code: Select all

#############################################################################################################
#                                                                                                           
# 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
billperrotta
Novice
Posts: 3
Liked: never
Joined: May 26, 2015 7:12 pm
Full Name: Bill Perrotta
Contact:

Re: Andys scripting corner - Ultimate start job script *upda

Post by billperrotta »

I have no idea really where to begin?

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.
Andreas Neufert
VP, Product Management
Posts: 6707
Liked: 1401 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Andys scripting corner - Ultimate start job script *upda

Post by Andreas Neufert »

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
billperrotta
Novice
Posts: 3
Liked: never
Joined: May 26, 2015 7:12 pm
Full Name: Bill Perrotta
Contact:

Re: Andys scripting corner - Ultimate start job script *upda

Post by billperrotta »

Can you be more specific?

Which guide explains how to schedule backups?

Will it work with veeam free edition?
Andreas Neufert
VP, Product Management
Posts: 6707
Liked: 1401 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Andys scripting corner - Ultimate start job script *upda

Post by Andreas Neufert »

http://www.veeam.com/veeam_backup_8_use ... are_pg.pdf
http://www.veeam.com/veeam_backup_evalu ... are_pg.pdf

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.

B&R isn´t that expensive.
Andreas Neufert
VP, Product Management
Posts: 6707
Liked: 1401 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Andys scripting corner - Ultimate start job script *upda

Post by Andreas Neufert »

Oh, there is a 30 day trail available on the website, where you can test the whole functionality.
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests