当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 批量添加辅助 SMTP 地址

作者:精品下载站 日期:2024-12-15 00:41:36 浏览:13 分类:玩电脑

使用 PowerShell 批量添加辅助 SMTP 地址


在上一篇文章中,您了解了如何查找丢失的 SMTP 地址。现在您已掌握信息,您希望向邮箱添加辅助 SMTP 地址。您可以手动添加辅助 SMTP 地址,但使用 PowerShell 脚本自动添加会更快。在本文中,您将了解如何使用 PowerShell 批量添加辅助 SMTP 地址。

介绍

Add-SMTP.ps1 PowerShell 脚本适用于:

  • 本地交换

  • 交换混合

  • 在线交流

准备添加 SMTP 地址 PowerShell 脚本

下载 Add-SMTP.ps1 脚本或将以下代码复制并粘贴到记事本中。将其命名为 Add-SMTP.ps1 并将其放置在 C:\scripts 文件夹中。如果没有,请创建一个 scripts 文件夹。

# Output will be added to C:\temp folder. Open the Add-SMTP-Address.log with a text editor. For example, Notepad.
Start-Transcript -Path C:\temp\Add-SMTP-Address.log -Append

# Get all mailboxes
$Mailboxes = Get-Mailbox -ResultSize Unlimited

# Loop through each mailbox
foreach ($Mailbox in $Mailboxes) {

    # Search for specified SMTP address in every mailbox
    $SMTPAddress = $Mailbox.EmailAddresses | Where-Object { $_ -like "*@contoso.com" }
      
    # Do nothing when there is already an SMTP address configured
    If (($SMTPAddress | Measure-Object).Count -eq 0) {
	
        # Change @contoso.com to the domain that you want to add
        $Address = "$($Mailbox.Alias)@contoso.com"

        # Remove the -WhatIf parameter after you tested and are sure to add the secondary email addresses
        Set-Mailbox $Mailbox.DistinguishedName -EmailAddresses @{add = $Address } -WhatIf

        # Write output
        Write-Host "Adding $($Address) to $($Mailbox.Name) Mailbox" -ForegroundColor Green
    }
}

Stop-Transcript
  • 第 11 行和第 17 行:将两个位置的 @contoso.com 值更改为要添加到邮箱的域。

在我的例子中,它看起来是这样的。它将查找域名为 @a-d.site 的邮箱。如果未设置,则 SMTP 地址 [email protected] 将添加到邮箱中。

# Output will be added to C:\temp folder. Open the Add-SMTP-Address.log with a text editor. For example, Notepad.
Start-Transcript -Path C:\temp\Add-SMTP-Address.log -Append

# Get all mailboxes
$Mailboxes = Get-Mailbox -ResultSize Unlimited

# Loop through each mailbox
foreach ($Mailbox in $Mailboxes) {

    # Search for specified SMTP address in every mailbox
    $SMTPAddress = $Mailbox.EmailAddresses | Where-Object { $_ -like "*@a-d.site"}
      
    # Do nothing when there is already an SMTP address configured
    If (($SMTPAddress | Measure-Object).Count -eq 0) {
	
        # Change @contoso.com to the domain that you want to add
        $Address = "$($Mailbox.Alias)@a-d.site"

        # Remove the -WhatIf parameter after you tested and are sure to add the secondary email addresses
        Set-Mailbox $Mailbox.DistinguishedName -EmailAddresses @{add = $Address } -WhatIf

        # Write output
        Write-Host "Adding $($Address) to $($Mailbox.Name) Mailbox" -ForegroundColor Green
    }
}

Stop-Transcript

在下一步中,您将运行该脚本并查看其实际效果。

批量添加辅助 SMTP 地址 PowerShell 脚本

该脚本适用于 Exchange 本地、Exchange 混合和 Exchange Online。在运行脚本之前,您必须连接正确的工具:

  • 本地 Exchange/Exchange 混合:以管理员身份运行 Exchange 命令行管理程序。

  • Exchange Online (Microsoft 365/Office 365):以管理员身份运行 PowerShell 并连接到 Exchange Online PowerShell。

转到脚本路径并运行 Add-SMTP.ps1 脚本。该脚本将遍历 Exchange 组织中的所有邮箱。脚本中添加了 -WhatIf 参数。如果运行该脚本,环境中不会发生任何事情。您将得到一个输出,显示将会发生什么。

在我的示例中,将批量添加 SMTP 地址@a-d.site。运行脚本后,确认这些是需要添加的 SMTP 地址。

[PS] C:\> cd \scripts
[PS] C:\scripts>.\Add-SMTP.ps1
Transcript started, output file is C:\temp\Add-SMTP-Address.log
What if: Setting mailbox "exoip.local/Users/Administrator".
Adding [email protected] to Administrator Mailbox
What if: Setting mailbox "exoip.local/Company/Users/HR/Amanda Morgan".
Adding [email protected] to Amanda Morgan Mailbox
What if: Setting mailbox "exoip.local/Company/Users/HR/Christopher Payne".
Adding [email protected] to Christopher Payne Mailbox
What if: Setting mailbox "exoip.local/Company/Users/ServiceAccounts/Mary Walsh".
Adding [email protected] to Mary Walsh Mailbox
What if: Setting mailbox "exoip.local/Company/Users/HR/Dylan Piper".
Adding [email protected] to Dylan Piper Mailbox
What if: Setting mailbox "exoip.local/Company/Users/ServiceAccounts/Benetiz Anees".
Adding [email protected] to Benetiz Anees Mailbox
Transcript stopped, output file is C:\temp\Add-SMTP-Address.log

从 PowerShell 脚本中删除 -WhatIf 参数并重新运行该脚本。 SMTP 地址是批量添加的。

[PS] C:\scripts>.\Add-SMTP.ps1
Transcript started, output file is C:\temp\Add-SMTP-Address.log
Adding [email protected] to Administrator Mailbox
Adding [email protected] to Amanda Morgan Mailbox
Adding [email protected] to Christopher Payne Mailbox
Adding [email protected] to Mary Walsh Mailbox
Adding [email protected] to Dylan Piper Mailbox
Adding [email protected] to Benetiz Anees Mailbox
Transcript stopped, output file is C:\temp\Add-SMTP-Address.log

我希望这可以帮助您将 SMTP 地址批量添加到 Exchange 组织中的邮箱。

了解更多:列出不继承电子邮件地址政策的用户 »

结论

在本文中,您了解了如何使用 PowerShell 批量添加辅助 SMTP 地址。下载 Add-SMTP PowerShell 脚本。添加您要添加的域并运行脚本。脚本输出将显示添加了哪些代理地址。不要忘记首先使用 -WhatIf 参数进行测试,如文章中所示。

您喜欢这篇文章吗?您可能还喜欢使用 PowerShell 获取 Exchange 中所有用户的邮箱大小。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