使用 PowerShell 將別名地址更改為主 SMTP 地址
您已擁有每個郵箱集的主 SMTP 地址和別名地址。但是,任務是將別名地址與主 SMTP 地址交換。執行此操作的最佳方法是使用 PowerShell。在本文中,您將了解如何使用 PowerShell 將別名地址更改為 Exchange Server 中的主 SMTP 地址。
介紹
Set-PrimaryAddress.ps1 PowerShell 腳本適用於:
- 本地交換
- 交換混合
如果您想對 Exchange Online (Microsoft 365) 環境執行相同的操作,請閱讀文章使用 PowerShell 將 Microsoft 365 別名地址更改為主 SMTP 地址。
筆記:別名 smtp 地址成為主 SMTP 地址,主 SMTP 地址將成為別名地址。所以主SMTP地址不會被刪除;它只是與別名 smtp 地址進行交換。
開始之前
如果沒有為每個郵箱設置別名地址怎麼辦?最好是閱讀使用 PowerShell 批量添加輔助 SMTP 地址一文。
驗證是否為所有郵箱設置了別名地址後,運行本文中的 Set-PrimaryAddress.ps1 PowerShell 腳本以將別名地址與主 SMTP 地址交換。
假設您已完成並想要刪除現在是別名地址的舊主 SMTP 地址,您可以閱讀使用 PowerShell 批量刪除輔助 SMTP 地址一文。
分階段執行此操作總是比使用一個腳本立即完成所有操作要好,這樣您就可以確保所有設置都正確無誤。
下載 Set-PrimaryAddress.ps1 PowerShell 腳本或將以下代碼複製並粘貼到記事本中。給它起個名字設置主地址.ps1並將其放置在C:腳本文件夾。創建一個腳本文件夾(如果沒有)。
<#
.SYNOPSIS
Set-PrimaryAddress.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 Server and Exchange Hybrid. 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-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, 06/04/2024 - Initial version
V1.10, 09/03/2024 - Added parameters
#>
param (
[Parameter(Mandatory = $true)]
[string]$DomainName,
[switch]$WhatIf,
[switch]$Remote
)
# Output will be added to C:temp folder. Open the log with a text editor.
Start-Transcript -Path C:tempSet-Primary-SMTP-Address.log -Append
# Ensure the domain starts with '@'
$PrimarySMTPDomain = "@" + $DomainName
# Choose the appropriate cmdlets based on the switch
if ($Remote) {
$GetMailboxCmdlet = "Get-RemoteMailbox"
$SetMailboxCmdlet = "Set-RemoteMailbox"
}
else {
$GetMailboxCmdlet = "Get-Mailbox"
$SetMailboxCmdlet = "Set-Mailbox"
}
# Get all mailbox users
$users = Invoke-Expression "$GetMailboxCmdlet -ResultSize Unlimited"
foreach ($user in $users) {
$currentPrimarySMTP = $user.PrimarySmtpAddress.ToString()
$aliasAddresses = $user.EmailAddresses | Where-Object { $_ -clike "smtp*" -and $_ -clike "*$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:", ""
Write-Host "Updating primary SMTP for $($user.DisplayName) to $newPrimarySMTP" -ForegroundColor Green
# Set the new primary SMTP address
if ($WhatIf) {
Invoke-Expression "$SetMailboxCmdlet -Identity '$($user.Identity)' -PrimarySmtpAddress '$newPrimarySMTP' -EmailAddressPolicyEnabled `$false -WhatIf"
}
else {
Invoke-Expression "$SetMailboxCmdlet -Identity '$($user.Identity)' -PrimarySmtpAddress '$newPrimarySMTP' -EmailAddressPolicyEnabled `$false"
}
}
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
批量設置主 SMTP 地址 PowerShell 腳本
該腳本適用於 Exchange 本地和 Exchange 混合環境。您必須以管理員身份運行 Exchange 命令行管理程序。
轉到腳本路徑並運行 Set-PrimaryAddress.ps1 腳本。該腳本將遍歷 Exchange 組織中的所有郵箱。
C:scripts.Set-PrimaryAddress.ps1 -DomainName "exoip.nl" -WhatIf
假設您想要將 Exchange 混合環境中的雲郵箱作為目標,您需要添加-偏僻的範圍。這將從 Get-Mailbox 更改為 Get-RemoteMailbox 以檢索所有云郵箱,並且 Set-Mailbox 將從更改為 Set-RemoteMailbox 以將更改應用到雲郵箱。
C:scripts.Set-PrimaryAddress.ps1 -DomainName "exoip.nl" -Remote -WhatIf
筆記:有一個-如果什麼參數進行空運行,這樣就不會發生任何事情,並且如果一切看起來都符合您的要求,您可以仔細檢查 PowerShell 輸出。一旦一切看起來都不錯,刪除-如果什麼參數並重新運行腳本。
了解更多:在 Exchange 中使用 PowerShell 獲取所有 SMTP(電子郵件)地址
C:scripts.Set-PrimaryAddress.ps1 -DomainName "exoip.nl"
如果郵箱存在多個別名地址,它將不會應用任何更改,並且輸出將顯示該信息,以便您可以查看它。
在此示例中,域為 @exoip.nl 的現有別名地址將被批量設置為主 SMTP 地址。
Transcript started, output file is C:tempSet-Primary-SMTP-Address.log
Multiple alias addresses found for Alysia Maverick - Primary SMTP not updated
No alias address found for Boris Campbell - Primary SMTP not updated
Updating primary SMTP for Christopher Payne to [email protected]
No alias address found for Discovery Search Mailbox - Primary SMTP not updated
Updating primary SMTP for James Paterson to [email protected]
No alias address found for Fraser, Max - Primary SMTP not updated
Updating primary SMTP for Nicholas Murray to [email protected]
No alias address found for Richard Hunter - Primary SMTP not updated
No alias address found for sharedmailboxonprem - Primary SMTP not updated
Transcript stopped, output file is C:tempSet-Primary-SMTP-Address.log
現有的 SMTP 別名地址現在是所有用戶的主要 SMTP 地址。
結論
您了解瞭如何使用 PowerShell 將別名地址批量設置為主 SMTP 地址。首先,下載 Set-PrimaryAddress PowerShell 腳本。然後,在命令中添加您希望其在郵箱別名地址中搜索的域。最後,運行腳本。請記住首先使用 -WhatIf 參數進行測試。
您喜歡這篇文章嗎?您可能還喜歡創建具有相同別名的共享郵箱。不要忘記關注我們並分享這篇文章。
