Custom Attribute

PowerShell script exchange

Custom Attribute

Veeam Logoby Petrie » Wed Aug 28, 2013 9:54 am

Hi Andy,

a few months ago, a buddy sent me a link (viewtopic.php?f=26&t=13372) and told me,
there was a script that is able to fill VBR jobs with VMs basaed on their vSphere custom attribute ("Annotations").
However, I am just trying to implement this, - but I cannot find the script.

Could you please point me to the thread?
Thank your very much!

Regards,
Helge
Petrie
Enthusiast
 
Posts: 29
Liked: 1 time
Joined: Wed Oct 24, 2012 11:17 am
Full Name: Helge Hagedorn

Re: Getting Started and Code Examples

Veeam Logoby v.Eremin » Wed Aug 28, 2013 11:42 am

Hi, Helge. The quick search didn’t given anything, so, you might want to share a little bit information regarding what you’re trying to achieve and, probably, we will be able to help you. Thanks.
v.Eremin
Veeam Software
 
Posts: 13255
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Custom Attribute

Veeam Logoby Petrie » Wed Aug 28, 2013 12:15 pm

Hi Vladimir,

seems as if I was logged out while typing my reply. :cry:
It´s alway a good idea to copy the text prior to clicking on "submit". ;-)

I have a vSphere HA environment with two clsutered ESXi hosts and two VBR backup proxies.
In order to have some kind of loadbalancing I created two jobs (each running on one backup proxy) and backuped "host 1" and "host 2", - assuming this would backup the VMs running on host 1 and host 2.

While this worked like a charm I noticed the warnin messages for VMs that had been moved by DRS.
As I cannot use the vSphere folders as a backup criterium (they are used for other stuff) a buddy sent me the above mentioned link and told me there was a powershell script written by a Veeam employee that reads the vSphere annotations ("custom attribute") and sorts the VMs into the different (existing) jobs based upon the value.
E.g.: I created a custom attribute calles "veeam" and set it to "dmz_1" and "dmz_2".

But unfortunately I am not able to find this script, either.

Could you please give me advice what I can do?

Thanks a lot,
Helge
Petrie
Enthusiast
 
Posts: 29
Liked: 1 time
Joined: Wed Oct 24, 2012 11:17 am
Full Name: Helge Hagedorn

Re: Custom Attribute

Veeam Logoby v.Eremin » Wed Aug 28, 2013 1:59 pm 1 person likes this post

Then, something like this should answer your requirements:

Code: Select all
Add-PSSnapin VMware.VimAutomation.Core
asnp VeeamPssnapin
$Job1 = Get-vbrJob -name "Name of first Backup Job"  #Specify here the name of job to which VMs with "dmz_1" as custom attribute should be added
$Job2 = Get-vbrJob -name "Name of second Backup Job" #Specify here the name of job to which VM swith "dmz_2" as custom attribute should be added
$VcServer = Get-VBRServer -name "Name of vCenter"
Connect-VIServer -Server "Name of vCenter" -User "User" -Password "Password"
Foreach ($VM in Get-VM | where {$_.customfields.key -eq "Veeam"} )
{
   if ($VM.customfields.value -eq "dmz_1")
   {
     $Object = Find-VBRObject -Server $VCServer -name $VM.name
     Add-VBRJobObject -job $Job1 -Server $VcServer -Entities $Object
   }
   if ($VM.customfields.value -eq "dmz_2")
   {
     $Object = Find-VBRObject -Server $VCServer -name $VM.name
     Add-VBRJobObject -job $Job2 -Server $VcServer -Entities $Object
   }
}


As always, don’t forget to test this script before implementing.

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

Sorry!

Veeam Logoby Petrie » Tue Nov 19, 2013 4:35 pm

Hi Vladimir,

it seems that I have missed the email notification about your reply. I just saw it when opening another topic a few moments ago.
I am very sorry about that, and I really appreciate your help! :-(

I´m going to test it as soon as possible!

Thanks again and regards,
Helge
Petrie
Enthusiast
 
Posts: 29
Liked: 1 time
Joined: Wed Oct 24, 2012 11:17 am
Full Name: Helge Hagedorn

Re: Custom Attribute

Veeam Logoby v.Eremin » Tue Nov 19, 2013 4:38 pm

Not a problem. :) Let me know if the proposed script works for you. Thanks.
v.Eremin
Veeam Software
 
Posts: 13255
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Custom Attribute

Veeam Logoby tsightler » Tue Nov 19, 2013 5:54 pm

Here's a script I wrote for a customer some time ago, it actually creates a new backup job if the job doesn't already exist, adds VMs to an existing job, and removes VMs from jobs if the attribute value is deleted, etc. It's a little ugly as my Powershell skills where just evolving at that point but it seemed to get the job done. It probably needs to be updated for V7 as it doesn't use the credentials manager, but it might be a place to start. I'm actually working on a significantly more sophisticated versions that allows the implementation of a policy based backup scheme using tags.

Code: Select all
if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
   Add-PSSnapin VMware.VimAutomation.Core
   }

if (-not (Get-PSSnapin VeeamPSSnapIn -ErrorAction SilentlyContinue)) {
   Add-PSSnapin VeeamPSSnapIn
   }

$username = "Domain\User" # Username for VSS
$password = "Password"  # Password for VSS
$Credentials = New-Object -TypeName Veeam.Backup.Common.CCredentials -ArgumentList $username,$password,0,0
$repository = "<Repository Name>" # The repository used by jobs created by this script
$VCServer = "<vCenter Server>" # vCenter Server for VMs managed by this script
$JobPrefix = "VeeamJob-" # This prefix will prepended to the Job Name for all jobs created by this script

