PowerShell是否列出具有“完全访问”委托权限的多于1个人的Exchange邮箱?

时间:2018-09-03 12:05:35

标签: powershell active-directory office365 exchange-server

我需要知道除用户显示名称本身以外的多个人当前正在访问哪个Exchange用户邮箱?

Get-Mailbox -ResultSize Unlimited | Get-MailboxPermission | Where-Object { ($_.AccessRights -eq "FullAccess") -and ($_.IsInherited -eq $false) -and -not ($_.User -like "NT AUTHORITY\SELF") -and -not ($_.User -like '*Discovery Management*') } |
   Select @{Name="User Name";expression={(Get-Recipient $_.user.tostring()).displayname}}, Identity,AccessRights,PrimarySMTPAddress | Export-Csv C:\Results.csv -NoTypeInformation

如果有人知道更好的脚本或可以解决上述问题,将不胜感激。

谢谢。

2 个答案:

答案 0 :(得分:2)

尽管使用-and -not是正确的,但我并不是说这不是最优雅的方法,因为-like-eq的运算符是相反的({{3}建议}中的已删除评论)。您的where语句可以修改为:

 Where-Object { ($_.AccessRights -like "*FullAccess*") -and (-not $_.IsInherited) -and ($_.User -ne "NT AUTHORITY\SELF") -and ($_.User -notlike '*Discovery Management*') }

我所做的更改:

# from
($_.AccessRights -eq "FullAccess")
# to 
($_.AccessRights -like "*FullAccess*")

包括用户在AccessRight中有一个或多个访问条目的情况(尽管我不确定现实生活中是否需要它)。您的代码将过滤{FullAccess, ReadPermission},因为它不等于FullAccess

# from
($_.IsInherited -eq $false) 
# to 
(-not $_.IsInherited)

为什么?更优雅。 IsInherited是布尔值,您可以直接使用-not

# from
-and -not ($_.User -like "NT AUTHORITY\SELF")
# to
-and ($_.User -ne "NT AUTHORITY\SELF")

为什么?这里不需要like / notlike,您可以直接使用-ne

# from 
-and -not ($_.User -like '*Discovery Management*')
# to
-and ($_.User -notlike '*Discovery Management*')

与上述类似,但是我不确定这里可以使用哪些值,因此我没有更改为-ne


此外,在您的Select-Object中,您使用PrimarySMTPAddress,因为权限输入中没有这样的参数,因此无法使用。您将必须使用与User Name相似的方法(同样,在这种情况下,我不认为.ToString()是必需的)

@{Name="PrimarySMTPAddress";expression={(Get-Recipient $_.user).PrimarySMTPAddress}}

答案 1 :(得分:1)

您可以尝试下面的代码列出具有完全访问委托权限的交换邮箱:

Get-Mailbox -ResultSize Unlimited | Get-MailboxPermission | Where-Object { ($_.AccessRights -like "*FullAccess*") -and (-not $_.IsInherited) -and ($_.User -ne "NT AUTHORITY\SELF") -and ($_.User -notlike '*Discovery Management*') } |
Select @{Name="User Name";expression={(Get-Recipient $_.user.tostring()).displayname}}, Identity,AccessRights,PrimarySMTPAddress | Export-Csv C:\Results.csv -NoTypeInformation