Help with script modification

PowerShell script exchange

Help with script modification

Veeam Logoby tfloor » Sun Mar 25, 2012 9:38 am

Hello,

I'm a beginner with powershell, but i have searched for usefull scripts and information and i have created a script for our environment. It's working very good, and it's very basic in powershell, so i can read / modify it by myself.

The script is for running veeam backup jobs, then according to the day it's running several backup exec jobs.
Everything is running after each other, so that i don't have to think about complex schedules.

Now the problem:.This weekend, the target for veeam was shutdown unexpectly. So the veeam backups are failed, then automatically the backup exec jobs where also started (because the script goes further).

All of the properties for the veeam backup jobs are set within the gui. So the script is doing not more than only starting it. But i need some more intelligence to it.

Can someone help me with the following modification inside my own script:
1. Only go further with the script if the veeam jobs are finished succesfull (maybe with a warning), but status failed is wrong! (something with get-VBRstatus).
2. A retry job could fixed my issue with the unexpected shutdown of target i think, because some minutes later the target was available (but job was failed in the middle of process). So adding a retry to the scripts will be very cool.
3. As i said before i now start only the job from the script, while the job- settings are done with the gui. So M-F = forward incrementals and on Satruday = Synthetic Full. Is that a good way?

Here is my script:
Code: Select all
# Register Veeam Powershell snapins to use Veeam specific powershell commands in Windows powershell.
Add-PSSnapin VeeamPSSnapIn

#ALLDAY SECTION


# Place each existing job in a variable and start each job after each other (powershell will wait for finishing job).
 $job1 = Get-VBRJob -name “Backup WDM-VM Linux”
 Start-VBRJob –Job ($job1)
 
 $job2 = Get-VBRJob -name “Backup WDM-VM OES”
 Start-VBRJob –Job ($job2)

 $job3 = Get-VBRJob -name “Backup WDM-VM Windows 2008”
 Start-VBRJob –Job ($job3)
 
 $job4 = Get-VBRJob -name “Backup WDM-VM Windows 2003”
 Start-VBRJob –Job ($job4)
 
 
 
 
#DAY SPECIFIC SECTION
 
 #In order to know if whe need to run backup exec jobs for M-F or the weekend, we need to identify that start time rather than the end time
 #So run a query that will check the last starttime of each job.

$Date1 = $job1.FindLastSession().progress.starttime
$Date2 = $job2.FindLastSession().progress.starttime
$Date3 = $job3.FindLastSession().progress.starttime
$Date4 = $job4.FindLastSession().progress.starttime

if($Date1.DayofWeek -eq "Saturday")

{

# Launch a backup exec Job that will perform a FULL File-Level backup from WFS1,WFS2,WGB1 to WQNAP1\Symantec.
& "C:\Program Files\Symantec\Backup Exec\bemcmd.exe" -o01 -jBackup_Exec_File_Level_OES_NSS_Physical_Vrij_Full

# Wait 4,5 hours before going further, so the previous backup job will be finished. Extend this time if needed.
Start-Sleep -s 16200

# Launch a backup exec job that will perform a File-Level backup of all Veeam backups (Synthetic Full and Config) on WQNAP1\Veeam\*.vib,*.vbm to tape.
& "C:\Program Files\Symantec\Backup Exec\bemcmd.exe" -o01 -jVeeam_Results_To_Tape_Vrij

}


else

{

# Launch a backup exec Job that will perform a DIFFERENTIAL File-Level backup from WFS1,WFS2,WGB1 to WQNAP1\Symantec.
& "C:\Program Files\Symantec\Backup Exec\bemcmd.exe" -o01 -jBackup_Exec_File_Level_OES_NSS_Physical_Ma_Do_Differential

# Wait 1 hour before going further, so the previous backup job will be finished. Extend this time if needed.
Start-Sleep -s 3600

# Remove all archive attributes from the WQNAP1\Veeam directory, so the folowwing backup exec job will not backup too much.
& "c:\windows\system32\attrib.exe" -A /S B:\Backup\Veeam\*.*

# Launch a backup exec job that will perform a File-Level backup of all Veeam backups (Forward-Incremental and Config) on WQNAP1\Veeam\*.vib,*.vbm to tape.
& "C:\Program Files\Symantec\Backup Exec\bemcmd.exe" -o01 -jVeeam_Results_To_Tape_Ma_Do

}

# Possible second ALLDAY Section:
# END

I hope someone can help me. ( i know the commands, but placing it in a existing script is difficult for me as beginner)

Thanks.
Tristan.
tfloor
Expert
 
Posts: 270
Liked: 14 times
Joined: Tue Jan 03, 2012 2:02 pm
Full Name: Tristan Floor

Re: Help with script modification

Veeam Logoby tfloor » Tue Mar 27, 2012 5:38 am

It would be nice if I can add an extra "if" to only start the backup exec job that will copy the veeam results to tape if the jobs were successful
Otherwise my tape backup is useless
tfloor
Expert
 
Posts: 270
Liked: 14 times
Joined: Tue Jan 03, 2012 2:02 pm
Full Name: Tristan Floor

Re: Help with script modification

Veeam Logoby tfloor » Wed Mar 28, 2012 3:47 pm

Where are the diehard powershell people who can throw some modifications out of their sleeve. :)
tfloor
Expert
 
Posts: 270
Liked: 14 times
Joined: Tue Jan 03, 2012 2:02 pm
Full Name: Tristan Floor

Re: Help with script modification

Veeam Logoby Andreas Neufert » Wed Mar 28, 2012 10:27 pm

Hi,

just a small idea. Think that works for you. My lab are down so this is not tested code.

Code: Select all
#Edit this and type in your jobname
$jobname = "My Job"

#Running Job status (do not edit this)
$jobnotactivestate = "None"

#Check if the job runs and wait till it is finished.
DO{
$job = Get-VBRJob | where {$_.name -eq $jobname}
Write-host "Waiting until the Veeam $Jobname Job is finished. Waiting 10 seconds"
Start-Sleep -s 10
}
UNTIL ($job.Info.LatestStatus -ne $jobnotactivestate)


#What to do if
If ($job.result -eq "Success")
{
Place your Backup to Tape Job here
}
Else
{
If ($job.result -eq "Warning")
{
Place your Backup to Tape Job here
}
Else
{
Write-host "Job finished with Error and nothing was backed up to tape"
}
}





Cu Andy
Andreas Neufert
Veeam Software
 
Posts: 2201
Liked: 360 times
Joined: Wed May 04, 2011 8:36 am
Location: Germany
Full Name: @AndyandtheVMs Veeam PM

Re: Help with script modification

Veeam Logoby tfloor » Thu Mar 29, 2012 7:01 am

Andreas,

That's indeed something i want.
I see:
#Edit this and type in your jobname
$jobname = "My Job"

But this one is only checking one job.

I start all the backups after each other, and backup everything in 1 backup exec job.
So i don't know what's good, because if 1 job errored, the other can be good.
Any idea how i can make my existing script a little bit more error friendly
tfloor
Expert
 
Posts: 270
Liked: 14 times
Joined: Tue Jan 03, 2012 2:02 pm
Full Name: Tristan Floor

Re: Help with script modification

Veeam Logoby Andreas Neufert » Thu Mar 29, 2012 7:19 am

Copy the DO-Until for your 4 jobs right after each other.

Do-Until Job1
Do Until Job2
Do-Until Job3
Do-Until Job4

Then proceed with the other script
Andreas Neufert
Veeam Software
 
Posts: 2201
Liked: 360 times
Joined: Wed May 04, 2011 8:36 am
Location: Germany
Full Name: @AndyandtheVMs Veeam PM

Re: Help with script modification

Veeam Logoby tfloor » Thu Mar 29, 2012 7:51 am

Sorry but i'm not so experienced with that.

I think i have to leave it as it is. Don't have the knowledge to modify my script, and i'm still thinking what's the best way to make it better.
So i'm not finishing thinking about it :)

Maybe other people can use this script as example
tfloor
Expert
 
Posts: 270
Liked: 14 times
Joined: Tue Jan 03, 2012 2:02 pm
Full Name: Tristan Floor

Re: Help with script modification

Veeam Logoby Andreas Neufert » Thu Mar 29, 2012 9:49 am

Hi,

give this a try. As I said before I didn´t test this in a lab. maybe you had to modify something.

Code: Select all

# Register Veeam Powershell snapins to use Veeam specific powershell commands in Windows powershell.
Add-PSSnapin VeeamPSSnapIn

#ALLDAY SECTION


# Place each existing job in a variable and start each job after each other (powershell will wait for finishing job).
 $job1 = Get-VBRJob -name “Backup WDM-VM Linux”
 Start-VBRJob –Job ($job1)
 
$job2 = Get-VBRJob -name “Backup WDM-VM OES”
 Start-VBRJob –Job ($job2)

 $job3 = Get-VBRJob -name “Backup WDM-VM Windows 2008”
 Start-VBRJob –Job ($job3)
 
$job4 = Get-VBRJob -name “Backup WDM-VM Windows 2003”
 Start-VBRJob –Job ($job4)
 
# Check Status

#Running Job status (do not edit this)
$jobnotactivestate = "None"

#Check if the job runs and wait till it is finished.
DO{
$job1a = Get-VBRJob | where {$_.name -eq $job1}
Write-host "Waiting until the Veeam $Jobname Job is finished. Waiting 10 seconds"
Start-Sleep -s 10
}
UNTIL ($job1a.Info.LatestStatus -ne $jobnotactivestate)
DO{
$job2a = Get-VBRJob | where {$_.name -eq $job2}
Write-host "Waiting until the Veeam $Jobname Job is finished. Waiting 10 seconds"
Start-Sleep -s 10
}
UNTIL ($job2a.Info.LatestStatus -ne $jobnotactivestate)
DO{
$job3a = Get-VBRJob | where {$_.name -eq $job3}
Write-host "Waiting until the Veeam $Jobname Job is finished. Waiting 10 seconds"
Start-Sleep -s 10
}
UNTIL ($job3a.Info.LatestStatus -ne $jobnotactivestate)
DO{
$job4a = Get-VBRJob | where {$_.name -eq $job4}
Write-host "Waiting until the Veeam $Jobname Job is finished. Waiting 10 seconds"
Start-Sleep -s 10
}
UNTIL ($job4a.Info.LatestStatus -ne $jobnotactivestate)



#No Backup to other Backup system if one job failed
If ($job1a.result -eq "Failed")
{
Write-host "Jobs finished with Errors and nothing was backed up to tape"
}
Else
{
If ($job2a.result -eq "Failed")
{
Write-host "Jobs finished with Errors and nothing was backed up to tape"
}
Else
{
If ($job3a.result -eq "Failed")
{
Write-host "Jobs finished with Errors and nothing was backed up to tape"
}
Else
{
If ($job4a.result -eq "Failed")
{
Write-host "Jobs finished with Errors and nothing was backed up to tape"
}
Else
{




#DAY SPECIFIC SECTION

#In order to know if whe need to run backup exec jobs for M-F or the weekend, we need to identify that start time rather than the end time
 #So run a query that will check the last starttime of each job.

$Date1 = $job1.FindLastSession().progress.starttime
$Date2 = $job2.FindLastSession().progress.starttime
$Date3 = $job3.FindLastSession().progress.starttime
$Date4 = $job4.FindLastSession().progress.starttime

if($Date1.DayofWeek -eq "Saturday")

{

# Launch a backup exec Job that will perform a FULL File-Level backup from WFS1,WFS2,WGB1 to WQNAP1\Symantec.
& "C:\Program Files\Symantec\Backup Exec\bemcmd.exe" -o01 -jBackup_Exec_File_Level_OES_NSS_Physical_Vrij_Full

# Wait 4,5 hours before going further, so the previous backup job will be finished. Extend this time if needed.
Start-Sleep -s 16200

# Launch a backup exec job that will perform a File-Level backup of all Veeam backups (Synthetic Full and Config) on WQNAP1\Veeam\*.vib,*.vbm to tape.
& "C:\Program Files\Symantec\Backup Exec\bemcmd.exe" -o01 -jVeeam_Results_To_Tape_Vrij

}


else

{

# Launch a backup exec Job that will perform a DIFFERENTIAL File-Level backup from WFS1,WFS2,WGB1 to WQNAP1\Symantec.
& "C:\Program Files\Symantec\Backup Exec\bemcmd.exe" -o01 -jBackup_Exec_File_Level_OES_NSS_Physical_Ma_Do_Differential

# Wait 1 hour before going further, so the previous backup job will be finished. Extend this time if needed.
Start-Sleep -s 3600

# Remove all archive attributes from the WQNAP1\Veeam directory, so the folowwing backup exec job will not backup too much.
& "c:\windows\system32\attrib.exe" -A /S B:\Backup\Veeam\*.*

# Launch a backup exec job that will perform a File-Level backup of all Veeam backups (Forward-Incremental and Config) on WQNAP1\Veeam\*.vib,*.vbm to tape.
& "C:\Program Files\Symantec\Backup Exec\bemcmd.exe" -o01 -jVeeam_Results_To_Tape_Ma_Do

}

# Possible second ALLDAY Section:
# END



#Close
}}}}





Andreas Neufert
Veeam Software
 
