J Lee
J Lee

Reputation: 13

Display all mailbox calendars which a user has access to

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?

Upvotes: 1

Views: 14690

Answers (1)

user6811411
user6811411

Reputation:

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-Mailboxoutput 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

Upvotes: 2

Related Questions