Need a little help with Add-VBRViReplicaJob

PowerShell script exchange

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby electricd7 » Thu 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: 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 » Thu 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?
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 7:17 pm

Did you verify that the $ListOfObjects is actually populated?
tsightler
Veeam Software
 
Posts: 4772
Liked: 1739 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 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: 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 » Thu 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.
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 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
Veeam Software
 
Posts: 4772
Liked: 1739 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 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()
tsightler
Veeam Software
 
Posts: 4772
Liked: 1739 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 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

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

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby Andreas Neufert » Mon 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"
Andreas Neufert
Veeam Software
 
Posts: 2201
Liked: 360 times
Joined: Wed May 04, 2011 8:36 am
Location: Germany
Full Name: @AndyandtheVMs Veeam PM

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby DMenzel » Mon 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.
DMenzel
Lurker
 
Posts: 1
Liked: 2 times
Joined: Tue Aug 05, 2014 2:56 pm
Full Name: Dirk Menzel

Re: Need a little help with Add-VBRViReplicaJob

Veeam Logoby Andreas Neufert » Mon 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.
Andreas Neufert
Veeam Software
 
Posts: 2201
Liked: 360 times
Joined: Wed May 04, 2011 8:36 am
Location: Germany
Full Name: @AndyandtheVMs Veeam PM

Previous

Return to PowerShell



Who is online

Users browsing this forum: robbysmith and 7 guests