Posts: 2201
Liked: 360 times
Joined: Wed May 04, 2011 8:36 am
Location: Germany
Full Name: @AndyandtheVMs Veeam PM

Re: Help with script modification

Veeam Logoby tfloor » Thu Mar 29, 2012 2:11 pm

Andreas,

Nice, This is good information so i can go further!

Thanks
tfloor
Expert
 
Posts: 270
Liked: 14 times
Joined: Tue Jan 03, 2012 2:02 pm
Full Name: Tristan Floor

Re: Help with script modification

Veeam Logoby bteichner » Fri Feb 22, 2013 9:58 pm

I'm attempting to use this script for a similar scenario of sending only successful backups to tape. Everytime I run this though, the script always returns "Job finished with Error and nothing was backed up to tape". I've tried to break the script down, and run each piece separately. As far as I can tell, the $job variable is not being set correctly so $job.result always returns a blank result. Here is an example of some the output

Code: Select all
PS C:\> $jobname = "Windows2008"
PS C:\> Write-Host $jobname
Windows2008
PS C:\> $job = Get-VBRJob | where {$_.name -eq $jobname}
PS C:\> Write-Host $job
Veeam.Backup.Core.CBackupJob
PS C:\> Write-Host $job.result

PS C:\>


I don't work to much with PowerShell, so I may be doing something wrong.

Thanks
bteichner
Enthusiast
 
Posts: 30
Liked: 2 times
Joined: Mon Apr 30, 2012 5:54 pm
Full Name: Brian Teichner

Re: Help with script modification

Veeam Logoby v.Eremin » Sat Feb 23, 2013 11:45 am 1 person likes this post

Hi, Brian.

It seems that scripts provided above are using obsolete parameter “.Result” that isn’t presented in VB&R PS console any longer.

Nowadays it’s .GetLastResult() that should be used instead.

So, here is a piece of the code that is likely to give you a little bit more understanding how the script should be modified:

Code: Select all
asnp VeeamPSSnapin
$Job = Get-VBRJob -name "Name of your job"
If ($job.GetLastresult() -eq "Warning" -or $job.GetLastresult() -eq "Success")
{
Place your Backup to Tape Job here
}
Else
{
Write-host "Job finished with Error and nothing has been backed up to tape"
}

Hope this helps.
Thanks.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Help with script modification

Veeam Logoby bteichner » Mon Feb 25, 2013 3:35 pm

That worked. Thanks
bteichner
Enthusiast
 
Posts: 30
Liked: 2 times
Joined: Mon Apr 30, 2012 5:54 pm
Full Name: Brian Teichner

Re: Help with script modification

Veeam Logoby mdr6273 » Tue Nov 12, 2013 9:23 pm

v.Eremin wrote:Hi, Brian.

It seems that scripts provided above are using obsolete parameter “.Result” that isn’t presented in VB&R PS console any longer.

Nowadays it’s .GetLastResult() that should be used instead.

So, here is a piece of the code that is likely to give you a little bit more understanding how the script should be modified:

Code: Select all
asnp VeeamPSSnapin
$Job = Get-VBRJob -name "Name of your job"
If ($job.GetLastresult() -eq "Warning" -or $job.GetLastresult() -eq "Success")
{
Place your Backup to Tape Job here
}
Else
{
Write-host "Job finished with Error and nothing has been backed up to tape"
}

Hope this helps.
Thanks.


OK Maybe I'm a complete noob here and am missing something, but using this does not work at all for me. No matter the result it never matches "Warning" or "Success", and if I have it write $job.GetLastresult() to the screen it outputs "Getlastresult()" which is clearly not the last result for my job. The snapin successful loads so I'm not sure what the issue is.
mdr6273
Lurker
 
Posts: 1
Liked: never
Joined: Tue Nov 12, 2013 9:13 pm
Full Name: Mike Richardson

Re: Help with script modification

Veeam Logoby Andreas Neufert » Wed Nov 13, 2013 9:30 am

Are you on Win2012?
Andreas Neufert
Veeam Software
 
Posts: 2201
Liked: 360 times
Joined: Wed May 04, 2011 8:36 am
Location: Germany
Full Name: @AndyandtheVMs Veeam PM

Re: Help with script modification

Veeam Logoby v.Eremin » Wed Nov 13, 2013 9:42 am

Also, did you specify $Job variable correctly? What data will be shown if you just type $Job in the console after "specifying" step? Thanks.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Next

Return to PowerShell



Who is online

Users browsing this forum: No registered users and 5 guests