-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRecipientReportv5.ps1
More file actions
173 lines (145 loc) · 6.47 KB
/
RecipientReportv5.ps1
File metadata and controls
173 lines (145 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<#
.SYNOPSIS
Generates recipient address statistics and SMTP proxy address reports for Exchange on-premises.
.DESCRIPTION
Creates two report files on the desktop:
- TXT: Recipient address statistics (counts by type, accepted domain comparison)
- CSV: Complete listing of all recipients and their SMTP proxy addresses
Connects to an Exchange 2010/2013 server via remote PowerShell.
Run this from standard PowerShell, not Exchange Management Shell (EMS may cause issues
due to PSv2 in Exchange 2010).
.PARAMETER ExchangeFQDN
The fully qualified domain name of a Client Access Server to connect to.
.EXAMPLE
.\RecipientReportv5.ps1 server5.domain.local
Connects to the specified Exchange server and generates both report files on the desktop.
.NOTES
Author: Mike Crowley
https://mikecrowley.us
Requires: Exchange 2010 or 2013, PowerShell 3.0+
.LINK
https://github.com/Mike-Crowley/Public-Scripts
#>
[CmdletBinding()]
param(
[Parameter(
Position = 0,
Mandatory = $true,
ValueFromPipeline = $false,
HelpMessage = 'Type the name of a Client Access Server'
)]
[ValidateNotNullOrEmpty()]
[string]$ExchangeFQDN
)
if ($host.version.major -lt 3) {
Write-Host ""
Write-Host "This script requires PowerShell 3.0 or later." -ForegroundColor Red
Write-Host "Note: Exchange 2010's EMC always runs as version 2. Perhaps try launching PowerShell normally." -ForegroundColor Red
Write-Host ""
Write-Host "Exiting..." -ForegroundColor Red
Start-Sleep 3
Exit
}
if ((Test-Connection $ExchangeFQDN -Count 1 -Quiet) -ne $true) {
Write-Host ""
Write-Host ("Cannot connect to: " + $ExchangeFQDN) -ForegroundColor Red
Write-Host ""
Write-Host "Exiting..." -ForegroundColor Red
Start-Sleep 3
Exit
}
Clear-Host
# Misc variables
$ReportTimeStamp = Get-Date -Format 'yyyyMMdd_HHmmss'
$TxtFile = "$env:USERPROFILE\Desktop\" + $ReportTimeStamp + "_RecipientAddressReport_Part_1of2.txt"
$CsvFile = "$env:USERPROFILE\Desktop\" + $ReportTimeStamp + "_RecipientAddressReport_Part_2of2.csv"
# Connect to Exchange
Write-Host ("Connecting to " + $ExchangeFQDN + "...") -ForegroundColor Cyan
Get-PSSession | Where-Object { $_.ConfigurationName -eq 'Microsoft.Exchange' } | Remove-PSSession
$Session = @{
ConfigurationName = 'Microsoft.Exchange'
ConnectionUri = 'http://' + $ExchangeFQDN + '/PowerShell/?SerializationLevel=Full'
Authentication = 'Kerberos'
}
Import-PSSession (New-PSSession @Session)
# Get Data
Write-Host "Getting data from Exchange..." -ForegroundColor Cyan
$AcceptedDomains = Get-AcceptedDomain
$InScopeRecipients = @(
'DynamicDistributionGroup'
'UserMailbox'
'MailUniversalDistributionGroup'
'MailUniversalSecurityGroup'
'MailNonUniversalGroup'
'PublicFolder'
)
$AllRecipients = Get-Recipient -recipienttype $InScopeRecipients -ResultSize unlimited | Select-Object name, emailaddresses, RecipientType
$UniqueRecipientDomains = ($AllRecipients.emailaddresses | Where-Object { $_ -like 'smtp*' }) -split '@' | Where-Object { $_ -NotLike 'smtp:*' } | Select-Object -Unique
Write-Host "Preparing Output 1 of 2..." -ForegroundColor Cyan
# Output address stats
$TextBlock = @(
"Total Number of Recipients: " + $AllRecipients.Count
"Number of Dynamic Distribution Groups: " + ($AllRecipients | Where-Object { $_.RecipientType -eq 'DynamicDistributionGroup' }).Count
"Number of User Mailboxes: " + ($AllRecipients | Where-Object { $_.RecipientType -eq 'UserMailbox' }).Count
"Number of Mail-Universal Distribution Groups: " + ($AllRecipients | Where-Object { $_.RecipientType -eq 'MailUniversalDistributionGroup' }).Count
"Number of Mail-UniversalSecurity Groups: " + ($AllRecipients | Where-Object { $_.RecipientType -eq 'MailUniversalSecurityGroup' }).Count
"Number of Mail-NonUniversal Groups: " + ($AllRecipients | Where-Object { $_.RecipientType -eq 'MailNonUniversalGroup' }).Count
"Number of Public Folders: " + ($AllRecipients | Where-Object { $_.RecipientType -eq 'PublicFolder' }).Count
""
"Number of Accepted Domains: " + $AcceptedDomains.count
""
"Number of domains found on recipients: " + $UniqueRecipientDomains.count
""
$DomainComparrison = Compare-Object $AcceptedDomains.DomainName $UniqueRecipientDomains
"These domains have been assigned to recipients, but are not Accepted Domains in the Exchange Organization:"
($DomainComparrison | Where-Object { $_.SideIndicator -eq '=>' }).InputObject
""
"These Accepted Domains are not assigned to any recipients:"
($DomainComparrison | Where-Object { $_.SideIndicator -eq '<=' }).InputObject
""
"See this CSV for a complete listing of all addresses: " + $CsvFile
)
Write-Host "Preparing Output 2 of 2..." -ForegroundColor Cyan
$RecipientsAndSMTPProxies = @()
$CounterWatermark = 1
$AllRecipients | ForEach-Object {
# Create a new placeholder object
$RecipientOutputObject = New-Object PSObject -Property @{
Name = $_.Name
RecipientType = $_.RecipientType
SMTPAddress0 = ($_.emailaddresses | Where-Object { $_ -clike 'SMTP:*' } ) -replace "SMTP:"
}
# If applicable, get a list of other addresses for the recipient
if (($_.emailaddresses).count -gt '1') {
$OtherAddresses = @()
$OtherAddresses = ($_.emailaddresses | Where-Object { $_ -clike 'smtp:*' } ) -replace "smtp:"
$Counter = $OtherAddresses.count
if ($Counter -gt $CounterWatermark) { $CounterWatermark = $Counter }
$OtherAddresses | ForEach-Object {
$RecipientOutputObject | Add-Member -MemberType NoteProperty -Name (“SmtpAddress” + $Counter) -Value ($_ -replace "smtp:")
$Counter--
}
}
$RecipientsAndSMTPProxies += $RecipientOutputObject
}
$AttributeList = @(
'Name'
'RecipientType'
)
$AttributeList += 0..$CounterWatermark | ForEach-Object { "SMTPAddress" + $_ }
Write-Host "Saving report files to your desktop:" -ForegroundColor Green
Write-Host ""
Write-Host $TxtFile -ForegroundColor Green
Write-Host $CsvFile -ForegroundColor Green
try {
$TextBlock | Out-File $TxtFile -Encoding UTF8 -ErrorAction Stop
$RecipientsAndSMTPProxies | Select-Object $AttributeList | Sort-Object RecipientType, Name | Export-Csv $CsvFile -NoTypeInformation -Encoding UTF8 -ErrorAction Stop
}
catch {
Write-Host ""
Write-Host "Failed to save report files: $_" -ForegroundColor Red
Exit
}
Write-Host ""
Write-Host ""
Write-Host "Report Complete!" -ForegroundColor Green