clear-host
 
write-host Please choose an option below:
 
 
write-host '1) Get Mailbox From Server'
 
write-host '2) Get MailBox From SpreadSheet'
 
write-host '3) Exit' -ForegroundColor Red
 
write-host
 
$Option = Read-Host "Select an option [1 - 3]"


switch($Option)
{
1 
    { 
        $AllMailboxes = @()
        Get-Mailbox -ResultSize Unlimited | % { $AllMailboxes += $_ }
    }
2
    {
        $AllMailboxes = @()
        Import-Csv "\\nopmgr1\g$\CM Storage\Migration Batches\Done CSV\Migration_Batch1_Batch3_Combined.csv" | % { $AllMailboxes += (Get-Mailbox $_.ImportName) }
    }
3
    {
        $AllMailboxes = $null
    }
}


$OutputArray = @()
$MailboxCount = 0
$AllMailboxCount = $AllMailboxes.Count

foreach($Mailbox in $AllMailboxes)
{
    Write-Host $Mailbox.DisplayName
    $MailboxCount++
    Write-Progress -PercentComplete (($MailboxCount / $AllMailboxCount) * 100) -Activity $Mailbox.DisplayName

    $FullAccessHash = @{}
    $SendOnBehalfHash = @{}
    $SendAsHash = @{}
    $AllTrustees = @()

    ## Get FullAccess Perms
    Get-MailboxPermission -Identity $Mailbox.Identity.ToString() | ? {$_.AccessRights -eq 'FullAccess' -and $_.IsInherited -eq $FALSE -and $_.User -notlike "*NotesAd*"} | % {
    
        $FullAccessHash.Add((Get-Recipient $_.User).Alias.ToString(), $true)
        $AllTrustees += (Get-Recipient $_.User).Alias.ToString()

    }#end mbxperm

    ## Get SendOnBehalfTo Perms
    $Mailbox.GrantSendOnBehalfTo | % {
         $SendOnBehalfHash.Add((Get-Recipient $_).Alias.ToString(), $true)
         $AllTrustees += (Get-Recipient $_).Alias.ToString()

    }#end sendonbehalf

    ## Get SendAs Perms
    <#
    Get-ADPermission -Identity $Mailbox.Identity | ? {$_.IsInherited -eq $FALSE -and $_.ExtendedRights -like "*Send-As*" -and $_.User -notlike "*NT AUTHORITY\SELF*"} | % {
    
        $SendAsHash.Add((Get-Recipient $_.User).DisplayName.ToString(), $true)
        $AllTrustees += (Get-Recipient $_.User).DisplayName.ToString()

    }#end mbxperm
    #>

    foreach($UniqueTrustee in $AllTrustees | Select-Object -Unique)
    {
        $OutputArray += [PSCustomObject]@{
                'Mailbox' = $Mailbox.Alias.ToString();
                'Trustee' = $UniqueTrustee;
                'FullAccess' = $FullAccessHash.ContainsKey($UniqueTrustee);
                #'SendAs' = $SendAsHash.ContainsKey($UniqueTrustee);
                'SendOnBehalf' = $SendOnBehalfHash.ContainsKey($UniqueTrustee);
        }#end psobject

    }#end trustee loop

}

$OutputArray | Export-Csv C:\Scripts\Output\AllMailboxAccessRights.csv -NoTypeInformation 

