使用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:脚本文件夹。创建一个脚本文件夹,如果您没有一个文件夹。

建议阅读:使用PowerShell将别名地址更改为主要SMTP地址

<#
    .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组织中的所有邮箱进行。

笔记:有一个-如果什么干燥运行的参数,以免发生任何事情,如果一切看起来都想要,您可以仔细检查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)中发送为别名。不要忘记关注我们并分享这篇文章。