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

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

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

使用 PowerShell 批量删除辅助 SMTP 地址


在上一篇文章中,您学习了如何使用 PowerShell 列出所有 SMTP 地址。我们可以看到邮箱配置了多个 SMTP 地址。这些辅助 SMTP 地址也称为别名地址。在本文中,您将了解如何使用 PowerShell 批量删除辅助 SMTP 地址。

介绍

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

  • 本地交换

  • 交换混合

  • 在线交流

准备删除 SMTP 地址 PowerShell 脚本

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

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

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

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

    # Change @contoso.com to the domain that you want to remove
    $Mailbox.EmailAddresses | Where-Object { ($_ -clike "smtp*") -and ($_ -like "*@contoso.com") } | 

    # Perform operation on each item
    ForEach-Object {

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

        # Write output
        Write-Host "Removing $_ from $Mailbox Mailbox" -ForegroundColor Green
    }
}

Stop-Transcript
  • 第 11 行:*@contoso.com 值更改为要从邮箱中删除的域。

举几个例子:

  • 示例 1:仅删除域为 www.a-d.site 的 SMTP 地址。添加“*@a-d.site”。

  • 示例 2:删除所有名为 tajran 的 SMTP 地址。添加“*tajran*”。

在我的例子中,它看起来是这样的。

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

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

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

    # Change @contoso.com to the domain that you want to remove
    $Mailbox.EmailAddresses | Where-Object { ($_ -clike "smtp*") -and ($_ -like "*tajran*") } | 

    # Perform operation on each item
    ForEach-Object {

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

        # Write output
        Write-Host "Removing $_ from $Mailbox Mailbox" -ForegroundColor Green
    }
}

Stop-Transcript

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

批量删除辅助 SMTP 地址 PowerShell 脚本

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

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

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

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

在我的示例中,所有包含 tajran 的 SMTP 地址都将被批量删除。运行脚本后,确认这些是需要删除的 SMTP 地址。

[PS] C:\>cd \scripts
[PS] C:\scripts>.\Remove-SMTP.ps1
Transcript started, output file is C:\temp\Remove-SMTP-Address.log
What if: Setting mailbox "Kyle Peake".
Removing smtp:[email protected] from Kyle Peake Mailbox
What if: Setting mailbox "Kylie Davidson".
Removing smtp:[email protected] from Kylie Davidson Mailbox
What if: Setting mailbox "Larson Tevin".
Removing smtp:[email protected] from Larson Tevin Mailbox
What if: Setting mailbox "Madeleine Ross".
Removing smtp:[email protected] from Madeleine Ross Mailbox
What if: Setting mailbox "Mary Walsh".
Removing smtp:[email protected] from Mary Walsh Mailbox
What if: Setting mailbox "Max Fraser".
Removing smtp:[email protected] from Max Fraser Mailbox
Transcript stopped, output file is C:\temp\Remove-SMTP-Address.log

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

[PS] C:\scripts>.\Remove-SMTP.ps1
Transcript started, output file is C:\temp\Remove-SMTP-Address.log
Removing smtp:[email protected] from Kyle Peake Mailbox
Removing smtp:[email protected] from Kylie Davidson Mailbox
Removing smtp:[email protected] from Larson Tevin Mailbox
Removing smtp:[email protected] from Madeleine Ross Mailbox
Removing smtp:[email protected] from Mary Walsh Mailbox
Removing smtp:[email protected] from Max Fraser Mailbox
Transcript stopped, output file is C:\temp\Remove-SMTP-Address.log

我希望这可以帮助您从 Exchange 组织的邮箱中删除不需要的 SMTP 地址。

继续阅读:使用 PowerShell 查找电子邮件地址 »

结论

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

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

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

取消回复欢迎 发表评论:

关灯