Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FUNCTION GetAllMailboxes-WhereFullAccessFor
- {
- PARAM(
- [Parameter(Mandatory = $true, Position = 0)]
- [ValidateNotNull()]
- [string]$Account,
- [Parameter(Mandatory = $true)]
- [ValidateScript({[System.IO.Directory]::Exists("$($_)")})]
- [string]$SaveToDir,
- [Parameter(Mandatory = $true)]
- [ValidateSet('CSV','HTML')]
- $ExportAs
- )
- TRY
- {
- Write-Host "NOTE: This may take some time to complete, please be patient.." -f Magenta
- $SU = Get-ADuser -Filter "Name -like '*$Account*'"
- IF($SU.Count -gt 1)
- {
- FUNCTION FoundMulti
- {
- Write-Host ''
- Write-Host "We Found Multiple Users That Match The Account: $Account" -f Yellow
- $ii = 0
- $SU | % {
- Write-Host "$ii. $_" -f Green
- $ii++
- }
- $RH = Read-Host "Please type corresponding number for listed accounts, and press enter."
- $theError = "Please Make A Valid Selection!"
- $theSuccess = "Thank you, now locating all accounts with full access for $($SU.SamAccountName[$RH])"
- IF( ($RH -lt 1) -or ($RH -gt $SU.Count) ){ Write-Host $theError -f Red; Pause; FoundMulti }
- IF( ($RH -ge 1) -and ($RH -le $SU.Count) ){ Write-Host $theSuccess -f Green; return $($SU.SamAccountName[$RH]) }
- }
- $Account = FoundMulti
- }
- IF($SU.Count -eq 1)
- {
- $Account = $($SU.SamAccountName)
- }
- # HTML styling for report that will be generated
- $Head = "<style>"
- $Head += "`r`nBODY{background-color:peachpuff;}"
- $Head += "`r`nTABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
- $Head += "`r`nTH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}"
- $Head += "`r`nTD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"
- $Head += "`r`n</style>"
- $uObj = @()
- FOREACH($Mbx in (Get-Mailbox -ResultSize Unlimited))
- {
- $FAU = Get-MailboxPermission $Mbx | ? {($_.AccessRights -like "*FullAccess*") -and (-not $_.Deny)}
- FOREACH($u in $FAU)
- {
- IF($u.User.RawIdentity -like "*\$Account*")
- {
- Write-Host "The account '$Account' has full access over mailbox '$Mbx'" -f Yellow
- $uProps = [array][pscustomobject] @{
- 'Account' = $u.User.RawIdentity
- 'HasFullAccessTo' = $Mbx
- }
- $uObj += $uProps
- }
- }
- }
- $tPreC = "<H2>$Account Has Full Access Over:</H2>"
- $rPostC = $uObj | sort Account,HasFullAccessTo | ConvertTo-Html -Fragment -PreContent $tPreC | Out-String
- $ExGo = $true
- }
- CATCH
- {
- $ExGo = $false
- $_
- }
- FINALLY
- {
- IF($ExGo -eq $true)
- {
- SWITCH($ExportAs)
- {
- 'HTML' {
- # Export to HTML, and open directory
- $HtmlSplat = @{
- 'Head'= $Head
- 'PostContent' = $rPostC
- 'PreContent' = “<H1>Mailbox Full Access Report</H1>”
- }
- $oPath = "$SaveToDir\User_FullAccessReport.htm"
- IF(Test-Path $oPath)
- {
- $r = Get-Random 99
- $oPath = "$SaveToDir\User_FullAccessReport$r.htm"
- ConvertTo-Html @HtmlSplat| Out-File "$oPath"
- }
- IF(!(Test-Path $oPath))
- {
- ConvertTo-Html @HtmlSplat| Out-File "$oPath"
- }
- }
- 'CSV' {
- $oPath = "$SaveToDir\User_FullAccessReport.csv"
- IF(Test-Path $oPath)
- {
- $r = Get-Random 99
- $oPath = "$SaveToDir\User_FullAccessReport$r.csv"
- $uObj | sort | Export-Csv -Path "$oPath" -Force -NoTypeInformation
- }
- IF(!(Test-Path $oPath))
- {
- $uObj | sort | Export-Csv -Path "$oPath" -Force -NoTypeInformation
- }
- }
- }
- Start $SaveToDir
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement