您已经拥有每个邮箱集的主要SMTP地址和别名地址。但是,任务是将别名地址与主要SMTP地址交换。最好的方法是与PowerShell一起使用。在本文中,您将学习如何将Alias地址更改为与PowerShell Exchange Server中的主要SMTP地址。
介绍
Set-promaryAddress.ps1 PowerShell脚本可用于:
- 交换本地
- 交换混合动力
如果您想对在线交换(Microsoft 365)环境进行相同的操作,请阅读文章将Microsoft 365别名地址更改为PowerShell的主要SMTP地址。
笔记:别名SMTP地址成为主要SMTP地址,主要SMTP地址将成为别名地址。因此,主要的SMTP地址不会被删除;它只是与别名SMTP地址交换。
开始之前
如果每个邮箱都没有设置别名地址怎么办?最好的是通过PowerShell添加次级SMTP地址浏览文章。
一旦您验证了所有邮箱设置的别名地址,请从本文中运行Set-primaryAddress.ps1 PowerShell脚本,以将别名地址与主SMTP地址交换。
假设您已经完成了,并且想删除现在是别名地址的旧主SMTP地址,您可以使用PowerShell删除secondary SMTP地址。
在一个阶段进行此操作总是比立即完成所有操作的脚本要好,因此您确定所有内容都已正确设置。
下载set-promaryAddress.ps1 powershell脚本,或将下面的代码复制并粘贴到记事本中。给它名字set-primaryAddress.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 Management Shell。
转到脚本路径并运行set-promaryAddress.ps1脚本。该脚本将通过Exchange组织中的所有邮箱。
C:scripts.Set-PrimaryAddress.ps1 -DomainName "exoip.nl" -WhatIf
假设您想在交换混合环境中定位云邮箱,您需要添加-偏僻的范围。这将从Get-Mailbox更改为Get-RemoteMailbox以检索所有云邮箱,并且SET-MAILBOX将更改为Set-Remotemailbox,以将更改应用于Cloud Mailbox。
C:scripts.Set-PrimaryAddress.ps1 -DomainName "exoip.nl" -Remote -WhatIf
笔记:有一个-如果什么干燥运行的参数,以免发生任何事情,如果一切看起来都想要,您可以仔细检查PowerShell输出。一旦一切都很好,请删除-如果什么参数并重新运行脚本。
参见:
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
No alias address found for Discovery Search Mailbox - Primary SMTP not updated
Updating primary SMTP for James Paterson to
No alias address found for Fraser, Max - Primary SMTP not updated
Updating primary SMTP for Nicholas Murray to
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-PraryAddress PowerShell脚本。然后,在您希望其在邮箱别名地址中搜索的命令中添加域。最后,运行脚本。切记首先使用-Whatif参数测试。
您喜欢这篇文章吗?您也可能喜欢创建具有相同别名的共享邮箱。不要忘记关注我们并分享这篇文章。