Advertisement
Guest User

GetAllMailboxes-WhereFullAccessFor_

a guest
Feb 23rd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. FUNCTION GetAllMailboxes-WhereFullAccessFor
  2. {
  3.  
  4.     PARAM(
  5.         [Parameter(Mandatory = $true, Position = 0)]
  6.         [ValidateNotNull()]
  7.         [string]$Account,
  8.  
  9.         [Parameter(Mandatory = $true)]
  10.         [ValidateScript({[System.IO.Directory]::Exists("$($_)")})]
  11.         [string]$SaveToDir,
  12.  
  13.         [Parameter(Mandatory = $true)]
  14.         [ValidateSet('CSV','HTML')]
  15.         $ExportAs
  16.     )
  17.  
  18.     TRY
  19.     {
  20.         Write-Host "NOTE: This may take some time to complete, please be patient.." -f Magenta              
  21.         $SU = Get-ADuser -Filter "Name -like '*$Account*'"
  22.         IF($SU.Count -gt 1)
  23.         {
  24.             FUNCTION FoundMulti
  25.             {
  26.                 Write-Host ''
  27.                 Write-Host "We Found Multiple Users That Match The Account: $Account" -f Yellow
  28.                 $ii = 0
  29.                 $SU | % {
  30.                     Write-Host "$ii. $_" -f Green
  31.                     $ii++
  32.                 }
  33.                 $RH = Read-Host "Please type corresponding number for listed accounts, and press enter."
  34.                 $theError = "Please Make A Valid Selection!"
  35.                 $theSuccess = "Thank you, now locating all accounts with full access for $($SU.SamAccountName[$RH])"
  36.                 IF( ($RH -lt 1) -or ($RH -gt $SU.Count) ){ Write-Host $theError -f Red; Pause; FoundMulti }
  37.                 IF( ($RH -ge 1) -and ($RH -le $SU.Count) ){ Write-Host $theSuccess -f Green; return $($SU.SamAccountName[$RH]) }
  38.                
  39.             }            
  40.             $Account = FoundMulti
  41.         }
  42.         IF($SU.Count -eq 1)
  43.         {
  44.             $Account = $($SU.SamAccountName)
  45.         }
  46.        
  47.         # HTML styling for report that will be generated
  48.         $Head = "<style>"
  49.         $Head += "`r`nBODY{background-color:peachpuff;}"
  50.         $Head += "`r`nTABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
  51.         $Head += "`r`nTH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}"
  52.         $Head += "`r`nTD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"
  53.         $Head += "`r`n</style>"  
  54.  
  55.         $uObj = @()
  56.         FOREACH($Mbx in (Get-Mailbox -ResultSize Unlimited))
  57.         {
  58.             $FAU = Get-MailboxPermission $Mbx | ? {($_.AccessRights -like "*FullAccess*") -and (-not $_.Deny)}                                    
  59.             FOREACH($u in $FAU)
  60.             {    
  61.                 IF($u.User.RawIdentity -like "*\$Account*")
  62.                 {    
  63.                     Write-Host "The account '$Account' has full access over mailbox '$Mbx'" -f Yellow                          
  64.                     $uProps = [array][pscustomobject] @{
  65.                         'Account' = $u.User.RawIdentity
  66.                         'HasFullAccessTo' = $Mbx
  67.                     }
  68.                     $uObj += $uProps                    
  69.                 }
  70.             }                                            
  71.         }
  72.         $tPreC = "<H2>$Account Has Full Access Over:</H2>"
  73.         $rPostC = $uObj | sort Account,HasFullAccessTo | ConvertTo-Html -Fragment -PreContent $tPreC | Out-String  
  74.         $ExGo = $true
  75.     }
  76.  
  77.     CATCH
  78.     {
  79.         $ExGo = $false
  80.         $_
  81.     }
  82.  
  83.     FINALLY
  84.     {  
  85.         IF($ExGo -eq $true)
  86.         {
  87.             SWITCH($ExportAs)
  88.             {
  89.                 'HTML' {
  90.                     # Export to HTML, and open directory                                                                        
  91.                     $HtmlSplat = @{
  92.                         'Head'= $Head
  93.                         'PostContent' = $rPostC
  94.                         'PreContent' =<H1>Mailbox Full Access Report</H1>
  95.                     }
  96.                     $oPath = "$SaveToDir\User_FullAccessReport.htm"
  97.                     IF(Test-Path $oPath)
  98.                     {
  99.                         $r = Get-Random 99
  100.                         $oPath = "$SaveToDir\User_FullAccessReport$r.htm"
  101.                         ConvertTo-Html @HtmlSplat| Out-File "$oPath"
  102.                     }  
  103.                     IF(!(Test-Path $oPath))
  104.                     {
  105.                         ConvertTo-Html @HtmlSplat| Out-File "$oPath"
  106.                     }  
  107.                 }
  108.  
  109.                 'CSV' {
  110.                     $oPath = "$SaveToDir\User_FullAccessReport.csv"
  111.                     IF(Test-Path $oPath)
  112.                     {
  113.                         $r = Get-Random 99
  114.                         $oPath = "$SaveToDir\User_FullAccessReport$r.csv"
  115.                         $uObj | sort | Export-Csv -Path "$oPath" -Force -NoTypeInformation  
  116.                     }  
  117.                     IF(!(Test-Path $oPath))
  118.                     {
  119.                         $uObj | sort | Export-Csv -Path "$oPath" -Force -NoTypeInformation                        
  120.                     }                    
  121.                 }
  122.             }
  123.             Start $SaveToDir
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement