PowerShell 列出拥有超过 1 人完全访问委托权限的 Exchange 邮箱
PowerShell to list Exchange mailbox that have Full Access delegate permission more than 1 person
我需要知道除用户显示名称本身之外,有多个人当前正在访问哪个 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
是布尔值你可以直接使用 -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}}
您可以尝试使用以下代码列出具有完全访问委托权限的交换邮箱:
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
我需要知道除用户显示名称本身之外,有多个人当前正在访问哪个 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
是布尔值你可以直接使用 -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}}
您可以尝试使用以下代码列出具有完全访问委托权限的交换邮箱:
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