Add-TenantProxyAddress.ps1
<#PSScriptInfo
.VERSION 2.0 .GUID 614d2139-f250-4c6a-923a-ca47802a5351 .DESCRIPTION Add Office 365 tenant proxy address for all mailboxes. .AUTHOR Aaron Guilmette .COMPANYNAME Microsoft .COPYRIGHT 2022 .TAGS Email Address Policy Template .LICENSEURI .PROJECTURI https://www.undocumented-features.com/2015/12/09/add-office-365-tenant-proxy-address-to-exchange-objects-with-email-address-policy-disabled/ .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# THIS CODE AND ANY ASSOCIATED INFORMATION ARE PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INABILITY TO USE, OR RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER. Author: Aaron Guilmette aaron.guilmette@microsoft.com #> <# .SYNOPSIS Use this script to add the tenant.mail.onmicrosoft.com proxy address to mailboxes without the email address policy applied. .PARAMETER Tenant Specifies the tenant name. For example, in contoso.onmicrosoft.com, 'contoso' is the tenant name. .PARAMETER User Optionally specifies a user. .PARAMETER Logfile Optionally specifies a logfile. .PARAMETER Searchbase Optionally specifies Active Directory OU. .PARAMETER Confirm Confirms changes to be made. .EXAMPLE .\Add-TenantProxyAddress -tenant contoso -confirm Adds proxy address "@contoso.mail.onmicrosoft.com" to all user objects in the AD forest. .EXAMPLE .\Add-TenantProxyAddress -tenant contoso -user joe.smith@contoso.com -confirm Adds proxy address "@contoso.mail.onmicrosoft.com" to user mailbox 'joe.smith@contoso.com' .LINK https://gallery.technet.microsoft.com/Add-Office-365-Tenant-93391e4c #> [CmdletBinding()] Param( [Parameter(Mandatory=$true,HelpMessage='Enter Office 365 Tenant Name')] [string]$Tenant, [Parameter(Mandatory=$false,HelpMessage='Enter user name or leave blank for all matching users')] [string]$User, [Parameter(Mandatory=$false,HelpMessage='Enter log file name')] [string]$Logfile, [Parameter(Mandatory=$false,HelpMessage='Confirm')] [switch]$Confirm, [Parameter(Mandatory=$false,HelpMessage='Active Directory Search Base')] [string]$Searchbase = (Get-OrganizationalUnit -SingleNodeOnly | ? { $_.Type -eq "Domain" }).DistinguishedName ) # Strip Tenant Subdomain Name $tenant = $tenant.Replace(".mail.onmicrosoft.com","") $tenant = $tenant.Replace(".onmicrosoft.com","") Set-ADServerSettings -ViewEntireForest $True If (!($user)) { Write-Host -ForegroundColor Green "Processing all users without Email Address Policy applied." $mbx = Get-Mailbox -OrganizationalUnit $SearchBase -ResultSize Unlimited -Filter {(EmailAddresses -notlike "*$tenant.mail.onmicrosoft.com")} } Else { Write-Host -ForegroundColor Green "Processing $($user)." $mbx = Get-Mailbox -OrganizationalUnit $SearchBase -ResultSize Unlimited -Filter {(EmailAddresses -notlike "*$tenant.mail.onmicrosoft.com")} $user } If ($logfile) { $startDate = Get-Date $startDateData = "Started at $StartDate." $startDateData | Out-File $logfile -Append } ForEach ($mailbox in $mbx) { # create proxy address $addAlias = "smtp:"+$mailbox.alias+"@$tenant.mail.onmicrosoft.com" # Check if mailbox already has proxy If ($mailbox.EmailAddresses -notcontains $addAlias) { Write-Host -ForegroundColor Green "Adding $addAlias to $mailbox ..." $mailbox.EmailAddresses.Add($addAlias) If ($Confirm) { Set-Mailbox -Identity $mailbox.Identity -EmailAddresses $mailbox.EmailAddresses -ErrorAction SilentlyContinue If ($Logfile) { $data = "Added $addAlias to user account $mailbox." $data | Out-File $logfile -Append $data = $null } } Else { Write-Host -ForegroundColor Cyan " Mailbox $mailbox is in scope to be updated. Run using -Confirm switch to commit changes." If ($Logfile) { $data = "Mailbox $mailbox is in scope to be updated. Run using -Confirm switch to commit changes." $data | Out-File $Logfile -Append $data = $null } } } Else { Write-Host -ForegroundColor Cyan " Skipping because $addAlias is already present on object." If ($logfile) { $data = "Skipping $mailbox because $addAlias is already present." $data | Out-File $logfile -Append $data = $null } } } If ($logfile) { $endDate = Get-Date $endDateData = "Stopped at $endDate." $endDateData | Out-File $logfile -Append } |