How to Export Exchange Delegate information, Send-as, Full Access to email HTML or CSV REPORT

How to Export Exchange Delegate information, Send-as, Full Access to email HTML or CSV REPORT

You can change the list of users fed into the first foreach loop to increase the size of the report

examples:

Dump users:

get-user | foreach { #get all users in active directory and send them into the report

Import from excel file CSV:

#Gets a list from a csv file (excel) the file must contain all the fields required in the script.

#Fields: samaccountname, DistinguishedName

import-csv "~\desktop\employees.csv" | foreach {

Import-Module ActiveDirectory
add-PSSnapin  quest.activeroles.admanagement
function do_mail ($myhtml) {
 $mymail = new-object Net.Mail.MailMessage
 $smtp = new-object Net.Mail.SmtpClient("mailrelay.Company.com") #Dns name or IP
 $mymail.IsBodyHTML = $true
 $mymail.From = "ServerReporting@Company.com"
 $mymail.To.Add("ServerAdmins@Company.com")
 $mymail.Subject = ("Exchange Account permissions Report")
 $mymail.Body = $myhtml 
 #$mymail.Attachments.Add($attachmentpathandfilename)
 $smtp.Send($mymail)
} #do_mail
$myarray = @()
get-user helpdesk | foreach {
$myuser = $null
$myuser = "" | select name,MYdelegates,delegateOF,sendAS,Full
$delegates = Get-QADuser $_.samaccountname -IncludedProperties publicdelegates,publicdelegatesbl | select publicdelegates,publicdelegatesbl
$sendas = Get-ADPermission -Identity $_.DistinguishedName | ?{$_.ExtendedRights -like "Send-As" -and $_.User -like "*DOMAIN\*" -and $_.User -notlike "DOMAIN\BESAdmin" -and $_.User -notlike "DOMAIN\UnityMsgSvc" -and $_.User -notlike "*DOMAIN ADMIN*" -and $_.User -notlike "*Enterprise*" } 
$fullaccess = Get-MailboxPermission $_.samaccountname | ?{$_.AccessRights -eq "FullAccess" -and $_.IsInherited -eq $false -and $_.User -notlike "*DOMAIN*" -and $_.User -notlike "*Enterprise*" -and $_.User -like "*DOMAIN\*"} 
#cleanup and make reader friendly 
foreach ($row in $sendas) { $myuser.sendAS += "" + $row.User + " | " }
foreach ($row in $fullaccess) { $myuser.Full += "" + $row.User + " | " }
foreach ($row in $delegates.publicDelegates) { $myuser.MYdelegates += "" + $row.split(",")[1] + " , " + (($row.split(",")[0]).split("=")[1]).split("\")[0] + " | " }
foreach ($row in $delegates.publicdelegatesbl) { $myuser.delegateOF += "" + $row.split(",")[1] + " , " + (($row.split(",")[0]).split("=")[1]).split("\")[0] + " | " }
$myuser.name = $_.samaccountname
$delegates = $null
$sendas = $null
$fullaccess = $null
$myarray += $myuser
}
#make the email and send
 $head = "<style>"
 $head = $head + "BODY{background-color:#9FAEB5;}"
 $head = $head + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
 $head = $head + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#999999}"
 $head = $head + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#CCCCCC}"
 $head = $head + "</style>"
$myhtml = $myarray | ConvertTo-HTML -head $head -body ("<H2>Exchange Account permissions Report</H2>")
do_mail $myhtml