我需要知道除用户显示名称本身之外的多个人当前正在访问哪个 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
这里需要修复什么?
最佳答案
尽管使用 -and -not
是正确的,但我并不是说这不是最优雅的方法,因为存在与 -like
和 - 相反的运算符eq
(由 @Paxz 在现已删除的评论中建议)。您的 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
是 bool 值,您可以直接使用-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}}
关于PowerShell 列出具有超过 1 人完全访问委托(delegate)权限的 Exchange 邮箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52149486/