【问题标题】:List all mailboxes that forward to a specific user列出转发给特定用户的所有邮箱
【发布时间】:2014-03-21 20:50:29
【问题描述】:
我有这个脚本列出了所有正在转发电子邮件的邮箱,但是,我很好奇是否有办法让它返回所有转发给特定用户的邮箱。基本上,我试图找出将邮件转发到“johndoe”的每个邮箱。任何帮助将不胜感激!这是为了交换 2007 btw...
这是目前为止的脚本:
$fwds = 获取邮箱 | Where-Object { $_.ForwardingAddress -ne $null }
|选择名称、转发地址
foreach ($fwd in $fwds) {$fwd | add-member -membertype noteproperty
-name “ContactAddress” -value (get-contact $fwd.ForwardingAddress).WindowsEmailAddress}
$fwds
【问题讨论】:
标签:
powershell
exchange-server
【解决方案1】:
Exchange 使用 CanonicalName 作为转发地址,因此您需要从用户名中查找该地址。因为它可能是邮箱、DL 或联系人,所以我知道的最简单的方法是使用 Get-Recipient,并获取 Identity 属性。
$RecipientCN = (get-recipient johndoe).Identity
get-mailbox | Where-Object { $_.ForwardingAddress -eq $RecipientCN }
【解决方案2】:
@mjolinor 的版本可以工作,但速度很慢,因为它会加载所有邮箱。在我的系统上,通过大约 300 个邮箱大约需要 30 秒。
这可以通过在 Get-Mailbox 命令中添加过滤器来加快速度,以仅返回实际正在转发的邮件,如下所示:
$RecipientCN = (get-recipient johndoe).Identity
Get-Mailbox -ResultSize Unlimited -Filter {ForwardingAddress -ne $null} | Where-Object {$_.ForwardingAddress -eq $RecipientCN}
但是等等,我们可以变得更快!为什么不在过滤器中搜索正确的用户权限?可能是因为很难获得正确的语法,因为在 -Filter 中使用变量会让人感到困惑。
诀窍是在整个过滤器表达式周围使用双引号,在变量周围使用单引号:
$RecipientCN = (get-recipient johndoe).Identity
Get-Mailbox -ResultSize Unlimited -Filter "ForwardingAddress -eq '$RecipientCN'"
此版本在 0.6 秒内返回相同的结果 - 大约快 50 倍。