PowerShell script exchange
Post Reply
PNWMtnBiker
Enthusiast
Posts: 63
Liked: 8 times
Joined: Jan 16, 2014 11:12 pm
Full Name: Jon Dufour
Contact:

Sharing my PowerShell job creation script

Post by PNWMtnBiker »

Sharing the PowerShell script I've created to build all my backup and backup copy jobs at once for me. I hope it can help someone out in some way.
Credit to v.Eremin for helping me with the scheduling piece of this.

My script creates backup jobs based on folders in VCenter and prefixes the jobs with HSB and PARKWAY repository names for better organizing in the job window, modify for your needs. It copies most of the settings from my existing job, HSB Applications, just change to whatever you have.

* The $Array part requires having the powercli module for powershell installed, which I did for my Veeam backup manager as it just makes it all simpler for me. I can then get all the folders easily and create the jobs on them.
* Change the $hour and $minute variables to be what time you would want the first job to start.
* The IF statements increment the job start times by 5 minutes.

Code: Select all

# Set the repository, date and minutes
$Repository1 = Get-VBRBackupRepository -name HSB2
$Repository2 = Get-VBRBackupRepository -name PARKWAY1
$Date = Get-Date -format "MMM d yyyy"
$hour = 18
$minute = 05

# Copy the job options from HSB Applications, it's easier to copy than to create from scratch
$Job = Get-VBRJob -name "HSB Applications"
$Options = $Job.GetOptions()

# Set the job VSS options
$vssoptions=New-VBRJobVssOptions -forjob
$vssoptions.Enabled="True"
$vssoptions.GuestFSIndexingType="None"
$vssoptions.TransactionLogsTruncation="Always"

# Create the Array for all the jobs
$Array = Get-Folder | where {$_.type -eq "VM" -and $_.name -notlike "*disc*" -and $_.name -notlike "vm"} | foreach{$_.name} | sort

# Start the loop to create backup jobs
foreach ($_ in $Array)

{
# Create a backup job
$Description = "$_ backup job created by Jon on $Date"
Find-VBRViFolder -server vcenterslm -Name $_ | Add-VBRViBackupJob -Name "HSB $_" -BackupRepository $Repository1 -Description $Description

# Apply the job options
$NewJob = Get-VBRJob -name "HSB $_"
$NewJob.SetOptions($Options)

# Apply the VSS Settings
$NewJob | Set-VBRJobVssOptions -Options $vssoptions	
$NewJob | Set-VBRJobVssOptions -Credentials "Domain\vbackup"

# Set the job schedule and increment by 5 minutes
$ScheduleOptions = New-VBRJobScheduleOptions
$ScheduleOptions.OptionsContinuous.Enabled = $False
$ScheduleOptions.OptionsMonthly.Enabled = $False
$ScheduleOptions.OptionsScheduleAfterJob.IsEnabled = $False
$ScheduleOptions.OptionsPeriodically.Enabled = $False

IF ($minute -gt 55) 
{
$hour += 1
$minute = 00
}
IF ($hour -gt 23)
{
$hour = 00
}

ELSE 
{
$Date = Get-Date -Hour $hour -Minute $minute
$minute += 5
}

$ScheduleOptions.StartDateTime = $Date
$ScheduleOptions.OptionsDaily.Enabled = $True
$ScheduleOptions.OptionsDaily.Kind = "Everyday"
$ScheduleOptions.OptionsDaily.Time = $Date
$Options = $Job.GetOptions()
$Options.JobOptions.RunManually = $False
$Job.SetOptions($Options)

# Apply the schedule
Set-VBRJobScheduleOptions -Job $NewJob -Options $ScheduleOptions

# Disable the job because we don't want things taking off on their own yet
Disable-VBRJob -Job "HSB $_"
}

Add-VBRViBackupCopyJob -DirectOperation -Name "PARKWAY Development" -Backupjob "HSB Dev SharePoint","HSB Dev SQL","HSB Dev Web","HSB Testing" -Repository $Repository2
Add-VBRViBackupCopyJob -DirectOperation -Name "PARKWAY File and Print" -Backupjob "HSB File Servers","HSB Printing" -Repository $Repository2
Add-VBRViBackupCopyJob -DirectOperation -Name "PARKWAY Telecom" -Backupjob "HSB Telecom","HSB Voice" -Repository $Repository2
Add-VBRViBackupCopyJob -DirectOperation -Name "PARKWAY Domain" -Backupjob "HSB Domain Services" -Repository $Repository2
Add-VBRViBackupCopyJob -DirectOperation -Name "PARKWAY Monitor" -Backupjob "HSB Monitoring" -Repository $Repository2
Add-VBRViBackupCopyJob -DirectOperation -Name "PARKWAY Exchange" -Backupjob "HSB Exchange" -Repository $Repository2
Add-VBRViBackupCopyJob -DirectOperation -Name "PARKWAY SharePoint" -Backupjob "HSB SharePoint" -Repository $Repository2
Add-VBRViBackupCopyJob -DirectOperation -Name "PARKWAY Oracle" -Backupjob "HSB Oracle" -Repository $Repository2
Add-VBRViBackupCopyJob -DirectOperation -Name "PARKWAY SQL" -Backupjob "HSB SQL" -Repository $Repository2
Add-VBRViBackupCopyJob -DirectOperation -Name "PARKWAY Templates" -Backupjob "HSB Templates" -Repository $Repository2
Add-VBRViBackupCopyJob -DirectOperation -Name "PARKWAY Veeam" -Backupjob "HSB Veeam" -Repository $Repository2
Add-VBRViBackupCopyJob -DirectOperation -Name "PARKWAY Web" -Backupjob "HSB Web" -Repository $Repository2
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Sharing my PowerShell job creation script

Post by tsightler »

Nice script and thanks for sharing. One possible improvement that jumps out, I know you mention needing PowerCLI because of using Get-Folder but, unless I'm missing something (certainly possible), I believe you could use the following code to give you the exact same result and eliminate the dependency on PowerCLI:

Code: Select all

$Array = (Find-VBRViFolder -Server $vCenter | where {$_.name -notlike "*disc*" -and $_.name -notlike "vm"}).Name | Sort
I did a little testing, and the code above produced the same results as the Get-Folder code in all of the environments I had access to.

Assuming that code does work, then you could actually make some additional efficiency improvements to the code by simply loading all of the folder objects into $Array and then looping through the objects, eliminating the redundant calls to Find-VBRViFolder that are within the loop since you'll already have all of the objects. I didn't actually test this change, but I believe it would be as easy as modifying the line above to:

Code: Select all

$Array = Find-VBRViFolder -Server $vCenter | where {$_.name -notlike "*disc*" -and $_.name -notlike "vm"}) | Sort Name
Then modifying the loop to remove use $_.Name instead of $_, and removing the then unneeded calls to Find-VBRViFolder. If I manage to dig up a few minutes I'll give it a spin and see how it works.
PNWMtnBiker
Enthusiast
Posts: 63
Liked: 8 times
Joined: Jan 16, 2014 11:12 pm
Full Name: Jon Dufour
Contact:

Re: Sharing my PowerShell job creation script

Post by PNWMtnBiker »

Thanks Tom, you're right using the new code to populate the array works just fine with out having to load PowerCLI. I like that much better.

I tried the second half of you what you're saying, getting rid of the unneeded calls in the loop to Find-VBRViFolder although it would not work for me. I got an error stating that part of the statement was missing. I tried a couple different variations and was not successful. I do understand what you're saying, I'm just not seeing how to code it right.
veremin
Product Manager
Posts: 20406
Liked: 2299 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Sharing my PowerShell job creation script

Post by veremin »

PNWMtnBiker wrote:I tried the second half of you what you're saying, getting rid of the unneeded calls in the loop to Find-VBRViFolder although it would not work for me. I got an error stating that part of the statement was missing. I tried a couple different variations and was not successful. I do understand what you're saying, I'm just not seeing how to code it right.
Hi, Jon,

It seems that there is a small typo in the script Tom provided. More specifically, the bracket before sorting operation "(" isn't required in the second example. May be this is why you got "missing part" error?

Code: Select all

$Array = Find-VBRViFolder -Server $vCenter | where {$_.name -notlike "*disc*" -and $_.name -notlike "vm"} | Sort Name
Thanks.
PNWMtnBiker
Enthusiast
Posts: 63
Liked: 8 times
Joined: Jan 16, 2014 11:12 pm
Full Name: Jon Dufour
Contact:

Re: Sharing my PowerShell job creation script

Post by PNWMtnBiker »

v.Eremin wrote: Hi, Jon,

It seems that there is a small typo in the script Tom provided. More specifically, the bracket before sorting operation "(" isn't required in the second example. May be this is why you got "missing part" error?

Code: Select all

$Array = Find-VBRViFolder -Server $vCenter | where {$_.name -notlike "*disc*" -and $_.name -notlike "vm"} | Sort Name
Thanks.
Yes I did notice the typo right away and fixed it in my test, sorry I should have mentioned that. Where I had a problem was this line when trying to remove that extra call to Find-VBRViFolder,

Code: Select all

Find-VBRViFolder -server $vcenter -Name $_ | Add-VBRViBackupJob -Name "HSB $_" -BackupRepository $Repository1 -Description $Description
If I changed anything on that line I would get an error at the job creation. I tried a couple different variations with no luck. I didn't copy the error message so I'll have to run it again if you're curious, but it was something akin to the value cannot be null.
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Sharing my PowerShell job creation script

Post by tsightler »

I think you should be able to just do something like:

Code: Select all

$JobName = $_.Name
Add-VBRViBackupJob -Name "HSB $JobName" -BackupRepository $Repository1 -Description $Description -Entity $_
Of course, you'd also need to replace the other existing references to $_ to $JobName (I guess you could also just use $_.Name in all places). I'm travelling this week, but I'll give it a spin next week if you guys don't beat me to it. It's not a big deal anyway, but the Find-VBRViFolder command is a fairly slow command due to having to call vCenter so it would add some efficiency.
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests