PowerShell script exchange
Post Reply
electricd7
Expert
Posts: 117
Liked: 7 times
Joined: Mar 27, 2012 10:13 pm
Full Name: Chad Killion
Contact:

Need a little help with Add-VBRViReplicaJob

Post by electricd7 » Nov 05, 2013 5:00 pm

Hello,

I am writing a quick script to create Replica jobs. I have gotten the script to create a job, but only if I send it directly to an ESXi host which I gathered with Get-VBRServer. I want to target this job to a Cluster on the DR side, but I tried to use the Find-VBRViEntity to return the cluster, but that didn't work. How do I set the -Server option for Add-VBRViReplicaJob to a Cluster item instead of directly to a host?

I am also going to want to set things like seeding and network remap and specific proxies on both sides. Is there any example code out there for this?

electricd7
Expert
Posts: 117
Liked: 7 times
Joined: Mar 27, 2012 10:13 pm
Full Name: Chad Killion
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by electricd7 » Nov 05, 2013 7:38 pm

OK, so I have the script completely working now except for being able to target a DR cluster for the replication job instead of a host and being able to set the Network Remap and Replica Seeding options. I am creating about 90 jobs, so it would be helpful to get these items into the script as entering those changes in the GUI is pretty tedious. I have copied the working script below for reference. Please help if you can:

Code: Select all

    #############################################################################################################
    #                                                                                                           
    # Veeam                                                                             
    #                                                                                                           
    # Author: Mostly copied from scripts created by Andreas Neufert - Systems Engineer Central EMEA (Germany)                                                                                   
    #                                                                                                           
    #
    #
    #                                                                                                
    #                                                                                                           
    # ./createreplicationjob.ps1
    #
    # Description:
    # Create replication job based on input
    #
    #                                                                   
    #
    #
    #############################################################################################################
    #                                                                                                       
    #############################################################################################################
    #Manual Input (With input validation)
    #$VM = "vmname"
    #$NameSuffix " Replication"
    #$Datastore "vmds_datastore"
    #$ResourcePool "Resources"
    #$Folder "vm"
    #$VMSuffix = "_replica"
    #$VSSEnabled = "TRUE"
    #$DisableJob = "TRUE" #Create job in a disabled state

    #External Input
    $VM = $args[0]
    $NameSuffix = $args[1]
    $Datastore = $args[2]
    $ResourcePool = $args[3]
    $Folder = $args[4]
    $VMSuffix = $args[5]
    $VSSEnabled = $args[6]
    $DisableJob = $args[7]

    ############################################################################################################
    # Editable Settings (be carefull => no input validation)

    $VCenter = "virtualcenter.domain.local" #vCenter/ESX-Host that was added to B&R Console - as displayed in the B&R Console
    $BackupRepository = "Veeam Repository"
    $CreateJobName = $VM+$NameSuffix
    $DRCluster = "DR 1"
    $DRHost = "UCS-Blade201.domain.local"

    $RetainCyclesadd = "1" # Restore Points


    $VSSDefaultUsername = "veeam.svc"
    $SetResultsToVmNotesadd = "True" #Write Results to VM description/custom field
    $VmAttributeNameadd = "BackupNotes" #Write Results to VM description/custom field

    #Scheduler
    $EnableScheduler = "True" #True/False
    $BackupWindow = "True" #True/False
    $RetrySpecifiedAdd = "True" #True/False
    $RetryTimesAdd = "5"
    $RetryTimeoutAdd = "5"
    $OptionsDailyAddKind = "SelectedDays" #Everyday/SelectedDays/WeekDays
    $OptionsDailyAddDays = "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday", "Saturday", "Sunday"
    $OptionsDailyAddTime = "20:00:00"
    #Attention: If you set a time earlier than now, the job will start at the next selected Weekday

    ############################################################################################################
    # Hardcoded Settings (do not change => Other settings than these are not considered in the code)
    $Algorithmadd = "Syntethic" #Syntethic means ReverseIncremental
    $SourceProxyAutoDetectadd = "True" #Automatic Proxy Selection
    $schedulemode = "Daily" #Daily/Monthly/Periodically/Continuous
    $MaximumErrorCount = 1000
    $VSSEnabledadd = "True" #True => Development switch




    #############################################################################################################
    # 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 Starttime
    $StartTime = get-date -format u
    write-host $StartTime "Information: Start time:" $StartTime



    #Loads Veeam Powershell Snapin
    Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
    $ActualDate = get-date -format u
    write-host $ActualDate "Information: Veeam Powershell plugin loaded"

    #Reset Error Counter
    $Resultcounter = 0
    $error.clear()
    $ActualDate = get-date -format u
    write-host $ActualDate "Information: Error counter reset"



    #Check if jobname is empty
    # =>IF 2<=
    if  ($CreateJobName -eq "") {
    $resultcounter++
    $ActualDate = get-date -format u
    Write-host $ActualDate "Error: Jobname is empty."

    # =>IF 2<=
    } ELSE {
    Write-host $ActualDate "Information: Jobname not empty."

    #Check if jobname exists
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    # =>IF 1<=
    if  ($JobnameObject -eq $Null)  {

    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Jobname does not exist. Jobname: " $CreateJobName
    Write-host $ActualDate "Information: Starting job creation for: " $CreateJobName

    #Check if VCenter/ESX exists
    $vbrserverVC = Get-VBRServer | where {$_.Name -eq $VCenter}
    # =>IF 9<=
    if  ($vbrserverVC -eq $Null)  {
    $resultcounter++
    $ActualDate = get-date -format u
    Write-host $ActualDate "Error: vcenter/ESX name not correct. vcenter/ESX: " $VCenter
    # =>IF 9<=
    } Else {
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: vcenter/ESX name accepted. vcenter/ESX: " $VCenter

    #Check if VM exists
    $vbrobjects = Find-VBRViEntity -Server $vbrserverVC -Name $VM
    # =>IF 10<=
    if  ($vbrobjects -eq $Null)  {
    $resultcounter++
    $ActualDate = get-date -format u
    Write-host $ActualDate "Error: VM does not exist. VM Name: " $VM
    # =>IF 10<=
    } Else {
    $ActualDate = get-date -format u
    Write-host $ActualDate  "Information: VM exists. VM Name: " $VM

    #Create job

    $ActualDate = get-date -format u
    Write-host $ActualDate  "Information: Create job " $CreateJobName " please wait..."

    $CreateBackupRepository = Get-VBRBackupRepository -Name $BackupRepository
    $CreateJobVM = Find-VBRViEntity -Server $vbrserverVC -Name $VM
    $CreateHost = Get-VBRServer -Type ESXi -Name $DRHost
    $CreateResourcePool = Find-VBRViResourcePool -Server $vbrserverVC -Name $ResourcePool
    $CreateDatastore = Find-VBRViDatastore -Server $vbrserverVC -Name $Datastore
    $CreateFolder = Find-VBRViFolder -Server $vbrserverVC -Name $Folder
    Write-host $CreateFolder
    Add-VBRViReplicaJob -Name $CreateJobName -Server $CreateHost -Entity $CreateJobVM -Datastore $CreateDatastore -ResourcePool $CreateResourcePool -Folder $CreateFolder -Suffix $VMSuffix -Description $CreateJobName -BackupRepository $CreateBackupRepository

    #Check if job name exists
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    # =>IF 11<=
    if  ($JobnameObject -eq $Null)  {
    $resultcounter++
    $ActualDate = get-date -format u
    Write-host $ActualDate "Error: Job was not created. Jobname: " $CreateJobName
    # =>IF 11<=
    } Else {
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Job" $CreateJobName "succesfully created."


    #Set Job Options
    $JobnameObjectOptions = $JobnameObject.GetOptions()
    $JobnameObjectOptions.BackupStorageOptions.RetainCycles = $RetainCyclesadd
    $JobnameObjectOptions.JobOptions.SourceProxyAutoDetect = $SourceProxyAutoDetectadd
    $JobnameObjectOptions.BackupStorageOptions.RetainDays = $RetainDaysadd
    $JobnameObjectOptions.ViSourceOptions.SetResultsToVmNotes = $SetResultsToVmNotesadd
    $JobnameObjectOptions.ViSourceOptions.VmAttributeName = $VmAttributeNameadd
    $JobnameObjectOptions.BackupTargetOptions.Algorithm = $Algorithmadd
    $JobnameObject.setoptions($JobnameObjectOptions)
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Job options set"



    # Set Scheduling Options
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    $JobnameObjectSchedule = $JobnameObject | Get-VBRjobscheduleoptions
    If ($RetrySpecifiedAdd -eq "True"){
    $JobnameObjectSchedule.RetrySpecified = $true
    } ELSE {
    $JobnameObjectSchedule.RetrySpecified = $false
    }
    $JobnameObjectSchedule.RetryTimes = $RetryTimesAdd 
    $JobnameObjectSchedule.RetryTimeout = $RetryTimeoutAdd
    If ($schedulemode -eq "Daily"){
    $JobnameObjectSchedule.OptionsDaily.Enabled = $true
    $JobnameObjectSchedule.OptionsMonthly.Enabled = $false
    $JobnameObjectSchedule.OptionsPeriodically.Enabled = $false
    $JobnameObjectSchedule.OptionsContinuous.Enabled = $false
    } ELSE {
    If ($schedulemode -eq "Monthly"){
    $JobnameObjectSchedule.OptionsDaily.Enabled = $false
    $JobnameObjectSchedule.OptionsMonthly.Enabled = $true
    $JobnameObjectSchedule.OptionsPeriodically.Enabled = $false
    $JobnameObjectSchedule.OptionsContinuous.Enabled = $false
    } ELSE {
    If ($schedulemode -eq "Periodically"){
    $JobnameObjectSchedule.OptionsDaily.Enabled = $false
    $JobnameObjectSchedule.OptionsMonthly.Enabled = $false
    $JobnameObjectSchedule.OptionsPeriodically.Enabled = $true
    $JobnameObjectSchedule.OptionsContinuous.Enabled = $false
    } ELSE {
    If ($schedulemode -eq "Continuous"){
    $JobnameObjectSchedule.OptionsDaily.Enabled = $false
    $JobnameObjectSchedule.OptionsMonthly.Enabled = $false
    $JobnameObjectSchedule.OptionsPeriodically.Enabled = $false
    $JobnameObjectSchedule.OptionsContinuous.Enabled = $true
    } ELSE {
    $ActualDate = get-date -format u
    write-host $ActualDate "Error: Schedulemode entry not correct"
    }}}}

    $JobnameObjectSchedule.OptionsDaily.Kind = $OptionsDailyAddKind
    $JobnameObjectSchedule.OptionsDaily.Days = $OptionsDailyAddDays
    $JobnameObjectSchedule.OptionsDaily.Time = $OptionsDailyAddTime
    $JobnameObjectSchedule.NextRun = $OptionsDailyAddTime
    $JobnameObjectSchedule.StartDateTime = $OptionsDailyAddTime
    if ($BackupWindow -eq "True"){
    $JobnameObjectSchedule.OptionsBackupWindow.Isenabled = $True
    $JobnameObjectSchedule.OptionsBackupWindow.BackupWindow = "<scheduler><Sunday>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</Sunday><Monday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Monday><Tuesday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Tuesday><Wednesday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Wednesday><Thursday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Thursday><Friday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Friday><Saturday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0</Saturday></scheduler>"
    }
    Set-VBRJobScheduleOptions -Job $JobnameObject -Options $JobnameObjectSchedule


    #$JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    If ($EnableScheduler -eq "True"){
    $JobnameObject.options.JobOptions.RunManually = $false
    $JobnameObject.EnableScheduler()
    } ELSE {
    $JobnameObject.options.JobOptions.RunManually = $true
    $JobnameObject.DisableScheduler()
    }

    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Job scheduling options set"

    # =>IF 41<=
    If ($VSSEnabled -eq "True") {
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS is enabled"

    #Enable VSS
    $JobnameObjectOptionsVSS = $JobnameObject.GetVSSOptions()
    $JobnameObjectOptionsVSS.Enabled = $VSSEnabledadd
    $JobnameObject.setVSSoptions($JobnameObjectOptionsVSS)
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS Enabled"	
	
    #Set VSS Default credentials
    $jobnameobject = Get-VBRJob -Name $CreateJobName
    $CredentialsVSSDefault = Get-VBRCredentials -name "*$VSSDefaultUsername*"
    $jobnameobject | Set-VBRJobVssOptions -Credentials $CredentialsVSSDefault
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS Default Options set"
 
    #Set VSS VM options
    $VMLoaded = Get-VBRJob -Name $CreateJobName | Get-VBRJobObject -Name $VM
    $oVSS = New-VBRJobVssOptions
    $oVSS.Enabled=$true
    $oVSS.GuestFSIndexingType="None"
    $oVSS.TransactionLogsTruncation="Never"
    Set-VBRJobObjectVssOptions -object $VMLoaded -options $oVSS

    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS VM credentials set"


    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Finished with job options"

    # =>IF 41<=
    } ELSE {
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS is disabled"
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Finished with job options"
    # =>IF 41<=
    }

    #Disable Job if necessary
    if ($DisableJob -eq "TRUE") {
    	Get-VBRJob -Name $CreateJobName | Disable-VBRJob
	$ActualDate = get-date -format u
	Write-host $ActualDate "Information: Job $CreateJobName Has Been Disabled"
    }

    # =>IF 11<=
    }
    # =>IF 10<=
    }
    # =>IF 9<=
    }


    # =>IF 1<=
    } Else {

    # =>IF 1<=
    }
    # =>IF 2<=
    }









    $Resultcounterend = $Resultcounter + $error.count

    #Results
    IF ($Resultcounterend -lt 1)
    {
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Job started at " $StartTime
    Write-host $ActualDate "Information: Job finished at " $ActualDate
    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: Job started at " $StartTime
    Write-host $ActualDate "Information: Job 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


tsightler
VP, Product Management
Posts: 5399
Liked: 2229 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by tsightler » Nov 05, 2013 9:52 pm

For the network mapping portion you might want to look at this thread. It shows how to use a "template" job that has the existing network mapping and copy it to a new job. This works fine for new jobs and I also listed an "unsupported" way to update existing jobs network mapping. I think I have actually figured out a supported way to do this with V7 but I need to test.

I think possible this post will help with pointing replica job at a cluster. I think you have to create the job pointing at a host, and then update the job options to point it at the cluster.

I haven't attempting to to replica mapping yet though, but I'll take a look at it. Perhaps someone else already knows.

electricd7
Expert
Posts: 117
Liked: 7 times
Joined: Mar 27, 2012 10:13 pm
Full Name: Chad Killion
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by electricd7 » Nov 05, 2013 10:57 pm

The cluster settings look pretty straight-forward, weird that it can't be done at job creation, but so be it. I am really interested in seeing what your supported method is for setting network-mapping, so if you could share when you can I would really appreciate it. I am sure I can implement it via the thread you linked (thanks for that), but if there is a better way, I am definitely interested.

Now to setup replica mapping....anyone...Bueller....Bueller... :)

veremin
Product Manager
Posts: 16778
Liked: 1405 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by veremin » Nov 06, 2013 8:56 am

I've done a quick test, and for now I can provide only the way replica seeding/mapping can be enabled. However, so far I haven't been able to find a way how to map replica to a certain VM:

Code: Select all

$Job = Get-VBRJob -name "Name of replication Job"
$Options = $Job.GetOptions()
$Options.ViReplicaTargetOptions.InitialSeeding = $True
$Options.ViReplicaTargetOptions.UseVmMapping = $True
$Job.SetOptions($Options) 
Thanks.

electricd7
Expert
Posts: 117
Liked: 7 times
Joined: Mar 27, 2012 10:13 pm
Full Name: Chad Killion
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by electricd7 » Nov 06, 2013 1:42 pm

tslightier: I am using the link you provided above for re-mapping the networks on a new job. It doesn't tick the box to remap networks, but if I manually tick the box on a job the PS created, then the networks are indeed in there remapped. Here is my code for reference:

Code: Select all

    #Remap Networks if Necessary
    if ($RemapNetworks -eq "TRUE") {
    $template_job = Get-VBRJob –Name $Template_JobName
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    $JobnameObject.Options.ViNetworkMappingOptions.NetworkMapping = $template_job.Options.ViNetworkMappingOptions.NetworkMapping
    $JobnameObject.SetOptions($JobnameObject.Options)
    }
v.Eremin: Thanks for the replica remap code. I have it implemented and working as far as it will go. If you come across a way to set the remap to field, that is the only missing piece from what I need. Thanks.

veremin
Product Manager
Posts: 16778
Liked: 1405 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by veremin » Nov 06, 2013 1:46 pm

Network mapping option can be enabled via the following script:

Code: Select all

$Job = Get-VBRJob -name "Name of replication job"
$Options = $Job.GetOptions()
$Options.ViReplicaTargetOptions.UseNetworkMapping = $true
$Job.SetOptions($Options) 
Hope this helps.
Thanks.

electricd7
Expert
Posts: 117
Liked: 7 times
Joined: Mar 27, 2012 10:13 pm
Full Name: Chad Killion
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by electricd7 » Nov 06, 2013 1:52 pm

Perfect! Thanks. All I have left is being able to point a replication job at a specific DR target machine. Thanks for all of your help everyone!

tsightler
VP, Product Management
Posts: 5399
Liked: 2229 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by tsightler » Nov 06, 2013 3:18 pm

Unfortunately replica mapping is a tough one. I don't believe that there is a way to do that via Powershell, even in V7, although I'd love for someone to prove me wrong. I could probably figure out an ugly hack to do it directly via the DB as the VMReplicaMapping table is pretty simple (Job ID, Src Obj ID, Tgt Obj ID) but that of course would be unsupported.

electricd7
Expert
Posts: 117
Liked: 7 times
Joined: Mar 27, 2012 10:13 pm
Full Name: Chad Killion
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by electricd7 » Nov 07, 2013 1:35 pm

tslightler: Is the Src Obj ID and Tgt Obj ID equal to the VMID from the vmware database or does it use some other identifier? I am guessing I could leverage the PowerCLI for VMWare to get the IDs I need and inject them into the Veeam DB. Obviously it wouldn't be supported, but its only for job creation and not actually doing any process. It will either work or it won't I suppose.

veremin
Product Manager
Posts: 16778
Liked: 1405 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by veremin » Nov 07, 2013 1:46 pm

I believe it should be VM GUID. However, you can create a test replication job that uses replica mapping functionality in order to confirm my assumption.

Thanks.

electricd7
Expert
Posts: 117
Liked: 7 times
Joined: Mar 27, 2012 10:13 pm
Full Name: Chad Killion
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by electricd7 » Nov 07, 2013 2:10 pm

Is the table where these fields live called BJobs? This is where it looks like every other setting for the job lives, but I can't find the right field. The fields in this table are as follows:

SELECT TOP 1000 [id]
,[type]
,[name]
,[target_host_id]
,[target_dir]
,[target_file]
,[options]
,[schedule]
,[is_deleted]
,[latest_result]
,[post_command_run_count]
,[vss_options]
,[vcb_host_id]
,[target_type]
,[description]
,[usn]
,[job_source_type]
,[included_size]
,[excluded_size]
,[schedule_enabled]
,[platform]
,[source_proxy_host_id]
,[target_proxy_host_id]
,[repository_id]
,[initialsync_repository_id]
,[parent_schedule_id]
FROM [VeeamBackup].[dbo].[BJobs]

tsightler
VP, Product Management
Posts: 5399
Liked: 2229 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by tsightler » Nov 07, 2013 3:24 pm

I don't think you need to mess with the BJobs table at all. The Object IDs in question are the Veeam generated object IDs which are typically stored in the BObjects table, however, I don't think they will actually exist there until the object has been added to at least one job.

The only supported way that I know of to get this ID if the object is not currently in a Veeam job (which a mapped replica probably will not be) is to temporarily add it to the job, grab the ID, and then delete the object from the job. However, I also know the following "hack" shared with me by an old coworker that allows you to get the Veeam OIDs for all objects:

Code: Select all

$Server = Get-VBRServer –name “<Name_of_vCenter>”
$ListOfObjects = [Veeam.Backup.Core.CHierarchyObj]::GetObjectsOnHost($Server.id)
After running this code the $ListOfObjects should contain all objects from the specified vCenter, including their Object ID, so you can get the Object ID of any object using something like this:

Code: Select all

$VMRef = (Find-VBRViEntity -Name $vmname).Reference
$VMOID = ($ListOfObjects | ? {$_.ObjectId -eq $VMRef}).Id
Note that I use the VMs Reference ID, i.e. MoRef ID, to identify the VM because the name might be duplicated, either within the environment, or because the hacked function being used to build the ListOfObjects sometimes returns stale VMs so if the VM was restored or something else happened you can get multiple VMs with the same name returned. Using the MoRef makes sure you're getting the correct VM.

tsightler
VP, Product Management
Posts: 5399
Liked: 2229 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by tsightler » Nov 07, 2013 5:06 pm

So just to round this out, here's a "working" script that seems to update the Replica Mapping for a job fairly well. In this script I'm hard coding a lot of stuff as it's just an example, however, I've tested it with a new job and creating new replica mapping and it seems to work fine. I'm not manipulating the table directly, as that seemed horrible, but I am calling an existing stored procedure in the VM database that updates the mappings. I felt like this was the more "clean" way. All the other stuff is really pretty simple (i.e. getting the IDs, etc.) and it wouldn't be too difficult to turn this into a function and just pass it some variables, but I'll leave that part to you. :mrgreen:

Please note, although I believe the risk is low, if this breaks your environment, you get to keep the pieces!!

Code: Select all

asnp VeeamPSSnapin

$JobName = "<Replica_Job>"  # Name of Job to Update
$DBServer = "<SQL_Server>" # Name of SQL Server
$DBName = "VeeamBackup" # Name of SQL Database
$VCServer = Get-VBRServer –name “<vCenter_Server>” # Name of vCenter Server
$ListOfObjects = [Veeam.Backup.Core.CHierarchyObj]::GetObjectsOnHost($VCServer.id)

# Get the Job and the Job ID
$ReplJob = Get-VBRJob -Name "Replicate Exchange"
$ReplJobId = $ReplJob.Id

# Get the VMs
$SourceVMName = "<Source_VM>"
$TargetVMName = "<Mapped_VM>"

# Get the GUID for the source and target objects
$SourceVMId = (Get-VBRJobObject -Job $JobName -Name $SourceVMName).Info.ObjectId
$TargetVMRef = (Find-VBRViEntity -Server $Server -Name $TargetVMName).Reference
$TargetVMId = ($ListOfObjects | ? {$_.ObjectRef -eq $TargetVMRef}).Id

# Make a new GUID for the row
$RowID = [guid]::NewGuid()

# Setup SQL server connection
$SqlConn = New-Object System.Data.SqlClient.SqlConnection
$SqlConn.ConnectionString = "Server=$DBServer;Database=$DBName;Integrated Security=True"
$SqlConn.Open()
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.Connection = $SqlConn

# Call the stored procedure to create the new mapping (this does all the proper stuff like updating USN, etc)
$SqlCmd.CommandText = "EXEC dbo.CreateReplicaMapping @row_id='$RowID', @row_job_id='$ReplJobId', @row_obj_id='$SourceVMId', @row_mapped_obj_id='$TargetVMId'"
$SqlCmd.ExecuteScalar()

# Close the SQL Connection
$SqlConn.Close()

tsightler
VP, Product Management
Posts: 5399
Liked: 2229 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by tsightler » Nov 07, 2013 6:35 pm

And just in case you need this, the above script only adds entries to the Replica Mapping table, you still have to enable it on the job with the following:
$Job = Get-VBRJob -name "Name of replication job"
$Job.Options.ViReplicaTargetOptions.InitialSeeding = $true
$Job.Options.ViReplicaTargetOptions.UseVmMapping = $true
$Job.SetOptions($Job.Options)

electricd7
Expert
Posts: 117
Liked: 7 times
Joined: Mar 27, 2012 10:13 pm
Full Name: Chad Killion
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by electricd7 » Nov 07, 2013 6:38 pm

Awesome! I had that second part already, but will definitely be giving the replica-mapping procedure a try. Should save lots of time when setting up replication 90+ times. Thanks.

electricd7
Expert
Posts: 117
Liked: 7 times
Joined: Mar 27, 2012 10:13 pm
Full Name: Chad Killion
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by electricd7 » Nov 07, 2013 7:13 pm

I'm almost there...The script for some reason isn't getting the $TargetVMId value. I have verified that its using the correct name for the DR vm, but the function returns NULL when searching $ListOfObjects for the $TargetVMRef. I did verify that $TargetVMRef is getting populated properly (vm-9997 in this case.) Any ideas?

tsightler
VP, Product Management
Posts: 5399
Liked: 2229 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by tsightler » Nov 07, 2013 7:17 pm

Did you verify that the $ListOfObjects is actually populated?

electricd7
Expert
Posts: 117
Liked: 7 times
Joined: Mar 27, 2012 10:13 pm
Full Name: Chad Killion
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by electricd7 » Nov 07, 2013 7:22 pm

Yes, I did a Write-Host $ListOfObjects back to screen and it appears to be populated only with "Name: VMName" for every VM in the vcenter instance.

electricd7
Expert
Posts: 117
Liked: 7 times
Joined: Mar 27, 2012 10:13 pm
Full Name: Chad Killion
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by electricd7 » Nov 07, 2013 7:23 pm

I don't see any data in the write-out that is equal to vm-9997, though. Only names from what I can tell.

tsightler
VP, Product Management
Posts: 5399
Liked: 2229 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by tsightler » Nov 07, 2013 7:28 pm

It should include the ObjectRef propery, but I'm seeing a few issue with with caching or something as I added a new VM and it doesn't show up. I'll come up with another way to get that, it'll be a little bit of a hack, but it's not too bad.

tsightler
VP, Product Management
Posts: 5399
Liked: 2229 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by tsightler » Nov 07, 2013 8:05 pm

OK, here's a new version that gets rid of the call to the unsupported function to find the Object ID (of course the call to the stored procedure to add replica mapping is still unsupported, but that seems to work well). With this version I instead simply add the Target mapping VM to the job temporarily, grab the object and the object ID, and then immediately delete it. If someone else knows a better, reliable way to get the ObjectID let me know.

The cool thing is that the only unsupported code in this below is the actual call to the stored procedure to add the IDs to the VM Mapping table, but even there, I suspect I'm calling the same stored procedure that the GUI calls, so that can't be too bad, right? And overall the code is pretty simple. Let me know if this still doesn't work, but I tested it with a brand new job in my lab with two new VMs and it worked perfectly so I think it should be OK.

Code: Select all

asnp VeeamPSSnapin

# Job Variables
$JobName = "<Repl_Job_Name>"      # Name of Job to Update
$DBServer = "<SQL_Server_Name>"   # Name of SQL Server
$DBName = "VeeamBackup"           # Name of SQL Database
$VCName = "<vCenter_Server_Name>" # Name of vCenter Server
$SourceVMName = "<Source_VM>"     # Name of source VM
$TargetVMName = "<Target_VM>"     # Name of target VM to map

# Get vCenter Object
$VCServer = Get-VBRServer –name $VCName

# Get the Job and the Job ID
$ReplJob = Get-VBRJob -Name $JobName
$ReplJobId = $ReplJob.Id

# Get Source VM ID
$SourceVMId = (Get-VBRJobObject -Job $JobName -Name $SourceVMName).Info.ObjectId

# Get Target VM ID
# This is ugly, but I couldn't figure out any other way to reliably get the Object ID
# for the target VM so we add the target for the VM mapping to the replica job,
# get the job object, grab the ID, and finally remove the target VM from the job.
Add-VBRViJobObject -Job $ReplJob -Entities (Find-VBRViEntity -Server $Server -Name $TargetVMName) | Out-Null
$TargetVMObj = Get-VBRJobObject -Job $ReplJob -Name $TargetVMName
$TargetVMId = $TargetVMObj.Info.ObjectId
$TargetVMObj.Delete()

# Make a new ID for the row
$RowID = [guid]::NewGuid()

# Setup SQL server connection
$SqlConn = New-Object System.Data.SqlClient.SqlConnection
$SqlConn.ConnectionString = "Server=$DBServer;Database=$DBName;Integrated Security=True"
$SqlConn.Open()
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.Connection = $SqlConn

# Call the stored procedure to create the new mapping (this does all the proper stuff like updating USN, etc)
$SqlCmd.CommandText = "EXEC dbo.CreateReplicaMapping @row_id='$RowID', @row_job_id='$ReplJobId', @row_obj_id='$SourceVMId', @row_mapped_obj_id='$TargetVMId'"
$SqlCmd.ExecuteScalar()

# Close the SQL Connection
$SqlConn.Close()

electricd7
Expert
Posts: 117
Liked: 7 times
Joined: Mar 27, 2012 10:13 pm
Full Name: Chad Killion
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by electricd7 » Nov 07, 2013 8:22 pm 1 person likes this post

Yea that works great! Thanks for the follow through on this. I have posted the complete script below if anyone needs an easy way to create new replication jobs from powershell and enable Network Mapping, Replica Seeding, and assigning job to pre-existing replicas. Most of it was stolen from Andy's job create script. Thanks again for all of your help!

Code: Select all

    ##############################################################################################################
    #    Create New Replication Job from PS                                                                                               
    #############################################################################################################
    #Manual Input (With input validation)
    #$VM = "VMName"
    #$NameSuffix " Replication"
    #$Datastore "vmds_replica_ds"
    #$ResourcePool "Resources"
    #$Folder "vm"
    #$VMSuffix = "_replica"
    #$VSSEnabled = "TRUE"
    #$DisableJob = "TRUE" #Create job in a disabled state
    #$RemapNetworks = "TRUE" #Will grab network remap settings from <Template_Job> and copy to new job
    #$ReplicaSeeding = "TRUE" #Will setup initial replica seeding. 
    #$MapReplica = "TRUE" #Will try to auto map to VM_replica on DR cluster

    #External Input
    $VM = $args[0]
    $NameSuffix = $args[1]
    $Datastore = $args[2]
    $ResourcePool = $args[3]
    $Folder = $args[4]
    $VMSuffix = $args[5]
    $VSSEnabled = $args[6]
    $DisableJob = $args[7]
    $RemapNetworks = $args[8]
    $ReplicaSeeding = $args[9]
    $MapReplica = $args[10]

    ############################################################################################################
    # Editable Settings (be carefull => no input validation)

    $VCenter = "virtualcenter.domain.local" #vCenter/ESX-Host that was added to B&R Console - as displayed in the B&R Console
    $BackupRepository = "Veeam SMB 1"
    $CreateJobName = $VM+$NameSuffix
    $DRCluster = "DR 1"
    $DRHost = "UCS1Blade1.domain.local"

    $RetainCyclesadd = "1" # Restore Points


    $VSSDefaultUsername = "veeam.svc"
    $SetResultsToVmNotesadd = "True" #Write Results to VM description/custom field
    $VmAttributeNameadd = "BackupNotes" #Write Results to VM description/custom field

    #Scheduler
    $EnableScheduler = "True" #True/False
    $BackupWindow = "True" #True/False
    $RetrySpecifiedAdd = "True" #True/False
    $RetryTimesAdd = "5"
    $RetryTimeoutAdd = "5"
    $OptionsDailyAddKind = "SelectedDays" #Everyday/SelectedDays/WeekDays
    $OptionsDailyAddDays = "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday", "Saturday", "Sunday"
    $OptionsDailyAddTime = "20:00:00"
    #Attention: If you set a time earlier than now, the job will start at the next selected Weekday

    ############################################################################################################
    # Hardcoded Settings (do not change => Other settings than these are not considered in the code)
    $Algorithmadd = "Syntethic" #Syntethic means ReverseIncremental
    $SourceProxyAutoDetectadd = "True" #Automatic Proxy Selection
    $schedulemode = "Daily" #Daily/Monthly/Periodically/Continuous
    $MaximumErrorCount = 1000
    $VSSEnabledadd = "True" #True => Development switch
    $Template_JobName = "<Template_Job>"
    $SQLServer = "Veeam1\VEEAMSQL2008R2" #Needed for $MapReplica
    $SQLDB = "VeeamBackup"




    #############################################################################################################
    # 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 Starttime
    $StartTime = get-date -format u
    write-host $StartTime "Information: Start time:" $StartTime



    #Loads Veeam Powershell Snapin
    Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
    $ActualDate = get-date -format u
    write-host $ActualDate "Information: Veeam Powershell plugin loaded"

    #Reset Error Counter
    $Resultcounter = 0
    $error.clear()
    $ActualDate = get-date -format u
    write-host $ActualDate "Information: Error counter reset"



    #Check if jobname is empty
    # =>IF 2<=
    if  ($CreateJobName -eq "") {
    $resultcounter++
    $ActualDate = get-date -format u
    Write-host $ActualDate "Error: Jobname is empty."

    # =>IF 2<=
    } ELSE {
    Write-host $ActualDate "Information: Jobname not empty."

    #Check if jobname exists
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    # =>IF 1<=
    if  ($JobnameObject -eq $Null)  {

    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Jobname does not exist. Jobname: " $CreateJobName
    Write-host $ActualDate "Information: Starting job creation for: " $CreateJobName

    #Check if VCenter/ESX exists
    $vbrserverVC = Get-VBRServer | where {$_.Name -eq $VCenter}
    # =>IF 9<=
    if  ($vbrserverVC -eq $Null)  {
    $resultcounter++
    $ActualDate = get-date -format u
    Write-host $ActualDate "Error: vcenter/ESX name not correct. vcenter/ESX: " $VCenter
    # =>IF 9<=
    } Else {
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: vcenter/ESX name accepted. vcenter/ESX: " $VCenter

    #Check if VM exists
    $vbrobjects = Find-VBRViEntity -Server $vbrserverVC -Name $VM
    # =>IF 10<=
    if  ($vbrobjects -eq $Null)  {
    $resultcounter++
    $ActualDate = get-date -format u
    Write-host $ActualDate "Error: VM does not exist. VM Name: " $VM
    # =>IF 10<=
    } Else {
    $ActualDate = get-date -format u
    Write-host $ActualDate  "Information: VM exists. VM Name: " $VM

    #Create job

    $ActualDate = get-date -format u
    Write-host $ActualDate  "Information: Create job " $CreateJobName " please wait..."

    $CreateBackupRepository = Get-VBRBackupRepository -Name $BackupRepository
    $CreateJobVM = Find-VBRViEntity -Server $vbrserverVC -Name $VM
    $CreateHost = Get-VBRServer -Type ESXi -Name $DRHost
    $CreateResourcePool = Find-VBRViResourcePool -Server $vbrserverVC -Name $ResourcePool
    $CreateDatastore = Find-VBRViDatastore -Server $vbrserverVC -Name $Datastore
    $CreateFolder = Find-VBRViFolder -Server $vbrserverVC -Name $Folder
    Write-host $CreateFolder
    Add-VBRViReplicaJob -Name $CreateJobName -Server $CreateHost -Entity $CreateJobVM -Datastore $CreateDatastore -ResourcePool $CreateResourcePool -Folder $CreateFolder -Suffix $VMSuffix -Description $CreateJobName -BackupRepository $CreateBackupRepository

    #Check if job name exists
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    # =>IF 11<=
    if  ($JobnameObject -eq $Null)  {
    $resultcounter++
    $ActualDate = get-date -format u
    Write-host $ActualDate "Error: Job was not created. Jobname: " $CreateJobName
    # =>IF 11<=
    } Else {
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Job" $CreateJobName "succesfully created."


    #Set Job Options
    $JobnameObjectOptions = $JobnameObject.GetOptions()
    $JobnameObjectOptions.BackupStorageOptions.RetainCycles = $RetainCyclesadd
    $JobnameObject.setoptions($JobnameObjectOptions)
    Get-VBRJob | ?{$_.Name -eq $CreateJobName} | Set-VBRJobAdvancedStorageOptions -CompressionLevel 5 -StorageBlockSize KbBlockSize256 
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Job options set"
	
    #Point Job at Cluster if necessary
    if ($DRCluster -ne "") {
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    $Cluster = Find-VBRViEntity -Server $vbrserverVC -HostsAndClusters -name $DRCluster
    $OptionsToSet = $JobnameObject.Options
    $OptionsToSet.ViReplicaTargetOptions.ClusterName = $Cluster.Name
    $OptionsToSet.ViReplicaTargetOptions.ClusterReference = $Cluster.Reference
    $OptionsToSet.ViReplicaTargetOptions.HostReference = ""
    $JobnameObject.SetOptions($OptionsToSet) 
    }
	
    #Remap Networks if Necessary
    if ($RemapNetworks -eq "TRUE") {
    $template_job = Get-VBRJob –Name $Template_JobName
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    $JobnameObject.Options.ViNetworkMappingOptions.NetworkMapping = $template_job.Options.ViNetworkMappingOptions.NetworkMapping
    $JobnameObject.Options.ViReplicaTargetOptions.UseNetworkMapping = $true
    $JobnameObject.SetOptions($JobnameObject.Options)
    }
	
    #Setup Replica Seeding if Necessary
    if ($ReplicaSeeding -eq "TRUE") {
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    $Options = $JobnameObject.GetOptions()
    $Options.ViReplicaTargetOptions.InitialSeeding = $True
    $Options.ViReplicaTargetOptions.UseVmMapping = $True
    $JobnameObject.SetOptions($Options) 
    }
	
    #Map Replica to DR VM if Necessary
    if ($MapReplica -eq "TRUE") {

	$JobName = $CreateJobName  # Name of Job to Update
	$DBServer = $SQLServer # Name of SQL Server
	$DBName = $SQLDB # Name of SQL Database
	$SourceVMName = $VM # Name of source VM
	$TargetVMName = $VM+$VMSuffix # Name of target VM to map
	# Get vCenter Object
	$VCServer = $vbrserverVC
	# Get the Job and the Job ID
	$ReplJob = Get-VBRJob -Name $CreateJobName
	$ReplJobId = $ReplJob.Id
	# Get Source VM ID
	$SourceVMId = (Get-VBRJobObject -Job $CreateJobName -Name $SourceVMName).Info.ObjectId
	# Get Target VM ID
	Add-VBRViJobObject -Job $ReplJob -Entities (Find-VBRViEntity -Server $vbrserverVC -Name $TargetVMName) | Out-Null
	$TargetVMObj = Get-VBRJobObject -Job $ReplJob -Name $TargetVMName
	$TargetVMId = $TargetVMObj.Info.ObjectId
	$TargetVMObj.Delete()
	# Make a new ID for the row
	$RowID = [guid]::NewGuid()
	# Setup SQL server connection
	$SqlConn = New-Object System.Data.SqlClient.SqlConnection
	$SqlConn.ConnectionString = "Server=$DBServer;Database=$DBName;Integrated Security=True"
	$SqlConn.Open()
	$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
	$SqlCmd.Connection = $SqlConn
	# Call the stored procedure to create the new mapping (this does all the proper stuff like updating USN, etc)
	$SqlCmd.CommandText = "EXEC dbo.CreateReplicaMapping @row_id='$RowID', @row_job_id='$ReplJobId', @row_obj_id='$SourceVMId', @row_mapped_obj_id='$TargetVMId'"
	$SqlCmd.ExecuteScalar()
	# Close the SQL Connection
	$SqlConn.Close()
	
    }

    # Set Scheduling Options
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    $JobnameObjectSchedule = $JobnameObject | Get-VBRjobscheduleoptions
    If ($RetrySpecifiedAdd -eq "True"){
    $JobnameObjectSchedule.RetrySpecified = $true
    } ELSE {
    $JobnameObjectSchedule.RetrySpecified = $false
    }
    $JobnameObjectSchedule.RetryTimes = $RetryTimesAdd 
    $JobnameObjectSchedule.RetryTimeout = $RetryTimeoutAdd
    If ($schedulemode -eq "Daily"){
    $JobnameObjectSchedule.OptionsDaily.Enabled = $true
    $JobnameObjectSchedule.OptionsMonthly.Enabled = $false
    $JobnameObjectSchedule.OptionsPeriodically.Enabled = $false
    $JobnameObjectSchedule.OptionsContinuous.Enabled = $false
    } ELSE {
    If ($schedulemode -eq "Monthly"){
    $JobnameObjectSchedule.OptionsDaily.Enabled = $false
    $JobnameObjectSchedule.OptionsMonthly.Enabled = $true
    $JobnameObjectSchedule.OptionsPeriodically.Enabled = $false
    $JobnameObjectSchedule.OptionsContinuous.Enabled = $false
    } ELSE {
    If ($schedulemode -eq "Periodically"){
    $JobnameObjectSchedule.OptionsDaily.Enabled = $false
    $JobnameObjectSchedule.OptionsMonthly.Enabled = $false
    $JobnameObjectSchedule.OptionsPeriodically.Enabled = $true
    $JobnameObjectSchedule.OptionsContinuous.Enabled = $false
    } ELSE {
    If ($schedulemode -eq "Continuous"){
    $JobnameObjectSchedule.OptionsDaily.Enabled = $false
    $JobnameObjectSchedule.OptionsMonthly.Enabled = $false
    $JobnameObjectSchedule.OptionsPeriodically.Enabled = $false
    $JobnameObjectSchedule.OptionsContinuous.Enabled = $true
    } ELSE {
    $ActualDate = get-date -format u
    write-host $ActualDate "Error: Schedulemode entry not correct"
    }}}}

    $JobnameObjectSchedule.OptionsDaily.Kind = $OptionsDailyAddKind
    $JobnameObjectSchedule.OptionsDaily.Days = $OptionsDailyAddDays
    $JobnameObjectSchedule.OptionsDaily.Time = $OptionsDailyAddTime
    $JobnameObjectSchedule.NextRun = $OptionsDailyAddTime
    $JobnameObjectSchedule.StartDateTime = $OptionsDailyAddTime
    if ($BackupWindow -eq "True"){
    $JobnameObjectSchedule.OptionsBackupWindow.Isenabled = $True
    $JobnameObjectSchedule.OptionsBackupWindow.BackupWindow = "<scheduler><Sunday>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</Sunday><Monday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Monday><Tuesday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Tuesday><Wednesday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Wednesday><Thursday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Thursday><Friday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Friday><Saturday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0</Saturday></scheduler>"
    }
    Set-VBRJobScheduleOptions -Job $JobnameObject -Options $JobnameObjectSchedule


    #$JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    If ($EnableScheduler -eq "True"){
    $JobnameObject.options.JobOptions.RunManually = $false
    $JobnameObject.EnableScheduler()
    } ELSE {
    $JobnameObject.options.JobOptions.RunManually = $true
    $JobnameObject.DisableScheduler()
    }

    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Job scheduling options set"

    # =>IF 41<=
    If ($VSSEnabled -eq "True") {
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS is enabled"

    #Enable VSS
    $JobnameObjectOptionsVSS = $JobnameObject.GetVSSOptions()
    $JobnameObjectOptionsVSS.Enabled = $VSSEnabledadd
    $JobnameObject.setVSSoptions($JobnameObjectOptionsVSS)
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS Enabled"	
	
    #Set VSS Default credentials
    $jobnameobject = Get-VBRJob -Name $CreateJobName
    $CredentialsVSSDefault = Get-VBRCredentials -name "*$VSSDefaultUsername*"
    $jobnameobject | Set-VBRJobVssOptions -Credentials $CredentialsVSSDefault
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS Default Options set"
 
    #Set VSS VM options
    $VMLoaded = Get-VBRJob -Name $CreateJobName | Get-VBRJobObject -Name $VM
    $oVSS = New-VBRJobVssOptions
    $oVSS.Enabled=$true
    $oVSS.GuestFSIndexingType="None"
    $oVSS.TransactionLogsTruncation="Never"
    Set-VBRJobObjectVssOptions -object $VMLoaded -options $oVSS

    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS VM credentials set"


    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Finished with job options"

    # =>IF 41<=
    } ELSE {
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS is disabled"
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Finished with job options"
    # =>IF 41<=
    }

    #Disable Job if necessary
    if ($DisableJob -eq "TRUE") {
    	Get-VBRJob -Name $CreateJobName | Disable-VBRJob
	$ActualDate = get-date -format u
	Write-host $ActualDate "Information: Job $CreateJobName Has Been Disabled"
    }

    # =>IF 11<=
    }
    # =>IF 10<=
    }
    # =>IF 9<=
    }


    # =>IF 1<=
    } Else {

    # =>IF 1<=
    }
    # =>IF 2<=
    }









    $Resultcounterend = $Resultcounter + $error.count

    #Results
    IF ($Resultcounterend -lt 1)
    {
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Job started at " $StartTime
    Write-host $ActualDate "Information: Job finished at " $ActualDate
    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: Job started at " $StartTime
    Write-host $ActualDate "Information: Job 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


tsightler
VP, Product Management
Posts: 5399
Liked: 2229 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by tsightler » Nov 07, 2013 10:59 pm

Excellent and great job! Thanks for sharing the final result. I've had the request to do replica mapping via Powershell several times and have always hated having to tell people that it's not possible so your post motivated me to take a shot at it. Turned out not to be too bad.

Andreas Neufert
Veeam Software
Posts: 3747
Liked: 665 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by Andreas Neufert » Feb 24, 2014 2:37 pm

Hi everybody,

thanks for adding this code to the forum. Really great one.

I tried it with direct ESXi host as target and it didn´t work because of folder settings.
I changed it a bit for my demand.

Code: Select all

#On the BRE/SQL Server
#enable-wsmancredssp -role server
#set-item wsman:localhost\Shell\MaxMemoryPerShellMB 1024

#On the Client
#winrm quickconfig
#enable-wsmancredssp -role client -delegatecomputer backup, backup.demoinfra.an.veeam.de
#set-item wsman:localhost\Shell\MaxMemoryPerShellMB 1024
#
#gpedit.msc
#Computer Configuration -> Administrative Templates -> System -> Credentials Delegation -> Allow Fresh Credentials with NTLM-only Server Authentication
#Enable and add SPN and FQDN of the Servers in the list,like this:
#WSMAN/Servername               (without #)
#WSMAN/servername.domain.tld    (without #)
#
#PS with Administrative rights "gpupdate /force"

write-host " "
write-host " "
write-host " "
write-host " "
write-host " "
write-host " "
write-host " "
$actualtime = get-date
$actualtimeformated =$actualtime.ToUniversalTime()
Write-host $actualtime "Information: Loading Input"
$backupserver = "backup"
$username = "demoinfra\Administrator"
$password = convertto-securestring -string "Sumsi1!" -asplaintext -force
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $Username, $Password
$session = New-PSSession $backupserver -authentication CredSSP -Credential $credentials

$actualtime = get-date
$actualtimeformated =$actualtime.ToUniversalTime()
Write-host $actualtime "Information: Connectiong to Backup Server and processing commands..."

invoke-command -session $session -scriptblock{


    ##############################################################################################################
    #    Create New Replication Job from PS                                                                                               
    #############################################################################################################
    #Manual Input (With input validation)
    $VM = "Empty"
    $NamePrefix = "Replication Job for VM " 
    $Datastore = "datastore1-small" #Target Datastore
    $ResourcePool = "replicas" #Target Resource Pool
    $Folder = ""  #leave empty if your target is a direct esx host (no vcenter involved)
    $VMSuffix = "_replica"
    $VSSEnabled = "FALSE"
    $DisableJob = "FALSE" #Create job in a disabled state
    $RemapNetworks = "FALSE" #Will grab network remap settings from <Template_Job> and copy to new job
    $ReplicaSeeding = "FALSE" #Will setup initial replica seeding. 
    $MapReplica = "FALSE" #Will try to auto map to VM_replica on DR cluster

    #External Input
    #$VM = $args[0]
    #$NameSuffix = $args[1]
    #$Datastore = $args[2]
    #$ResourcePool = $args[3]
    #$Folder = $args[4]
    #$VMSuffix = $args[5]
    #$VSSEnabled = $args[6]
    #$DisableJob = $args[7]
    #$RemapNetworks = $args[8]
    #$ReplicaSeeding = $args[9]
    #$MapReplica = $args[10]

    ############################################################################################################
    # Editable Settings (be carefull => no input validation)

    $VCenter = "esx" #vCenter/ESX-Host that was added to B&R Console - as displayed in the B&R Console
    $BackupRepository = "REP001" #For Metadata Only - Needs to be neer to the source side
    $CreateJobName = $NamePrefix+$VM
    $DRCluster = "" #Leave empty if you do not have one
    $DRHost = "esx"

    $RetainCyclesadd = "3" # Restore Points
    $compression = "5"  #0-none, 4–“dedupe-friendly”,5–“optimal”,6–“High”,9–"Extreme"
    $blocksize = "kbBlockSize512" #default and recommended is kbBlockSize512 (LAN), kbBlockSize256 (WAN), kbBlockSize1024 (LAN), kbBlockSize8192 (LAN 16TB+)


    $VSSDefaultUsername = "veeam.svc"
    $SetResultsToVmNotesadd = "True" #Write Results to VM description/custom field
    $VmAttributeNameadd = "BackupNotes" #Write Results to VM description/custom field

    #Scheduler
    $EnableScheduler = "True" #True/False
    $BackupWindow = "False" #True/False Timframe in which the Replicas not run.
    $RetrySpecifiedAdd = "True" #True/False
    $RetryTimesAdd = "5" #If job fails it the amount of times to do it again
    $RetryTimeoutAdd = "10" # time in minutes between the retries
    $OptionsDailyAddKind = "SelectedDays" #Everyday/SelectedDays/WeekDays
    $OptionsDailyAddDays = "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday", "Saturday", "Sunday"
    $OptionsDailyAddTime = "20:00:00"
    #Attention: If you set a time earlier than now, the job will start at the next selected Weekday

    ############################################################################################################
    # Hardcoded Settings (do not change => Other settings than these are not considered in the code)
    $Algorithmadd = "Syntethic" #Syntethic means ReverseIncremental
    $SourceProxyAutoDetectadd = "True" #Automatic Proxy Selection
    $schedulemode = "Daily" #Daily/Monthly/Periodically/Continuous
    $MaximumErrorCount = 1000
    $VSSEnabledadd = "True" #True => Development switch
    $Template_JobName = "<Template_Job>"
    $SQLServer = "backup\VEEAMSQL2008R2" #Needed for $MapReplica
    $SQLDB = "VeeamBackup"




    #############################################################################################################
    # 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 Starttime
    $StartTime = get-date -format u
    write-host $StartTime "Information: Start time:" $StartTime



    #Loads Veeam Powershell Snapin
    Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
    $ActualDate = get-date -format u
    write-host $ActualDate "Information: Veeam Powershell plugin loaded"

    #Reset Error Counter
    $Resultcounter = 0
    $error.clear()
    $ActualDate = get-date -format u
    write-host $ActualDate "Information: Error counter reset"



    #Check if jobname is empty
    # =>IF 2<=
    if  ($CreateJobName -eq "") {
    $resultcounter++
    $ActualDate = get-date -format u
    Write-host $ActualDate "Error: Jobname is empty."

    # =>IF 2<=
    } ELSE {
    Write-host $ActualDate "Information: Jobname not empty."

    #Check if jobname exists
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    # =>IF 1<=
    if  ($JobnameObject -eq $Null)  {

    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Jobname does not exist. Jobname: " $CreateJobName
    Write-host $ActualDate "Information: Starting job creation for: " $CreateJobName

    #Check if VCenter/ESX exists
    $vbrserverVC = Get-VBRServer | where {$_.Name -eq $VCenter}
    # =>IF 9<=
    if  ($vbrserverVC -eq $Null)  {
    $resultcounter++
    $ActualDate = get-date -format u
    Write-host $ActualDate "Error: vcenter/ESX name not correct. vcenter/ESX: " $VCenter
    # =>IF 9<=
    } Else {
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: vcenter/ESX name accepted. vcenter/ESX: " $VCenter

    #Check if VM exists
    $vbrobjects = Find-VBRViEntity -Server $vbrserverVC -Name $VM
    # =>IF 10<=
    if  ($vbrobjects -eq $Null)  {
    $resultcounter++
    $ActualDate = get-date -format u
    Write-host $ActualDate "Error: VM does not exist. VM Name: " $VM
    # =>IF 10<=
    } Else {
    $ActualDate = get-date -format u
    Write-host $ActualDate  "Information: VM exists. VM Name: " $VM

    #Create job

    $ActualDate = get-date -format u
    Write-host $ActualDate  "Information: Create job " $CreateJobName " please wait..."

    $CreateBackupRepository = Get-VBRBackupRepository -Name $BackupRepository
    $CreateJobVM = Find-VBRViEntity -Server $vbrserverVC -Name $VM
    $CreateHost = Get-VBRServer -Type ESXi -Name $DRHost
    $CreateResourcePool = Find-VBRViResourcePool -Server $vbrserverVC -Name $ResourcePool
    $CreateDatastore = Find-VBRViDatastore -Server $vbrserverVC -Name $Datastore
    if ($Folder -ne "") {
   
    $CreateFolder = Find-VBRViFolder -Server $vbrserverVC -Name $Folder
     $ActualDate = get-date -format u
    Write-host $ActualDate "Foldername is" $CreateFolder
    Add-VBRViReplicaJob -Name $CreateJobName -Server $CreateHost -Entity $CreateJobVM -Datastore $CreateDatastore -ResourcePool $CreateResourcePool -Folder $CreateFolder -Suffix $VMSuffix -Description $CreateJobName -BackupRepository $CreateBackupRepository
    
    } ELSE {
  
    Add-VBRViReplicaJob -Name $CreateJobName -Server $CreateHost -Entity $CreateJobVM -Datastore $CreateDatastore -ResourcePool $CreateResourcePool  -Suffix $VMSuffix -Description $CreateJobName -BackupRepository $CreateBackupRepository
    
    }
 
    
    
    #Check if job name exists
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    # =>IF 11<=
    if  ($JobnameObject -eq $Null)  {
    $resultcounter++
    $ActualDate = get-date -format u
    Write-host $ActualDate "Error: Job was not created. Jobname: " $CreateJobName
    # =>IF 11<=
    } Else {
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Job" $CreateJobName "succesfully created."


    #Set Job Options
    $JobnameObjectOptions = $JobnameObject.GetOptions()
    $JobnameObjectOptions.BackupStorageOptions.RetainCycles = $RetainCyclesadd
    $JobnameObject.setoptions($JobnameObjectOptions)
    Get-VBRJob | ?{$_.Name -eq $CreateJobName} | Set-VBRJobAdvancedStorageOptions -CompressionLevel $compression -StorageBlockSize $blocksize
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Job options set"
   
    #Point Job at Cluster if necessary
    if ($DRCluster -ne "") {
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    $Cluster = Find-VBRViEntity -Server $vbrserverVC -HostsAndClusters -name $DRCluster
    $OptionsToSet = $JobnameObject.Options
    $OptionsToSet.ViReplicaTargetOptions.ClusterName = $Cluster.Name
    $OptionsToSet.ViReplicaTargetOptions.ClusterReference = $Cluster.Reference
    $OptionsToSet.ViReplicaTargetOptions.HostReference = ""
    $JobnameObject.SetOptions($OptionsToSet) 
    }
   
    #Remap Networks if Necessary
    if ($RemapNetworks -eq "TRUE") {
    $template_job = Get-VBRJob –Name $Template_JobName
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    $JobnameObject.Options.ViNetworkMappingOptions.NetworkMapping = $template_job.Options.ViNetworkMappingOptions.NetworkMapping
    $JobnameObject.Options.ViReplicaTargetOptions.UseNetworkMapping = $true
    $JobnameObject.SetOptions($JobnameObject.Options)
    }
   
    #Setup Replica Seeding if Necessary
    if ($ReplicaSeeding -eq "TRUE") {
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    $Options = $JobnameObject.GetOptions()
    $Options.ViReplicaTargetOptions.InitialSeeding = $True
    $Options.ViReplicaTargetOptions.UseVmMapping = $True
    $JobnameObject.SetOptions($Options) 
    }
   
    #Map Replica to DR VM if Necessary
    if ($MapReplica -eq "TRUE") {

   $JobName = $CreateJobName  # Name of Job to Update
   $DBServer = $SQLServer # Name of SQL Server
   $DBName = $SQLDB # Name of SQL Database
   $SourceVMName = $VM # Name of source VM
   $TargetVMName = $VM+$VMSuffix # Name of target VM to map
   # Get vCenter Object
   $VCServer = $vbrserverVC
   # Get the Job and the Job ID
   $ReplJob = Get-VBRJob -Name $CreateJobName
   $ReplJobId = $ReplJob.Id
   # Get Source VM ID
   $SourceVMId = (Get-VBRJobObject -Job $CreateJobName -Name $SourceVMName).Info.ObjectId
   # Get Target VM ID
   Add-VBRViJobObject -Job $ReplJob -Entities (Find-VBRViEntity -Server $vbrserverVC -Name $TargetVMName) | Out-Null
   $TargetVMObj = Get-VBRJobObject -Job $ReplJob -Name $TargetVMName
   $TargetVMId = $TargetVMObj.Info.ObjectId
   $TargetVMObj.Delete()
   # Make a new ID for the row
   $RowID = [guid]::NewGuid()
   # Setup SQL server connection
   $SqlConn = New-Object System.Data.SqlClient.SqlConnection
   $SqlConn.ConnectionString = "Server=$DBServer;Database=$DBName;Integrated Security=True"
   $SqlConn.Open()
   $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
   $SqlCmd.Connection = $SqlConn
   # Call the stored procedure to create the new mapping (this does all the proper stuff like updating USN, etc)
   $SqlCmd.CommandText = "EXEC dbo.CreateReplicaMapping @row_id='$RowID', @row_job_id='$ReplJobId', @row_obj_id='$SourceVMId', @row_mapped_obj_id='$TargetVMId'"
   $SqlCmd.ExecuteScalar()
   # Close the SQL Connection
   $SqlConn.Close()
   
    }

    # Set Scheduling Options
    $JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    $JobnameObjectSchedule = $JobnameObject | Get-VBRjobscheduleoptions
    If ($RetrySpecifiedAdd -eq "True"){
    $JobnameObjectSchedule.RetrySpecified = $true
    } ELSE {
    $JobnameObjectSchedule.RetrySpecified = $false
    }
    $JobnameObjectSchedule.RetryTimes = $RetryTimesAdd 
    $JobnameObjectSchedule.RetryTimeout = $RetryTimeoutAdd
    If ($schedulemode -eq "Daily"){
    $JobnameObjectSchedule.OptionsDaily.Enabled = $true
    $JobnameObjectSchedule.OptionsMonthly.Enabled = $false
    $JobnameObjectSchedule.OptionsPeriodically.Enabled = $false
    $JobnameObjectSchedule.OptionsContinuous.Enabled = $false
    } ELSE {
    If ($schedulemode -eq "Monthly"){
    $JobnameObjectSchedule.OptionsDaily.Enabled = $false
    $JobnameObjectSchedule.OptionsMonthly.Enabled = $true
    $JobnameObjectSchedule.OptionsPeriodically.Enabled = $false
    $JobnameObjectSchedule.OptionsContinuous.Enabled = $false
    } ELSE {
    If ($schedulemode -eq "Periodically"){
    $JobnameObjectSchedule.OptionsDaily.Enabled = $false
    $JobnameObjectSchedule.OptionsMonthly.Enabled = $false
    $JobnameObjectSchedule.OptionsPeriodically.Enabled = $true
    $JobnameObjectSchedule.OptionsContinuous.Enabled = $false
    } ELSE {
    If ($schedulemode -eq "Continuous"){
    $JobnameObjectSchedule.OptionsDaily.Enabled = $false
    $JobnameObjectSchedule.OptionsMonthly.Enabled = $false
    $JobnameObjectSchedule.OptionsPeriodically.Enabled = $false
    $JobnameObjectSchedule.OptionsContinuous.Enabled = $true
    } ELSE {
    $ActualDate = get-date -format u
    write-host $ActualDate "Error: Schedulemode entry not correct"
    }}}}

    $JobnameObjectSchedule.OptionsDaily.Kind = $OptionsDailyAddKind
    $JobnameObjectSchedule.OptionsDaily.Days = $OptionsDailyAddDays
    $JobnameObjectSchedule.OptionsDaily.Time = $OptionsDailyAddTime
    $JobnameObjectSchedule.NextRun = $OptionsDailyAddTime
    $JobnameObjectSchedule.StartDateTime = $OptionsDailyAddTime
    if ($BackupWindow -eq "True"){
    $JobnameObjectSchedule.OptionsBackupWindow.Isenabled = $True
    $JobnameObjectSchedule.OptionsBackupWindow.BackupWindow = "<scheduler><Sunday>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</Sunday><Monday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Monday><Tuesday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Tuesday><Wednesday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Wednesday><Thursday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Thursday><Friday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0</Friday><Saturday>0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0</Saturday></scheduler>"
    }
    Set-VBRJobScheduleOptions -Job $JobnameObject -Options $JobnameObjectSchedule


    #$JobnameObject = Get-VBRJob | Where {$_.Name -eq $CreateJobName}
    If ($EnableScheduler -eq "True"){
    $JobnameObject.options.JobOptions.RunManually = $false
    $JobnameObject.EnableScheduler()
    } ELSE {
    $JobnameObject.options.JobOptions.RunManually = $true
    $JobnameObject.DisableScheduler()
    }

    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Job scheduling options set"

    # =>IF 41<=
    If ($VSSEnabled -eq "True") {
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS is enabled"

    #Enable VSS
    $JobnameObjectOptionsVSS = $JobnameObject.GetVSSOptions()
    $JobnameObjectOptionsVSS.Enabled = $VSSEnabledadd
    $JobnameObject.setVSSoptions($JobnameObjectOptionsVSS)
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS Enabled"   
   
    #Set VSS Default credentials
    $jobnameobject = Get-VBRJob -Name $CreateJobName
    $CredentialsVSSDefault = Get-VBRCredentials -name "*$VSSDefaultUsername*"
    $jobnameobject | Set-VBRJobVssOptions -Credentials $CredentialsVSSDefault
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS Default Options set"
 
    #Set VSS VM options
    $VMLoaded = Get-VBRJob -Name $CreateJobName | Get-VBRJobObject -Name $VM
    $oVSS = New-VBRJobVssOptions
    $oVSS.Enabled=$true
    $oVSS.GuestFSIndexingType="None"
    $oVSS.TransactionLogsTruncation="Never"
    Set-VBRJobObjectVssOptions -object $VMLoaded -options $oVSS

    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS VM credentials set"


    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Finished with job options"

    # =>IF 41<=
    } ELSE {
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: VSS is disabled"
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Finished with job options"
    # =>IF 41<=
    }

    #Disable Job if necessary
    if ($DisableJob -eq "TRUE") {
       Get-VBRJob -Name $CreateJobName | Disable-VBRJob
   $ActualDate = get-date -format u
   Write-host $ActualDate "Information: Job $CreateJobName Has Been Disabled"
    }

    # =>IF 11<=
    }
    # =>IF 10<=
    }
    # =>IF 9<=
    }


    # =>IF 1<=
    } Else {

    # =>IF 1<=
    }
    # =>IF 2<=
    }









    $Resultcounterend = $Resultcounter + $error.count

    #Results
    IF ($Resultcounterend -lt 1)
    {
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    $ActualDate = get-date -format u
    Write-host $ActualDate "Information: Job started at " $StartTime
    Write-host $ActualDate "Information: Job finished at " $ActualDate
    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: Job started at " $StartTime
    Write-host $ActualDate "Information: Job 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




}
Remove-PSSession $session
$actualtime = get-date
$actualtimeformated =$actualtime.ToUniversalTime()
Write-host $actualtime "Information: Job finished"

DMenzel
Lurker
Posts: 1
Liked: 2 times
Joined: Aug 05, 2014 2:56 pm
Full Name: Dirk Menzel
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by DMenzel » Sep 22, 2014 11:41 am 2 people like this post

Hi,

don't know if it is still of interest. But here is my workaround.
First I create the job using a esxhost as target:

Code: Select all

 Add-VBRViReplicaJob -Name $JobName -Server $esxhost  -Datastore $Datastore  -Entity $Entity -Suffix "_Replica" -Description $JobName
And then when setting the other options .. i change the ViReplicatTargetOptions

Code: Select all

    write-host "setting Backup-Job Options..."
    $Job = Get-VBRJob | where {$_.name -eq $JobName}
    $JobOptions = Get-VBRJobOptions $Job
    $Cluster = Find-VBRViEntity -Name $DsHost                                # Need Reference for $DsHost="ClusterWAD"
    $jobOptions.ViReplicaTargetOptions.ClusterReference = $Cluster.Reference
    $jobOptions.ViReplicaTargetOptions.ClusterName = $DsHost	   # "ClusterWAD"
    $jobOptions.ViReplicaTargetOptions.hostreference=""                 # empty hostReference
Hope anyone still needs that.

Andreas Neufert
Veeam Software
Posts: 3747
Liked: 665 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: Need a little help with Add-VBRViReplicaJob

Post by Andreas Neufert » Sep 22, 2014 12:13 pm

Thanks for this feedback and workaround. Setting an ESXi host for job creation and later change to cluster is the actual needed way to go.
The ESXi host need to be a member of the cluster as well.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests