I have a PS script which first does a vmotion of a running guest to another host. After vmotion it fires off a Veeam backup job (copied Andy's ultimate backup job script verbatim), and then when the job is finished it migrates the machine back to the original host. It works great when I run it manually in powershell. However, when I run it as a scheduled task it does the vmotion, then starts the job, then immediately performs the 2nd vmotion. It doesn't wait for the backup to finish like it does when you run it from PS interactively. What can I do to fix this?
For now, I have removed the 2nd vmotion from the script and then call a vmotion script from within the Veeam job upon completion, but this is messy and doesn't allow me to just run the job manually from Powershell or from Veeam. Script looks like the following:
Code: Select all
#############################################################################################################
## VMware vMotion Script Begins below
#############################################################################################################
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSSnapIn VMware.VimAutomation.Core
}
$strVC = "virtualcenter"
$strMoveMachine = "virtualcenter"
$strSafeHost = "host1"
$strStartHost = "host2"
Connect-VIServer $strVC
Get-VM $strMoveMachine | Move-VM -Destination ( Get-VMHost $strSafeHost )
#############################################################################################################
#
# 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 = "virtualcenter" #this is the job name
$maxtimeinminutes = "120" #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
#############################################################################################################
## VMware vMotion Script Begins below
#############################################################################################################
Get-VM $strMoveMachine | Move-VM -Destination ( Get-VMHost $strStartHost )