Wednesday 1 April 2015

Microsoft: Exchange Report based on Display Name, Account Status, Memberof and Hide from Exchange Address Lists

The following Powershell script allows you to generate an Exchange report based on the display name, account status, memberof and hide from Exchange address lists:

param(
[Parameter(ParameterSetName='file')] [string]$file,
[Parameter(ParameterSetName='server')] [string]$server,
[Parameter(ParameterSetName='mailbox')] [string]$mailbox,
[Parameter(ParameterSetName='all')] [switch]$all,
[string]$filename
)


$ErrorActionPreference = "SilentlyContinue"
$WarningPreference = "SilentlyContinue"
$report = @()


#Set recipient scope
$2007snapin = Get-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.Admin
if ($2007snapin)
{
$AdminSessionADSettings.ViewEntireForest = 1
}
else
{
$2010snapin = Get-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.E2010
if ($2010snapin)
{
Set-ADServerSettings -ViewEntireForest $true
}
}


#If no filename specified, generate report file name with random strings for uniqueness

if ($filename)
{
$reportfile = $filename
}
else
{
$timestamp = Get-Date -UFormat %Y%m%d-%H%M
$random = -join(48..57+65..90+97..122 | ForEach-Object {[char]$_} | Get-Random -Count 6)
$reportfile = "MailboxReport-$timestamp-$random.csv"
}


#Add dependencies
Import-Module ActiveDirectory

#Get the mailbox list

Write-Host -ForegroundColor White "Collecting mailbox list"

if($all) { $mailboxes = @(Get-Mailbox -resultsize unlimited -IgnoreDefaultScope) }

if($server) { $mailboxes = @(Get-Mailbox -server $server -resultsize unlimited -IgnoreDefaultScope) }

if($hidden){ $mailboxes = @(Get-Mailbox -hiddenfromaddresslistsenabled $hidden -resultsize unlimited -IgnoreDefaultScope) }

if($file) { $mailboxes = @(Get-Content $file | Get-Mailbox -resultsize unlimited) }

if($mailbox) { $mailboxes = @(Get-Mailbox $mailbox) }

#Get the report

Write-Host -ForegroundColor White "Collecting report data"

$mailboxcount = $mailboxes.count
$i = 0

#Loop through mailbox list and find the aged mailboxes
foreach ($mb in $mailboxes)
{
$i = $i + 1
$pct = $i/$mailboxcount * 100
Write-Progress -Activity "Collecting mailbox details" -Status "Processing mailbox $i of $mailboxcount - $mb" -PercentComplete $pct

$user = Get-User $mb
$aduser = Get-ADUser $mb.samaccountname -Properties Enabled,AccountExpirationDate
$aduserr = Get-ADUser $mb.samaccountname -Properties *
$member = ($aduserr.memberof | % { (Get-ADGroup $_).Name; }) -join ';'

#Create a custom PS object to aggregate the data we're interested in

$userObj = New-Object PSObject
$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $mb.DisplayName
$userObj | Add-Member NoteProperty -Name "Enabled" -Value $aduser.Enabled
$userObj | Add-Member NoteProperty -Name "MemberOf" -Value $member
$userObj | Add-Member NoteProperty -Name "Hide from Exchange" -Value $mb.HiddenFromAddressListsEnabled


#Add the object to the report
$report = $report += $userObj
}

#Catch zero item results
$reportcount = $report.count

if ($reportcount -eq 0)
{
Write-Host -ForegroundColor Yellow "No mailboxes were found matching that criteria."
}
else
{
#Output single mailbox report to console, otherwise output to CSV file
if ($mailbox)
{
$report | Format-List
}
else
{
$report | Export-Csv -Path $reportfile -NoTypeInformation
Write-Host -ForegroundColor White "Report written to $reportfile in current path."
Get-Item $reportfile
}
}

No comments:

Post a Comment