使用PowerShell將Microsoft 365別名地址更改為主要SMTP地址
該組織將使用新的主要SMTP地址。他們已經將新地址設置為所有郵箱的別名地址。現在,他們想將其與主要SMTP地址交換。如果還有其他別名地址,則需要將它們保持不變。在本文中,您將學習如何使用PowerShell將別名地址更改為Microsoft 365中的主要SMTP地址。
介紹
Set-M365primaryAddress.ps1 PowerShell腳本可用於在線交換(Microsoft 365)。如果您想執行相同的操作,但要進行本地交換或交換混合環境,請閱讀文章將別名可別名地址更改為PowerShell的主要SMTP地址。
筆記:別名SMTP地址成為主要SMTP地址,主要SMTP地址將成為別名地址。因此,主要的SMTP地址不會被刪除;它只是與別名SMTP地址交換。
開始之前
如果每個郵箱都沒有設置別名地址怎麼辦?最好的是通過PowerShell添加次級SMTP地址瀏覽文章。
一旦您驗證了為所有郵箱設置的別名地址,請從本文中運行SET-M365PRIMARYADDRESS.PS1 POWERSHELL腳本,以將別名地址與主SMTP地址交換。
假設您已經完成了,並且想刪除現在是別名地址的舊主SMTP地址,您可以使用PowerShell刪除secondary SMTP地址。
在一個階段進行此操作總是比立即完成所有操作的腳本要好,因此您確定所有內容都已正確設置。
下載set-m365primaryAddress.ps1 powershell腳本,或將代碼複製並粘貼到記事本中。給它名字set-m365primaryAddress.ps1並將其放在C:腳本文件夾。創建一個腳本文件夾,如果您沒有一個文件夾。
<#
.SYNOPSIS
Set-M365PrimaryAddress.ps1
.DESCRIPTION
Get the secondary (alias) address from a specified domain and set it as the primary SMTP address
for all mailbox users in Exchange Online (Microsoft 365). If there is no alias address set with
the specified domain, it will skip the user and display a message. The primary SMTP address will
become an alias address and all the secondary email address will remain.
.LINK
www.alitajran.com/change-microsoft-365-alias-address-to-primary-smtp-address/
.NOTES
Written by: ALI TAJRAN
Website: alitajran.com
X: x.com/alitajran
LinkedIn: linkedin.com/in/alitajran
.CHANGELOG
V1.00, 09/01/2024 - Initial version
#>
param (
[Parameter(Mandatory = $true)]
[string]$DomainName,
[switch]$WhatIf
)
# Output will be added to C:temp folder. Open the log with a text editor.
Start-Transcript -Path C:tempSet-M365Primary-SMTP-Address.log -Append
# Specify the domain to set as primary SMTP
$primarySMTPDomain = "@" + $DomainName
# Get all mailbox users
$users = Get-Mailbox -ResultSize Unlimited
foreach ($user in $users) {
$currentPrimarySMTP = $user.PrimarySmtpAddress.ToString()
$allEmailAddresses = $user.EmailAddresses
$aliasAddresses = $allEmailAddresses | Where-Object { $_ -clike "smtp:*$primarySMTPDomain" }
# Check if the current primary SMTP address ends with the specified domain
if ($currentPrimarySMTP -like "*$primarySMTPDomain") {
Write-Host "Skipping $($user.DisplayName) - Primary SMTP already ends with $primarySMTPDomain" -ForegroundColor Yellow
}
elseif ($aliasAddresses.Count -eq 1) {
$newPrimarySMTP = $aliasAddresses -replace "smtp:", "SMTP:"
Write-Host "Updating primary SMTP for $($user.DisplayName) to $newPrimarySMTP" -ForegroundColor Green
# Combine new primary with other addresses (keeping all existing addresses)
$updatedAddresses = @($newPrimarySMTP) + ($allEmailAddresses -replace "SMTP:", "smtp:" | Where-Object { $_ -ne $newPrimarySMTP })
# Set the new primary SMTP address
if ($WhatIf) {
Set-Mailbox -Identity $user -EmailAddresses $updatedAddresses -WhatIf
}
else {
Set-Mailbox -Identity $user -EmailAddresses $updatedAddresses
}
}
elseif ($aliasAddresses.Count -eq 0) {
Write-Host "No alias address found for $($user.DisplayName) - Primary SMTP not updated" -ForegroundColor Cyan
}
else {
Write-Host "Multiple alias addresses found for $($user.DisplayName) - Primary SMTP not updated" -ForegroundColor Red
}
}
Stop-Transcript
散裝設置Microsoft 365主要SMTP地址PowerShell腳本
該腳本可在Microsoft 365環境中在線交換。運行PowerShell作為管理員,並連接到在線PowerShell。
Connect-ExchangeOnline
轉到腳本路徑並運行Set-M365primaryAddress.ps1腳本。該腳本將通過Exchange Online組織中的所有郵箱進行。
參見:如何強制刪除Microsoft 365與PowerShell的接觸
筆記:有一個-如果什麼乾燥運行的參數,以免發生任何事情,如果一切看起來都想要,您可以仔細檢查PowerShell輸出。一旦一切都很好,請刪除-如果什麼參數並重新運行腳本。
C:scripts.Set-M365PrimaryAddress.ps1 -DomainName "exoip.nl" -WhatIf
在沒有腳本的情況下運行腳本-如果什麼範圍。
C:scripts.Set-M365PrimaryAddress.ps1 -DomainName "exoip.nl"
如果存在郵箱的多個別名地址,則不會應用任何更改,並且輸出將顯示該信息,以便您研究它。
在此示例中,使用域 @exoip.nl的現有別名地址將批量設置為主要SMTP地址。
Transcript started, output file is C:tempSet-M365Primary-SMTP-Address.log
Multiple alias addresses found for Adam Clark - Primary SMTP not updated
No alias address found for ALI TAJRAN - Primary SMTP not updated
Updating primary SMTP for Alison Bell to SMTP:[email protected]
No alias address found for Grace Rees - Primary SMTP not updated
Skipping Jeff Baker - Primary SMTP already ends with @tajran.com
No alias address found for John Maverick - Primary SMTP not updated
No alias address found for Joshua Hunter - Primary SMTP not updated
Updating primary SMTP for Zoë Roberts to SMTP:[email protected]
Transcript stopped, output file is C:tempSet-M365Primary-SMTP-Address.log
就是這樣!
結論
您學會瞭如何使用PowerShell將Alias地址設置為Microsoft 365中的主要SMTP地址。首先,下載SET-M365PrimaryAddress PowerShell腳本。然後,在您希望其在郵箱別名地址中搜索的命令中添加域。最後,運行腳本。切記首先使用-Whatif參數測試。
您喜歡這篇文章嗎?您可能還喜歡從Microsoft Outlook移動應用程序(iOS和Android)中發送為別名。不要忘記關注我們並分享這篇文章。
