Need a little help with Add-VBRViReplicaJob

PowerShell script exchange

Need a little help with Add-VBRViReplicaJob

Veeam Logoby electricd7 » Tue 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: 103
Liked: 5 times
Joined: Tue Mar 27, 2012 10:13 pm
Full Name: Chad Killion

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby electricd7 » Tue 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

electricd7
Expert
 
Posts: 103
Liked: 5 times
Joined: Tue Mar 27, 2012 10:13 pm
Full Name: Chad Killion

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby tsightler » Tue 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.
tsightler
Veeam Software
 
Posts: 4768
Liked: 1737 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby electricd7 » Tue 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... :)
electricd7
Expert
 
Posts: 103
Liked: 5 times
Joined: Tue Mar 27, 2012 10:13 pm
Full Name: Chad Killion

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby v.Eremin » Wed 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.
v.Eremin
Veeam Software
 
Posts: 13255
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby electricd7 » Wed 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.
electricd7
Expert
 
Posts: 103
Liked: 5 times
Joined: Tue Mar 27, 2012 10:13 pm
Full Name: Chad Killion

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby v.Eremin » Wed 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.
v.Eremin
Veeam Software
 
Posts: 13255
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby electricd7 » Wed 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!
electricd7
Expert
 
Posts: 103
Liked: 5 times
Joined: Tue Mar 27, 2012 10:13 pm
Full Name: Chad Killion

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby tsightler » Wed 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.
tsightler
Veeam Software
 
Posts: 4768
Liked: 1737 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby electricd7 » Thu 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.
electricd7
Expert
 
Posts: 103
Liked: 5 times
Joined: Tue Mar 27, 2012 10:13 pm
Full Name: Chad Killion

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby v.Eremin » Thu 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.
v.Eremin
Veeam Software
 
Posts: 13255
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby electricd7 » Thu 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]
electricd7
Expert
 
Posts: 103
Liked: 5 times
Joined: Tue Mar 27, 2012 10:13 pm
Full Name: Chad Killion

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby tsightler » Thu 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
Veeam Software
 
Posts: 4768
Liked: 1737 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby tsightler » Thu 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
Veeam Software
 
Posts: 4768
Liked: 1737 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby tsightler » Thu 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)
tsightler
Veeam Software
 
Posts: 4768
Liked: 1737 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Next

Return to PowerShell



Who is online

Users browsing this forum: edirschedl, quazer and 5 guests