Quantcast
Channel: Rob V IT
Viewing all articles
Browse latest Browse all 64

Add VM to VEEAM Backup job using PowerShell

$
0
0

I created a simple PowerShell function to add VM’s in a VEEAM backup job. This script has 2 options, enable indexing  and/or application aware processing per object.

There are many more possibilities to add functionality. For now, it fits our needs. If you have some feature requests, please comment below.

It can be used by VMWare and Hyper-v users; it checks the hypervisor in the beginning.
Run this script on a VEEAM VBR Server.

Together we can improve this PowerShell function, don’t hesitate to comment.

<#
.EXAMPLE
    First, import the function with the following command:
    C:\PS> . .\PS_AddVMJob

    Second
    C:\PS> ps_addvmjob -vmname NAME -Application y -Indexing y

.HYPERVISOR
    This script can be used by VMWare and Hyper-v users, it uses commands from both hypervisors

.VERION
    Tested with VEEAM B&R V8 and V9
    1.0

.NOTES
    Author: Rob Verhees
    Date created: 18-2-2016

    PowerShell beginner
#>


function PS_AddVMJob { 
  [CmdletBinding()]

      param (
          
        [parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [string]$vmname,
        
        [parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [ValidateSet("y","n")]
        [string]$application,
                       
        [parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [ValidateSet("y","n")]
        [string]$indexing
    )


Begin {    
If ((Get-PSSnapin -Name VeeamPSSnapin -ErrorAction SilentlyContinue) -eq $null) {add-pssnapin VeeamPSSnapin}

#File indexing
$Excludedfolders = @("%windir%","%ProgramFiles%","%ProgramFiles(x86)%","%ProgramW6432%","%TEMP%")

#Determine the Hypervisor type
$VBRServerType = Get-VBRServer | where{$_.type -match "esxi" -or $_.type -match "HvServer"} | Select -ExpandProperty Type -Last 1

}

Process{

#Get VBRJobs and create a menu
    $jobs = Get-VBRjob | where{$_.JobType -eq 'Backup'}

    $menu = @{}
            for ($i=1;$i -le $jobs.count; $i++) {
            Write-Host "$i. $($jobs[$i-1].name)"
            $menu.Add($i,($jobs[$i-1].name))
            }

                [int]$ans = Read-Host 'Kies back-up job:'
                $selection = $menu.Item($ans)
                $jobname = get-vbrjob -name $selection

#Add VM in VBRjob Hyper-V
    IF($VBRServerType -match "HvServer"){
        $newvm = Find-VBRHvEntity -name $vmname

    IF(!$newvm){
        Write-host "No vm found with the name: $vmname" -foreground "red"
        Return
    }

    Add-VBRHvJobObject -Job $jobname -Entities $newvm

    }


#Add VM in VBRjob VMWare
    ELSEIF($VBRServerType -match "ESX"){
        $newvm = Find-VBRViEntity -name $vmname

    IF(!$newvm){
        Write-host "No vm found with the name: $vmname" -foreground "red"
        Return
    }

    Add-VBRViJobObject -Job $jobname -Entities $newvm

    }


    ELSE{
        Write-host "VBRServerType $VBRServerType not recognized" -Foreground "red"
        Return
    }



#Get Job object
    $jobobject = $jobname | Get-VBRJobObject | where{$_.name -match $newvm.name}



#NOTE!! If the VBRJob VSS option is not enabled, the script enabales this. Default values for all objects in the veeam backup job will be enabled.
    IF($Application -match "y" -and $indexing -match "n"){

        $vssjoboptions = Get-VBRJobVssOptions -job $jobname.name

            IF($vssjoboptions.enabled -match "false"){
                $vssjoboptions.enabled = $true
                Set-VBRJobVssOptions -Options $vssjoboptions -Job $jobname
                Write-Host "NOTE!! The VBRJob VSS option is not enabled, the script enabales this. Default values for all objects in the veeam backup job will be enabled." -foreground Yellow
            }
        
                $vssobjectoptions = Get-VBRJobObjectVssOptions -ObjectInJob $jobobject
                $vssobjectoptions.enabled = $true
                $vssobjectoptions.IgnoreErrors = $true
                $vssobjectoptions.SqlBackupOptions.TransactionLogsProcessing = "TruncateonlyonSuccessJob"
                $vssobjectoptions.ExchangeBackupOptions.TransactionLogsProcessing = "TruncateonlyonSuccessJob"
                set-vbrjobobjectvssoptions -Options $vssobjectoptions -object $jobobject
            }

    IF($Application -match "y" -and $indexing -match "y"){

        $vssjoboptions = Get-VBRJobVssOptions -job $jobname.name

            IF($vssjoboptions.enabled -match "false"){
                $vssjoboptions.enabled = $true
                $vssjoboptions.GuestFSIndexingType = "ExceptSpecifiedFolders"
                Set-VBRJobVssOptions -Options $vssjoboptions -Job $jobname
                Write-Host "NOTE!! The VBRJob VSS option is not enabled, the script enabales this. Default values for all objects in the veeam backup job will be enabled." -foreground Yellow
             }

                $vssobjectoptions = Get-VBRJobObjectVssOptions -ObjectInJob $jobobject
                $vssobjectoptions.enabled = $true
                $vssobjectoptions.IgnoreErrors = $true
                $vssobjectoptions.SqlBackupOptions.TransactionLogsProcessing = "TruncateonlyonSuccessJob"
                $vssobjectoptions.ExchangeBackupOptions.TransactionLogsProcessing = "TruncateonlyonSuccessJob"
                $vssobjectoptions.GuestFSIndexingType = "ExceptSpecifiedFolders"
                $vssobjectoptions.ExcludedIndexingFolders = $Excludedfolders
                Set-vbrjobobjectvssoptions -Options $vssobjectoptions -object $jobobject
            }

    IF($Application -match "n" -and $indexing -match "y"){

        $vssjoboptions = Get-VBRJobVssOptions -job $jobname.name

            IF($vssjoboptions.GuestFSIndexingType -match "none"){
                $vssjoboptions.GuestFSIndexingType = "ExceptSpecifiedFolders"
                Set-VBRJobVssOptions -Options $vssjoboptions -Job $jobname
             }

                $vssobjectoptions = Get-VBRJobObjectVssOptions -ObjectInJob $jobobject
                $vssobjectoptions.GuestFSIndexingType = "ExceptSpecifiedFolders"
                $vssobjectoptions.ExcludedIndexingFolders = $Excludedfolders
                Set-vbrjobobjectvssoptions -Options $vssobjectoptions -object $jobobject
             }


    IF($Application -match "n" -and $indexing -match "n"){

            $vssobjectoptions = Get-VBRJobObjectVssOptions -ObjectInJob $jobobject
            $vssobjectoptions.GuestFSIndexingType = "none"
            $vssobjectoptions.Enabled = $false
            set-vbrjobobjectvssoptions -Options $vssobjectoptions -object $jobobject
        }

    }
}

 


Viewing all articles
Browse latest Browse all 64

Trending Articles