Connect-VIServer $VCServer -AllLinked

# Get All Veeam Jobs
$VeeamJobs = Get-VBRJob -Name "$JobPrefix*"
# Get Veeam vCenter Server
$VeeamVCServer = Get-VBRServer -Name $VCServer

# Build VeeamJob Attribute from All VMs
$VMAttReport = @()
$VMAttJobs = @()
Get-VM -Server $VCServer | ForEach-Object {
   $Annotations = $_ | Get-Annotation -CustomAttribute VeeamJob
   $VMAttributes = "" | select VeeamJob, VMName
        if ($Annotations.Value -ne "") {
          $VMAttributes.VeeamJob = $JobPrefix + $Annotations.Value
        $VMAttributes.VMName = $_.Name
        $VMAttReport += $VMAttributes
          $VMAttJobs += $JobPrefix + $Annotations.Value
        }
}
# Sort Attribute List by VeeamJob
$VMAttReport = $VMAttReport.GetEnumerator() | Sort-Object VeeamJob
# Create list of unique job names
$VMAttJobs = $VMAttJobs | Sort-Object -unique

# Loop through each job from attribute list
foreach ($VMJob in $VMAttJobs) {
    # Check if job already exist
    if ($VeeamJobs | Where {$_.Name -eq $VMJob}) {
        $VeeamJobExist = $true
        $CurrentVeeamJob = $VeeamJobs | Where {$_.Name -eq $VMJob}
        $VeeamJobObjects = Get-VBRJobObject -job $CurrentVeeamJob
    }
    else {
        # Set flag for new job creation
        $VeeamJobExist = $false
    }
    # Loop through each VM that matches current VeeamJob attribute
    foreach ($VM in $VMAttReport | Where {$_.VeeamJob -eq $VMJob}) {
        if ($VeeamJobExist) {
            # If VM is not currently in job, add it, set VSS Options
            if (!($VeeamJobObjects | Where {$_.Name -eq $VM.VMname})) {
                Add-VBRJobObject -Job $CurrentVeeamJob -Server $VeeamVCserver -Objects $VM.VMName
                # Set Job Object VSS Properties
                $NewVeeamJobObjects = Get-VBRJobObject -job $CurrentVeeamJob
                foreach ($object in $NewveeamJobObjects){
                    $VSSJobObjectOptions = $object.GetVssOptions()
                    $VSSJobObjectOptions.Enabled = $true
                    $VSSJobObjectOptions.IgnoreErrors = $true
                    $VSSJobObjectOptions.GuestFSIndexingType = "None"
                    $object.SetVssOptions($VSSJobObjectOptions)
                }
 
            }
        }
        else {
            # If job does not exist create it and add first VM
            Add-VBRViBackupJob `
            -Name $VMjob `
            -BackupRepository (Get-VBRBackupRepository -Name $repository) `
            -Entity (Find-VBRViEntity -Name $VM.VMName)

            # Get the new job
            $NewVeeamJob = Get-VBRJob -Name $VMjob

            # Set new job properties
            $options = $NewVeeamJob.GetOptions()
            $options.BackupStorageOptions.RetainCycles = 30
            $options.BackupStorageOptions.CompressionLevel = 5
            $options.BackupTargetOptions.TransformFullToSyntethic = $false
            $options.BackupStorageOptions.EnableFullBackup = $true
            $options.VIsourceoptions.SetResultsToVmNotes = $true
            $NewVeeamJob.SetOptions($options)

            # Set Job VSS Properties
            $VSSJobOptions = $NewVeeamJob.GetVssOptions()
            $VSSJobOptions.Enabled = $true
            $VSSJobOptions.IgnoreErrors = $true
            $VSSJobOptions.GuestFSIndexingType = "None"
            $VSSJobOptions.Credentials = $Credentials
            $NewVeeamJob.SetVssOptions($VSSJobOptions)
      
            # Set Job Object VSS Properties
            $NewVeeamJobObjects = Get-VBRJobObject -job $NewVeeamJob
            foreach ($object in $NewveeamJobObjects){
                $VSSJobObjectOptions = $object.GetVssOptions()
                $VSSJobObjectOptions.Enabled = $true
                $VSSJobObjectOptions.IgnoreErrors = $true
                $VSSJobObjectOptions.GuestFSIndexingType = "None"
                $object.SetVssOptions($VSSJobObjectOptions)
            }
           
            # Rebuild $VeeamJobs with all jobs including new
            $VeeamJobs = Get-VBRJob -Name "$JobPrefix*"
            $CurrentVeeamJob = $VeeamJobs | Where {$_.Name -eq $VMJob}
            $VeeamJobObjects = Get-VBRJobObject -job $CurrentVeeamJob
            # Set Job Exist flag now that job has been created
            $VeeamJobExist = $true
        }
    }
    # Loop through VMs in each Veeam Job to look for removed VMs
    foreach ($VeeamJobObject in $VeeamJobObjects) {
        #If VM exist in job but there is no corresponding attribute, remove it from job
        if (!( $VMAttReport | Where {$_.VMName -eq $VeeamJobObject.Name} | Where {$_.VeeamJob -eq $VMJob})) {
            $DeleteVMfromJob = Get-VBRJob -Name $VMJob
            $DeleteVMfromJob.GetObjectsInJob()
            ($DeleteVMfromJob.GetObjectsinJob() | ?{$_.name -eq $VeeamJobObject.Name}).Delete()
        }
    }
}
tsightler
Veeam Software
 
Posts: 4767
Liked: 1737 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler


Return to PowerShell



Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests