Updated powershell exchange reporting

based on sendstats.ps1 , all the various reports as HTML formatted tables.

List mailboxes sorted by size

List mailboxes sorted by date last accessed.

List all email addresses assigned to mailboxes and public folders.

List of mailboxes being forwarded to other addresses.

List of rights to access of mailboxes

###
###
### to schedule this use PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1" -Command "getmailboxinformationandemail.ps1"
###
### Set the email addresses and server

Add-PSSnapin "Microsoft.Exchange.Management.PowerShell.Admin"

# set the from and to email addresses and other details
$FromAddress = "administrator@cortina.com"
$ToAddress = "jefft@cortina.com"
$MessageSubject = "Mailbox Size Report from $env:computername "
$MessageBody = "Attached is the current mailbox report."
## set the smtp servers name or address
$SendingServer = "smtp.local"

$body = @'
<style>
body { background-color:#dddddd;
       font-family:Tahoma;
       font-size:12pt; }
td, th { border:1px solid black; 
         border-collapse:collapse; }
th { color:white;
     background-color:black; }
table, tr, td, th { padding: 2px; margin: 0px }
table { margin-left:50px; }
</style>
'@

$body+="<h2>List of mailbox information</h2>"
#Get-MailboxDatabase | sort-object "server","storagegroup" | Select Server, StorageGroupName, Name, @{Name="Size (GB)";Expression={$objitem = (Get-MailboxDatabase $_.Identity); $path = "`\`\" + $objitem.server + "`\" + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $objItem.EdbFilePath.PathName.Remove(0,2); $size = ((Get-ChildItem $path).length)/1048576KB; [math]::round($size, 2)}}, @{Name="Size (MB)";Expression={$objitem = (Get-MailboxDatabase $_.Identity ) ; $path = "`\`\" + $objitem.server + "`\" + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $objItem.EdbFilePath.PathName.Remove(0,2); $size = ((Get-ChildItem $path).length)/1024KB; [math]::round($size, 2)}}, @{Name="No. Of Mbx";expression={(Get-Mailbox -Database $_.Identity -resultsize unlimited | Measure-Object).Count}} | Format-table -AutoSize
$body +=Get-MailboxDatabase | sort-object "server","storagegroup" | Select Server, StorageGroupName, Name, @{Name="Size (GB)";Expression={$objitem = (Get-MailboxDatabase $_.Identity); $path = "`\`\" + $objitem.server + "`\" + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $objItem.EdbFilePath.PathName.Remove(0,2); $size = ((Get-ChildItem $path).length)/1048576KB; [math]::round($size, 2)}}, @{Name="Size (MB)";Expression={$objitem = (Get-MailboxDatabase $_.Identity ) ; $path = "`\`\" + $objitem.server + "`\" + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $objItem.EdbFilePath.PathName.Remove(0,2); $size = ((Get-ChildItem $path).length)/1024KB; [math]::round($size, 2)}}, @{Name="No. Of Mbx";expression={(Get-Mailbox -Database $_.Identity -resultsize unlimited | Measure-Object).Count}}  | convertto-html -Fragment


foreach ($mbdatabase in Get-MailboxDatabase  | sort-object "server","storagegroup"  ) { 
$body += "<h3>Mailboxes in $mbdatabase.identity sorted by size</h3>" 
###Now get the stats and store in a text file
$body +=Get-MailboxStatistics -database $mbdatabase.identity  | Sort-Object    @{Expression="Database";Descending=$false}  ,@{Expression="TotalItemSize";Descending=$true}  | select database, DisplayName, lastlogontime ,@{label="TotalItemSize(MB)";expression={$_.TotalItemSize.Value.tomb()}}, ItemCount | convertto-html -fragment

$body +="<h2>Mailboxes in $mbdatabase.identity sorted by last logon (oldest first)</h2>" 
$body +=Get-MailboxStatistics -database $mbdatabase.identity  | Sort-Object @{Expression="Database";Descending=$false}  ,@{Expression="Lastlogontime";Descending=$false}  | select database, DisplayName, lastlogontime ,@{label="TotalItemSize(MB)";expression={$_.TotalItemSize.Value.tomb()}}, ItemCount |   convertto-html -fragment

}

$body+="<h2>Email addresses assigned to mailboxes</h2>"


$body += Get-Mailbox -ResultSize Unlimited |Select-Object DisplayName,ServerName,PrimarySmtpAddress, @{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq “smtp”} | ForEach-Object {$_.SmtpAddress}}} | convertto-html -Fragment

$body+="<h2>Email addresses assigned to public folders</h2>"

$body += get-mailpublicfolder  | select-object  displayname, WindowsEmailAddress, @{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq “smtp”}   | ForEach-Object {$_.SmtpAddress} } } | convertto-html -Fragment 

$body+="<h2>Forwarding addresses</h2>"

$fwds = get-mailbox | Where-Object { $_.ForwardingAddress -ne $null } | sort Name | select Name, ForwardingAddress

# now get the primary smtp adress of each forward address
foreach ($fwd in $fwds) {
$fwd | add-member -membertype noteproperty -name "ContactAddress" -value (get-Recipient $fwd.ForwardingAddress).PrimarySmtpAddress
}


$body +=  $fwds | convertto-html -Fragment 


$body+="<h2>Mailbox access permissions</h2>"

$body += Get-Mailbox  -ResultSize Unlimited | Get-MailboxPermission | where {$_.user.tostring() -ne "ausgold\nanotera" -and $_.user.tostring() -ne "NT AUTHORITY\SELF" -and $_.IsInherited -eq $false} | Select Identity,User,@{Name='Access Rights';Expression={[string]::join(', ', $_.AccessRights)}}  | convertto-html -Fragment 


$messageBody=$body 

###Create the mail message and add the statistics text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody 
$SMTPmessage.IsBodyHtml=$true

##$Attachment = New-Object Net.Mail.Attachment($tempfilename)
##$SMTPMessage.Attachments.Add($Attachment)


###Send the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Send($SMTPMessage)

remove-variable SMTPclient
remove-variable SMTPmessage
Remove-Variable attachment


About Jeff Turner

Technical director of Nano Tera Network Solutions.
This entry was posted in Powershell, VBS, VBA and other scripting., System Administration. Bookmark the permalink.