将多个foreach结果合并到一个报告中

时间:2018-04-06 20:04:14

标签: powershell

$results = foreach ($Mailbox in (Get-Mailbox -ResultSize Unlimited))
{
    get-MailboxFolderPermission -identity "$($Mailbox.Name):\Calendar" -ErrorAction SilentlyContinue |
        Where-Object {$_.User -notlike "Default" -and
                      $_.User -notlike "Anonymous" -and
                      $_.AccessRights -notlike "None" -and
                      $_.AccessRights } |
        Select @{N="Mailbox";E={$Mailbox.SamAccountName}}, FolderName, User, AccessRights
}
$results

我还在学习powershell(只有1年的经验)。我使用此代码报告我们环境中所有最终用户邮箱的日历权限。代码运行良好,但它只报告Calendar对象。我需要运行三个单独的报告来获取日历,联系人和收件箱权限。

我尝试过创建一个数组,但它会在一行中抛出多个值。 (有些最终用户有多个人可以访问他们的日历/联系人/收件箱。有没有人知道如何结合这些结果?

感谢

Here is an example of what results I would like:

2 个答案:

答案 0 :(得分:0)

仅使用其他

对邮箱进行一次迭代

ForEach ($Folder in 'Contents','Calendar','Inbox')

应该更有效率:

#Date

$date = (Get-Date -f yyyy-MM-dd)

#Pull Permissions

$Permissions = ForEach ($Mailbox in (Get-Mailbox -ResultSize Unlimited )) { 
    ForEach ($Folder in 'Contents','Calendar','Inbox'){
        Get-MailboxFolderPermission -identity "$($Mailbox.Name):\$($Folder)" -ErrorAction SilentlyContinue | 
          Where-Object {$_.User -notlike "Default" -and $_.User -notlike "Anonymous" -and $_.AccessRights -notlike "None" -and $_.AccessRights } | 
            Select @{N="Mailbox";E={$Mailbox.SamAccountName}}, 
                   @{N="Folder";E={$_.FolderName}}, 
                   @{N="User With Access";E={$_.User}}, 
                   @{N="Access";E={$_.AccessRights}}
    }
}

#Export to Desktop

$Permissions | Sort User | Export-Csv "$env:USERPROFILE\Desktop\ExchangePermissions-$Date.csv" -NoTypeInformation

答案 1 :(得分:0)

#Date

$date = (Get-Date -f yyyy-MM-dd)

#Pull Permissions

$Permissions = ForEach ($Mailbox in (Get-Mailbox -ResultSize Unlimited )) {
    $userInfo = get-user $Mailbox.name | select Title 
    ForEach ($Folder in 'Contacts','Calendar','Inbox'){
        Get-MailboxFolderPermission -identity "$($Mailbox.Name):\$($Folder)" -ErrorAction SilentlyContinue | 
          Where-Object {$_.User -notlike "Default" -and $_.User -notlike "Anonymous" -and $_.AccessRights -notlike "None" -and $_.AccessRights } | 
            Select @{N="Mailbox";E={$Mailbox.SamAccountName}},
                   @{N="Office";E={$Mailbox.Office}},
                   @{N="Title";E={$userInfo.Title}},
                   @{N="Folder";E={$_.FolderName}}, 
                   @{N="User With Access";E={$_.User}}, 
                   @{N="Access";E={$_.AccessRights}}
    }
}

#Export to Desktop

$Permissions | Sort User | Export-Csv 
"$env:USERPROFILE\Desktop\ExchangePermissions-$Date.csv" -NoTypeInformation