Skip to content

Instantly share code, notes, and snippets.

@tdewin
Last active November 29, 2017 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdewin/8b787096380467aa1dc1fe8a7732ba17 to your computer and use it in GitHub Desktop.
Save tdewin/8b787096380467aa1dc1fe8a7732ba17 to your computer and use it in GitHub Desktop.
Add Users from an OU to VBO365
## variables to set
$ad = "mydomain.com"
$cred = Get-Credential -UserName "administrator" -Message "AD"
$ouname = "My OU"
$orgname = "exchange.mydomain.com"
$reponame = "Default Backup Repository"
##
function New-VBOOUJob {
[cmdletbinding()]
param(
[Parameter(Mandatory=$true)][string]$ad,
[Parameter(Mandatory=$true)][System.Management.Automation.PSCredential]$cred,
[Parameter(Mandatory=$true)][string]$ouname,
[Parameter(Mandatory=$true)][string]$orgname,
[Parameter(Mandatory=$true)][string]$reponame
)
if ($(Get-Module -Name activedirectory) -eq $null) {
Import-Module activedirectory
write-verbose "Loading Active Directory"
}
if ($(Get-Module -Name Veeam.Archiver.PowerShell) -eq $null) {
$installpath = Get-ItemPropertyValue -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Veeam.Archiver.Service' -Name ImagePath -ErrorAction SilentlyContinue
if ($installpath -ne $null) {
$modulepath = Join-Path -Path (split-Path -Parent ($installpath -replace '"', "")) -ChildPath "Veeam.Archiver.PowerShell"
write-verbose "Loading VBO module from : $modulepath"
Import-module $modulepath
}
}
$ou = Get-ADOrganizationalUnit -Server $ad -Credential $cred -Filter { name -eq $ouname }
$users = Get-ADUser -Server $ad -Credential $cred -LDAPFilter "(name=*)" -SearchBase $ou.DistinguishedName -SearchScope OneLevel
$emaillist = $users | % { ("{0}@x.local" -f $_.SamAccountName) }
Write-Verbose "User list:"
write-verbose "$emaillist"
$org = Get-VBOOrganization -Name $orgname
$rep = Get-VBORepository -Name $reponame
$nam = $ou.DistinguishedName
$mbx = $org | Get-VBOOrganizationMailbox | ? { $_.email -in $emaillist }
$job = get-vbojob -name $nam
$result = $null
if ($job -eq $null) {
Write-Verbose "Making new job $nam"
$sch = New-VBOJobSchedulePolicy -PeriodicallyEvery Hours1 -Type Periodically
$result = Add-VBOJob -Organization $org -Name $nam -Repository $rep -SelectedMailboxes $mbx -SchedulePolicy $sch
} else {
Write-Verbose "Updating job $nam"
$result = Set-VBOJob -Job $job -SelectedMailboxes $mbx
}
if ($result -ne $null) {
write-verbose "Done"
}
}
New-VBOOUJob -ad $ad -cred $cred -ouname $ouname -reponame $reponame -orgname $orgname -Verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment