【问题标题】:Getting inbox rules for all mailboxes which set to forward redirect to获取所有设置为转发重定向到的邮箱的收件箱规则
【发布时间】:2022-12-30 01:00:56
【问题描述】:

我正在寻找一个脚本来查询整个组织(所有邮箱),找到设置为转发、重定向或作为附件转发到外部地址的收件箱规则,并输出到具有用户 ID、规则名称和外部的文件收件人 smtp。

到目前为止,我已经写过类似的东西。

特别是,我得到了非常奇怪的 Ruledescription 和 RedirectTo 输出。我们如何解决这个问题?

脚本 :

Get-Mailbox -ResultSize Unlimited  |
foreach {
    Write-Verbose "Checking $($_.alias)..." -Verbose
    $inboxrule = get-inboxrule -Mailbox $_.alias  
    if ($inboxrule) {
        foreach($rule in $inboxrule){
        [PSCustomObject]@{
            Mailbox         = $_.alias
            Rulename        = $rule.name
            Rulepriority    = $rule.priority
            Ruledescription = $rule.description
            ForwardTo       = $rule.ForwardTo
            ForwardAsAttachmentTo = $rule.ForwardAsAttachmentTo
            RedirectTo = $rule.RedirectTo
            DeleteMessage = $rule.DeleteMessage
        }
    }
    }
} | 
Export-csv "C:\temp\inbox_ruleexport.csv" -NoTypeInformation -encoding UTF8

输出 :

"Mailbox","Rulename","Rulepriority","Ruledescription","ForwardTo","ForwardAsAttachmentTo","RedirectTo","DeleteMessage"
"user","[all forwarding]","1","Take the following actions:
    redirect the message to 'user@contoso.com'
    and stop processing more rules on this message
",,,"Microsoft.Exchange.Data.Storage.Management.ADRecipientOrAddress[]","False"

【问题讨论】:

    标签: powershell


    【解决方案1】:

    Ruledescription 没问题。这正是在 Outlook 中看到的描述。它只是不那么漂亮并且包含那些换行符。你可以删除它们

    Ruledescription = $rule.description -replace [Environment]::NewLine , '' 
    

    RedirectTo 返回类型为ADRecipientOrAddress[] 的数组,Powershell 无法将其转换为字符串。但是,它包含有用的信息。你只想要向外转发的规则,所以你可以在创建[PSCustomObject]之前过滤这些对象的RoutingType是否包含SMTP

    $rule.ForwardTo.RoutingType -contains `SMTP`. 
    

    ForwardAsAttachmentToRedirectTo也是如此。
    现在我们只想要 ADRecipientOrAddress[]Address。所以我们只选择这个:

    ForwardTo = $rule.ForwardTo | Select-Object -ExpandProperty Address
    

    最重要的是,每个对象可以有多个地址,因为它是一个数组。这里我们应用了一个小技巧,直接在字符串中使用它们:

    ForwardTo = "$($rule.ForwardTo | Select-Object -ExpandProperty Address)"
    

    完成所有这些后,您的 CSV 文件应该看起来不错。

    Get-Mailbox -ResultSize Unlimited  |
    foreach {
        Write-Verbose "Checking $($_.alias)..." -Verbose
        $inboxrule = get-inboxrule -Mailbox $_.alias  
        if ($inboxrule) {
            foreach($rule in $inboxrule){
            if ($rule.ForwardTo.RoutingType -contains 'SMTP'-or $rule.ForwardAsAttachmentTo.RoutingType -contains 'SMTP'-or $rule.RedirectTo.RoutingType -contains 'SMTP' ){
                [PSCustomObject]@{
                    Mailbox         = $_.alias
                    Rulename        = $rule.name
                    Rulepriority    = $rule.priority
                    Ruledescription = $rule.description -replace [environment]::NewLine , ''
                    ForwardTo       = "$($rule.ForwardTo | Select-Object -ExpandProperty Address)"
                    ForwardAsAttachmentTo = "$($rule.ForwardAsAttachmentTo | Select-Object -ExpandProperty Address)"
                    RedirectTo = "$($rule.RedirectTo | Select-Object -ExpandProperty Address)"
                    DeleteMessage = $rule.DeleteMessage
                }
            }
    
        }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 2015-02-08
      • 1970-01-01
      • 2021-09-10
      • 2019-10-24
      • 2012-11-27
      相关资源
      最近更新 更多