Powershell - Exclude Teams object(s) from VBO Backup Job


Userlevel 2
Badge

Last year Veeam Backup for Microsoft Office 365 v5 added purpose-built backup and recovery for Microsoft Teams. Since then many customer and partners asked me "How can we protect all of our teams and exclude specific teams objects based on names automatically?".

As there is no filter logic within the UI for the Backup Job object exclusion list, I solved this "problem" using the script down below. First we store the credentials (encrypted) in a txt file:

$credential = Get-Credential
$credential.Password | ConvertFrom-SecureString | Set-Content <path to secure.txt>

And here's the script. Just change the values for the following variables: 

$organizationname Name of the tenant - Only needed if more than one is configured
$backupjob            Name of existing the Backup Job
$filter                    Name of the team that needs to be excluded. Regular expressions can be used
$userName            Username for retrieving the existing Teams in M365
$passwordText       Path to the secure.txt file that has been created in advance

#Some Variables
# $organizationname = "<tenant>"
$backupjob = "MS Teams Backup"
$org = Get-VBOOrganization #-Name $organizationname
$job = Get-VBOJob -Name $backupjob
$filter = "Test"

# Credentials
$userName = "<yourusername@here.com>"
$passwordText = Get-Content "<path to secure.txt>"

# Convert to secure string
$securePwd = $passwordText | ConvertTo-SecureString

# Create credential object
$credObject = New-Object System.Management.Automation.PSCredential -ArgumentList $userName, $securePwd

Import-Module MicrosoftTeams
Import-Module "C:\Program Files\Veeam\Backup365\Veeam.Archiver.PowerShell\Veeam.Archiver.PowerShell.psd1"

Connect-MicrosoftTeams -Credential $credObject

$Teams = (Get-Team |Select GroupId, DisplayName)
ForEach ($T in $Teams) {
$TeamName = (Get-UnifiedGroup -Identity $T.DisplayName | Select -ExpandProperty DisplayName)
$Team = Get-VBOOrganizationTeam -Organization $org -DisplayName $TeamName
$FilteredTeam = $Team.DisplayName -match "$filter"
if ($FilteredTeam) {
$exclTeam = New-VBOBackupItem -Team $Team
Add-VBOExcludedBackupItem -Job $job -BackupItem $exclTeam
}
}
Write-Host "Excluded a total of " -NoNewline
Write-Host $newSite.count -NoNewline -ForegroundColor Green
Write-Host " Team Site(s) in the Backup Job " -NoNewline
Write-Host $backupjob -ForegroundColor Green

Notes: - The MS Teams Powershell module is needed
          - Object type in Backup Job is Teams


3 comments

Userlevel 1
Badge

You can add the following to your script to check for the module.
 

# Do not change below unless you know what you are doing
if ((Get-InstalledModule -Name "MicrosoftTeams" -ErrorAction SilentlyContinue) -eq $null) {
Install-Module MicrosoftTeams
}

 

Userlevel 6
Badge +2

Great stuff Steve and a nice addition Niel

Userlevel 7
Badge +3

Nice script @SteveHeart 

And your addition @nielsengelen is great, too.

With this I will build a query to distinguish the V10 powershell plugin and the V11 powershell module and load the correct one in my scripts… Did not look for this up to now, but your snippet should do the job with some modification.

Comment