Display all mailbox calendars which a user has access to

独自空忆成欢 提交于 2019-12-11 08:53:23

问题


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


来源:https://stackoverflow.com/questions/51836373/display-all-mailbox-calendars-which-a-user-has-access-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!