Advertisement
Guest User

Get-MailboxFolderPermissionResolved

a guest
Apr 26th, 2013
1,240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-MailboxFolderPermissionResolved
  2. {
  3.     [CmdletBinding()]
  4.     param(
  5.         [Parameter(ValueFromPipeline=$True)] [string] $Identity
  6.     )
  7.     process
  8.     {
  9.         # First, get the permissions like normal
  10.         # But filter out "Default" entries since they cannot be resolved
  11.         [array] $rawACL = $identity | Get-MailboxFolderPermission -EA SilentlyContinue
  12.  
  13.         # If we wan't find an ACL for that folder? Just return nothing
  14.         # This happens a lot when feeding folder names from GetMailboxFolderStatistics
  15.         if ($rawACL -eq $null) { return }
  16.  
  17.         [array] $ACL = $rawACL |
  18.             Where { $_.User.DisplayName -ne "Default" } |
  19.             Where { $_.User.DisplayName -ne "Anonymous" } |
  20.             Where { -not ($_.User.DisplayName.StartsWith("NT User:")) }
  21.  
  22.         if ($ACL -eq $null -or $ACL.count -eq 0)
  23.         {
  24.             return
  25.         }
  26.  
  27.  
  28.         # We need to resolve the DisplayName into UserPrincipalName for each entry
  29.         # So iterate over each of the entries
  30.         foreach ($ace in $ACL)
  31.         {
  32.             $UserObject = $null
  33.             [string] $upn = ""
  34.             # Find all users that match this displayname
  35.             [array] $MatchUsers = get-user $ace.user.displayname
  36.             if ($MatchUsers.count -eq 1)
  37.             {
  38.                 # Okay no worries!
  39.                 $UserObject = $MatchUsers[0]
  40.                 $upn = $UserObject.UserPrincipalName
  41.             } else {
  42.                 foreach ($userobj in $MatchUsers) {
  43.                     # Do a lookup for each to check
  44.                     $lookup = $identity |
  45.                         get-mailboxfolderpermission -user $userobj.userPrincipalName -EA SilentlyContinue
  46.                     if ($lookup -ne $null -and $lookup.AccessRights[0] -eq $ace.AccessRights[0]) {
  47.                         $UserObject = $userobj
  48.                         $upn = $UserObject.UserPrincipalName
  49.                     }
  50.                 }
  51.             }
  52.  
  53.             if ($upn -eq "" -or $userObject -eq $null) {
  54.                 Write-Warning ("Could not find a match for: " + $ace.user.displayname)
  55.                 continue
  56.             }
  57.  
  58.             New-Object -TypeName PSObject -Property @{
  59.                 Identity=$upn;
  60.                 User=$ace.User;
  61.                 FolderName=$ace.FolderName;
  62.                 AccessRights=($ace.AccessRights -join ";");
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement