-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-EnabledProtocolReport.ps1
227 lines (176 loc) · 7.01 KB
/
Get-EnabledProtocolReport.ps1
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<#
.SYNOPSIS
Get a list of mailbox users having a selected client access protocol enabled
Thomas Stensitzki
THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE
RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
Version 1.0, 2019-12-08
Ideas, comments and suggestions to [email protected]
.LINK
http://scripts.Granikos.eu
.DESCRIPTION
This script gathers a list of enabled users for a selected Exchange Server client protocol.
The list of users is sent by email as HTML text in the email body or as an attached CSV file.
You can select to gather data for a single protocol or for all protocols.
.NOTES
Requirements
- Windows Server 2012 R2 or newer
- Exchange 2016+ Management Shell
- GlobalFunctions module ((http://scripts.granikos.eu)
Revision History
--------------------------------------------------------------------------------
1.0 Initial community release
.PARAMETER Protocol
The client access protocol to report on.
Options: All, POP, IMAP, ActiveSync
.PARAMETER ExportCsv
Switch to export the result set of users as CSV file and attach the file to email
.PARAMETER UserDetailsInEmailBody
Switch to include the result set of users in the mail body
.PARAMETER SendMail
Switch to send a report email
.PARAMETER MailFrom
Email address of report sender
.PARAMETER MailTo
Email address of report recipient
.PARAMETER MailServer
SMTP Server for email report
.EXAMPLE
.\Get-EnabledProtocolReport.ps1 -SendMail -MailFrom [email protected] -MailTo [email protected] -MailServer relay.varunagroup.de -Protocol ALL -ExportCsv
Find users having all protocols enabled, create a CSV file per protocol and send an email with CSV attachments
.EXAMPLE
.\Get-EnabledProtocolReport.ps1 -Protocol ALL -ExportCsv
Find users having all protocols enabled, create a CSV file per protocol only
#>
[CmdletBinding()]
Param(
[ValidateSet('All','POP','IMAP','ActiveSync')]
[string]$Protocol = 'All',
[switch]$ExportCsv,
[switch]$UserDetailsInEmailBody,
[switch]$SendMail,
[string]$MailFrom = '',
[string]$MailTo = '',
[string]$MailServer = ''
)
Add-Type -AssemblyName System.Web
# Import GlobalFunctions
if($null -ne (Get-Module -Name GlobalFunctions -ListAvailable).Version) {
Import-Module -Name GlobalFunctions
}
else {
Write-Warning -Message 'Unable to load GlobalFunctions PowerShell module.'
Write-Warning -Message 'Open an administrative PowerShell session and run Import-Module GlobalFunctions'
Write-Warning -Message 'Please check http://bit.ly/GlobalFunctions for further instructions'
exit
}
$ScriptDir = Split-Path -Path $script:MyInvocation.MyCommand.Path
$ScriptName = $MyInvocation.MyCommand.Name
$FileTimeStamp = $(Get-Date -Format 'yyyy-MM-dd HHmm')
$global:Files =@()
$logger = New-Logger -ScriptRoot $ScriptDir -ScriptName $ScriptName -LogFileRetention 30
$logger.Purge()
$logger.Write('Script started')
Function Test-SendMail {
if( ($MailFrom -ne '') -and ($MailTo -ne '') -and ($MailServer -ne '') ) {
return $true
}
else {
return $false
}
}
function Get-CASMailboxForProtocol {
[CmdletBinding()]
param(
[string]$Protocol
)
Write-Verbose -Message ('Fetching user mailboxes for {0}' -f $Protocol)
$htmlResult = ('<h3>{0}</h3>' -f $Protocol)
switch($Protocol) {
'POP' {
$result = Get-CASMailbox -ResultSize Unlimited | Where-Object{$_.PopEnabled -eq $true} | Select-Object -Property DisplayName,PrimarySmtpAddress | Sort-Object -Property DisplayName
}
'IMAP' {
$result = Get-CASMailbox -ResultSize Unlimited | Where-Object{$_.ImapEnabled -eq $true} | Select-Object -Property DisplayName,PrimarySmtpAddress | Sort-Object -Property DisplayName
}
'ActiveSync' {
$result = Get-CASMailbox -ResultSize Unlimited | Where-Object{$_.ActiveSyncEnabled -eq $true} | Select-Object -Property DisplayName,PrimarySmtpAddress | Sort-Object -Property DisplayName
}
}
try{
$count = ($result | Measure-Object).Count
$text = ('Found {0} mailboxes for {1} protocol.' -f $count, $Protocol)
$logger.Write($text)
Write-Verbose -Message $text
$htmlResult += ('<p>{0}</p>' -f ('Found <strong>{0}</strong> mailboxes for <strong>{1}</strong> protocol.' -f $count, $Protocol))
if($ExportCsv) {
$fileName = (Join-Path -Path $ScriptDir -ChildPath ('{0}-{1}.csv' -f $Protocol, $FileTimeStamp))
$result | Export-Csv -Path $fileName -NoTypeInformation -Encoding UTF8 -Delimiter ';' -Force -Confirm:$false
if(Test-Path -Path $fileName){
Write-Verbose -Message ('File exists: {0}' -f $fileName)
$global:Files += $fileName
}
}
if($UserDetailsInEmailBody) {
$htmlResult += '<table>'
foreach ($object in $result) {
$htmlResult += ('<tr><td>{0}</td><td>{1}</td></tr>' -f $object.DisplayName, $object.PrimarySmtpAddress)
}
$htmlResult += '</table><hr />'
}
}
catch {}
$htmlResult
}
# Main -----------------------------------------------------
If ($SendMail.IsPresent) {
If (-Not (Test-SendMail)) {
Throw 'If -SendMail specified, -MailFrom, -MailTo and -MailServer must be specified as well!'
}
}
### MAIN ###################################
# View entire Active Directory forest
Set-ADServerSettings -ViewEntireForest $true
$message = ('Fetching CAS mailboxes for {0} protocol(s).' -f ($Protocol))
$logger.Write($message)
Write-Verbose -Message $message
# Prepare Output
$Output = '<html>
<body>
<font size=""1"" face=""Arial,sans-serif"">'
switch($Protocol) {
'POP' {
$Output += Get-CASMailboxForProtocol -Protocol 'POP'
}
'IMAP' {
$Output += Get-CASMailboxForProtocol -Protocol 'IMAP'
}
'ActiveSync' {
$Output += Get-CASMailboxForProtocol -Protocol 'ActiveSync'
}
'ALL' {
$Output += Get-CASMailboxForProtocol -Protocol 'POP'
$Output += Get-CASMailboxForProtocol -Protocol 'IMAP'
$Output += Get-CASMailboxForProtocol -Protocol 'ActiveSync'
}
}
$Output += '</font></body></html>'
$Body = [Web.HttpUtility]::HtmlDecode($Output)
if($SendMail) {
try {
if($ExportCsv) {
$logger.Write(('Sending email with attachments to {0}' -f $MailTo))
Send-MailMessage -Encoding utf8 -From $MailFrom -To $MailTo -Subject ('Get-EnabledProtocolReport - {0}' -f $Protocol) -SmtpServer $MailServer -BodyAsHtml -Body $Body -Attachments $global:Files
}
else {
$logger.Write(('Sending email to {0}' -f $MailTo))
Send-MailMessage -Encoding utf8 -From $MailFrom -To $MailTo -Subject ('Get-EnabledProtocolReport - {0}' -f $Protocol) -SmtpServer $MailServer -BodyAsHtml -Body $Body
}
}
catch {
$logger.Write(('Error sending email to {0}' -f $MailTo),3)
}
}
$message = 'Script finished.'
$logger.Write($message)
Write-Verbose -Message $message