问题
I would like to find out all the calendars which a user can currently access. I searched up the Internet and the closest answer I got is this:
(get-mailbox) | foreach{Get-Mailboxfolderpermission (($_.PrimarySmtpAddress)+":\calendar") -user happyboy -ErrorAction SilentlyContinue} |select identity, user, accessrights
However, the display does not really show the actual identity which is the actual mailbox which happyboy (above) has access. The display is something like this:
Identity User AccessRights
-------- ---- ------------
HappyBoy HappyBoy {Reviewer}
HappyBoy HappyBoy {LimitedDetails}
HappyBoy HappyBoy {Editor}
HappyBoy HappyBoy {Editor}
HappyBoy HappyBoy {Owner}
HappyBoy HappyBoy {Editor}
I was expecting something like this:
Identity User AccessRights
-------- ---- ------------
FunnyMan HappyBoy {Reviewer}
PrettyGirl HappyBoy {LimitedDetails}
BadBoy HappyBoy {Editor}
LuckyBoy HappyBoy {Editor}
SadGirl HappyBoy {Owner}
LovelyGirl HappyBoy {Editor}
Can we modify the script to achieve this?
回答1:
Your one liner formatted a bit more readable:
(Get-Mailbox) | ForEach-Object {
Get-Mailboxfolderpermission (($_.PrimarySmtpAddress)+":\calendar") `
-User happyboy -ErrorAction SilentlyContinue
} | Select-Object Identity, User, Accessrights
Should make clear that the terminating pipe element (Select-Object
) receives the output of the Get-Mailboxfolderpermission
cmdlet, the Get-Mailbox
output is no longer directly accessible.
This (untested) script uses the $mailbox variable to store the currently iterated mailbox.
## Q:\Test\2018\08\14\SO_51836373.ps1
ForEach ($mailbox in (Get-Mailbox)){
Get-Mailboxfolderpermission (($mailbox.PrimarySmtpAddress)+":\calendar") `
-User happyboy -ErrorAction SilentlyContinue | ForEach-Object {
[PSCustomObject]@{
Identity = $mailbox.Identity
User = $_.User
AccessRights = $_.Accessrights
}
}
}
Another approach more resembling your template stores the Mailbox identity and inserts a calculated property into the Select-Object. (also untested)
(Get-Mailbox) | ForEach-Object {
$Identity = $_.Identity
Get-Mailboxfolderpermission (($_.PrimarySmtpAddress)+":\calendar") `
-User happyboy -ErrorAction SilentlyContinue
} | Select-Object @{n='Identity';e={$Identity}}, User, Accessrights
来源:https://stackoverflow.com/questions/51836373/display-all-mailbox-calendars-which-a-user-has-access-to