-
Notifications
You must be signed in to change notification settings - Fork 106
/
ExchangeDailyCheckList.ps1
511 lines (414 loc) · 20.5 KB
/
ExchangeDailyCheckList.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<#PSScriptInfo
.VERSION 2.0
.GUID d898fc0f-63ad-4d28-98e9-3579b6dd9475
.AUTHOR Vikas Sukhija
.COMPANYNAME TechWizard.cloud
.COPYRIGHT TechWizard.cloud
.TAGS
.LICENSEURI
.PROJECTURI https://techwizard.cloud/
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
https://techwizard.cloud
.PRIVATEDATA
#>
<#
.DESCRIPTION
Exchange Health Check - Tested on 2016
#############################################################################
# Author: Mahesh Sharma /Vikas Sukhija
# Reviewer: Vikas SUkhija
# Date: 06/10/2013
# Modified:06/19/2013 - made it to run from any path
# Modified:02/09/2014 - started modifying it for exchange 2010
# Modified:02/18/2014 - modified to include all mailox servers in test mailflow
# Modified:05/22/2014 - added activation prefrence
# Modified:09/09/2014 - included DAG DB backups status
# Modified:04/15/2015 - updated for Exchange 2013
# Modified:07/12/2015 - Updated to show yellow indicators if queue length increases 50
# Modified:08/28/2020 - Updated for 2016, Alert Yes No, Parameterized
# Modified:09/02/2020 - Updated to add alerts aon queues and added indicators
# Description: Exchange Health Status
#############################################################################
#>
param (
[Parameter(Mandatory = $true, HelpMessage = "Please Enter Exchange Server FQDN")]
[string]$Exserver,
[Parameter(Mandatory = $true, HelpMessage = "Please Enter SMTP Relay Server")]
[string]$smtpserver,
[Parameter(Mandatory = $true, HelpMessage = "Please Enter From Address")]
[string]$from,
[Parameter(Mandatory = $true, HelpMessage = "Add receipient email address")]
[String[]]$to,
[Parameter(Mandatory = $true, HelpMessage = "Enter Report or Alert")]
[ValidateSet('Report','Alert')]
[string]$Action,
[Parameter(Mandatory = $false)]
[int]$logrecyclelimit = "60",
[Parameter(Mandatory = $false)]
[int]$backupmonitorhours = "24",
[Parameter(Mandatory = $false)]
[int]$QueueMonitor = "100"
)
###########################Functions##########################################
function New-FolderCreation
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[string]$foldername
)
$logpath = (Get-Location).path + "\" + "$foldername"
$testlogpath = Test-Path -Path $logpath
if($testlogpath -eq $false)
{
#Start-ProgressBar -Title "Creating $foldername folder" -Timer 10
$null = New-Item -Path (Get-Location).path -Name $foldername -Type directory
}
}
function Write-Log
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true,ParameterSetName = 'Create')]
[array]$Name,
[Parameter(Mandatory = $true,ParameterSetName = 'Create')]
[string]$Ext,
[Parameter(Mandatory = $true,ParameterSetName = 'Create')]
[string]$folder,
[Parameter(ParameterSetName = 'Create',Position = 0)][switch]$Create,
[Parameter(Mandatory = $true,ParameterSetName = 'Message')]
[String]$message,
[Parameter(Mandatory = $true,ParameterSetName = 'Message')]
[String]$path,
[Parameter(Mandatory = $false,ParameterSetName = 'Message')]
[ValidateSet('Information','Warning','Error')]
[string]$Severity = 'Information',
[Parameter(ParameterSetName = 'Message',Position = 0)][Switch]$MSG
)
switch ($PsCmdlet.ParameterSetName) {
"Create"
{
$log = @()
$date1 = Get-Date -Format d
$date1 = $date1.ToString().Replace("/", "-")
$time = Get-Date -Format t
$time = $time.ToString().Replace(":", "-")
$time = $time.ToString().Replace(" ", "")
New-FolderCreation -foldername $folder
foreach ($n in $Name)
{$log += (Get-Location).Path + "\" + $folder + "\" + $n + "_" + $date1 + "_" + $time + "_.$Ext"}
return $log
}
"Message"
{
$date = Get-Date
$concatmessage = "|$date" + "| |" + $message +"| |" + "$Severity|"
switch($Severity){
"Information"{Write-Host -Object $concatmessage -ForegroundColor Green}
"Warning"{Write-Host -Object $concatmessage -ForegroundColor Yellow}
"Error"{Write-Host -Object $concatmessage -ForegroundColor Red}
}
Add-Content -Path $path -Value $concatmessage
}
}
} #Function Write-Log
#################connect exchange###########################################
$psurl = "http://" + $Exserver + "/" + "PowerShell"
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $psurl -Authentication Kerberos
Import-PSSession $Session -AllowClobber
###########################Define Variables#################################
$report = Write-Log -Name 2016Report -Ext htm -folder report
$hrs = (Get-Date).Addhours(-$backupmonitorhours)
###############################HTml Report Content##########################
Add-Content $report "<html>"
Add-Content $report "<head>"
Add-Content $report "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $report '<title>Exchange Status Report</title>'
Add-Content $report '<STYLE TYPE="text/css">'
Add-Content $report "<!--"
Add-Content $report "td {"
Add-Content $report "font-family: Tahoma;"
Add-Content $report "font-size: 11px;"
Add-Content $report "border-top: 1px solid #999999;"
Add-Content $report "border-right: 1px solid #999999;"
Add-Content $report "border-bottom: 1px solid #999999;"
Add-Content $report "border-left: 1px solid #999999;"
Add-Content $report "padding-top: 0px;"
Add-Content $report "padding-right: 0px;"
Add-Content $report "padding-bottom: 0px;"
Add-Content $report "padding-left: 0px;"
Add-Content $report "}"
Add-Content $report "body {"
Add-Content $report "margin-left: 5px;"
Add-Content $report "margin-top: 5px;"
Add-Content $report "margin-right: 0px;"
Add-Content $report "margin-bottom: 10px;"
Add-Content $report ""
Add-Content $report "table {"
Add-Content $report "border: thin solid #000000;"
Add-Content $report "}"
Add-Content $report "-->"
Add-Content $report "</style>"
Add-Content $report "</head>"
Add-Content $report "<body>"
Add-Content $report "<table width='100%'>"
Add-Content $report "<tr bgcolor='Lavender'>"
Add-Content $report "<td colspan='7' height='25' align='center'>"
Add-Content $report "<font face='tahoma' color='#003399' size='4'><strong>DAG Active Manager</strong></font>"
Add-Content $report "</td>"
Add-Content $report "</tr>"
Add-Content $report "</table>"
Add-Content $report "<table width='100%'>"
Add-Content $report "<tr bgcolor='IndianRed'>"
Add-Content $report "<td width='10%' align='center'><B>Identity</B></td>"
Add-Content $report "<td width='5%' align='center'><B>PrimaryActiveManager</B></td>"
Add-Content $report "<td width='20%' align='center'><B>OperationalMachines</B></td>"
Add-Content $report "</tr>"
##############################Get ALL DAG's##################################
$inputdag = @()
$indag = Get-DatabaseAvailabilityGroup
foreach($dg in $indag)
{
$mem = $dg.Servers
foreach($m in $mem)
{
if((Get-ExchangeServer $m.Name).AdminDisplayVersion -like "*15.1*"){$inputdag += $dg.Name}
}
}
$inputdag = $inputdag | Select-Object -uniq
########################################################################################################
########################################################################################################
$dagList = $inputdag
$TestMailFlow = Get-ExchangeServer | Where-Object{$_.ServerRole -like "*Mailbox*"}
########################################################################################################
##############################################Check PAM#################################################
foreach ($dag in $dagList)
{
$FullStatus = Get-DatabaseAvailabilityGroup -Status $dag
Foreach ($status in $FullStatus)
{
$Identity = $status.identity
$PrimaryActiveManager = $status.PrimaryActiveManager
$Servers = $status.Servers
Add-Content $report "<tr>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B> $Identity</B></td>"
Add-Content $report "<td bgcolor= 'Aquamarine' align=center> <B>$PrimaryActiveManager</B></td>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$Servers</B></td>"
Add-Content $report "</tr>"
}
}
##################################################################################################################
############################################## Mailbox Database Status ###########################################
Add-Content $report "<tr bgcolor='Lavender'>"
Add-Content $report "<td colspan='7' height='25' align='center'>"
Add-Content $report "<font face='tahoma' color='#003399' size='4'><strong>Mailbox Database Status</strong></font>"
Add-Content $report "</td>"
Add-Content $report "</tr>"
Add-Content $report "</tr>"
Add-Content $report "</table>"
Add-Content $report "<table width='100%'>"
Add-Content $report "<tr bgcolor='IndianRed'>"
Add-Content $report "<td width='25%' align='center'><B>databaseName</B></td>"
Add-Content $report "<td width='25%' align='center'><B>Status</B></td>"
Add-Content $report "<td width='25%' align='center'><B>ActiveCopy</B></td>"
Add-Content $report "<td width='25%' align='center'><B>CopyQueuelength</B></td>"
Add-Content $report "<td width='25%' align='center'><B>ReplayQueueLength</B></td>"
Add-Content $report "<td width='25%' align='center'><B>LastInspectedLogTime</B></td>"
Add-Content $report "<td width='25%' align='center'><B>ContentIndexState</B></td>"
Add-Content $report "</tr>"
$mbxdb = Get-MailboxDatabase | Get-MailboxDatabaseCopyStatus
$mbxdb = $mbxdb | Sort-Object Status -Descending
foreach ($db in $mbxdb)
{
$dbname = $db.name
foreach($dbn in $dbname)
{
$stcopy = Get-MailboxDatabaseCopyStatus $dbn
$acpref = $stcopy.ActivationPreference
}
$server = $db.Mailboxserver
$status = $db.Status
$ActiveCopy = $db.ActiveCopy
$CopyQueuelength = $db.CopyQueuelength
$ReplayQueueLength = $db.ReplayQueueLength
$LastInspectedLogTime = $db.LastInspectedLogTime
$ContentIndexState = $db.ContentIndexState
Add-Content $report "<tr>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$dbname</B></td>"
if ((($status -eq "Mounted") -and ($acpref -eq 1)) -or ($status -eq "Healthy"))
{Add-Content $report "<td bgcolor= 'Aquamarine' align=center> <B>$status</B></td>"}
elseif((($status -eq "Mounted") -and ($acpref -ne 1)) -or ($status -eq "Healthy"))
{Add-Content $report "<td bgcolor= 'yellow' align=center> <B>$status</B></td>"}
else
{
Add-Content $report "<td bgcolor= 'Red' align=center> <B>$status</B></td>"
if($Action -eq "Alert") { Send-MailMessage -SmtpServer $smtpserver -From $from -To $to -Subject "Open Critical - $server $dbname status $status"}
}
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$ActiveCopy</B></td>"
if ($CopyQueuelength -le "50")
{Add-Content $report "<td bgcolor= 'Aquamarine' align=center> <B>$CopyQueuelength</B></td>"}
else
{Add-Content $report "<td bgcolor= 'Yellow' align=center> <B>$CopyQueuelength</B></td>"
if($Action -eq "Alert") { Send-MailMessage -SmtpServer $smtpserver -From $from -To $to -Subject "Open Critical - $server $dbname Copy Queue $CopyQueuelength"}
}
if ($ReplayQueueLength -le "50")
{Add-Content $report "<td bgcolor= 'Aquamarine' align=center> <B>$ReplayQueueLength</B></td>"}
else
{Add-Content $report "<td bgcolor= 'Yellow' align=center> <B>$ReplayQueueLength</B></td>"
if($Action -eq "Alert") { Send-MailMessage -SmtpServer $smtpserver -From $from -To $to -Subject "Open Critical - $server $dbname Replay Queue $ReplayQueueLength"}
}
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$LastInspectedLogTime</B></td>"
if ($ContentIndexState -eq "Healthy")
{Add-Content $report "<td bgcolor= 'Aquamarine' align=center> <B>$ContentIndexState</B></td>"}
else
{Add-Content $report "<td bgcolor= 'Red' align=center> <B>$ContentIndexState</B></td>"
if($Action -eq "Alert") { Send-MailMessage -SmtpServer $smtpserver -From $from -To $to -Subject "Open Critical - $server $dbname Index $ContentIndexState"}
}
Add-Content $report "</tr>"
}
#################################################################################################################
##############################################DAG DB Backup Status###############################################
Add-Content $report "<tr bgcolor='Lavender'>"
Add-Content $report "<td colspan='7' height='25' align='center'>"
Add-Content $report "<font face='tahoma' color='#003399' size='4'><strong>DAG Database Backup Status</strong></font>"
Add-Content $report "</td>"
Add-Content $report "</tr>"
Add-Content $report "</tr>"
Add-Content $report "</table>"
Add-Content $report "<table width='100%'>"
Add-Content $report "<tr bgcolor='IndianRed'>"
Add-Content $report "<td width='10%' align='center'><B>Database</B></td>"
Add-Content $report "<td width='5%' align='center'><B>BackupInProgress</B></td>"
Add-Content $report "<td width='10%' align='center'><B>SnapshotLastFullBackup</B></td>"
Add-Content $report "<td width='5%' align='center'><B>SnapshotLastCopyBackup</B></td>"
Add-Content $report "<td width='10%' align='center'><B>LastFullBackup</B></td>"
Add-Content $report "<td width='5%' align='center'><B>RetainDeletedItemsUntilBackup</B></td>"
Add-Content $report "</tr>"
$dbst = Get-MailboxDatabase | Where-Object{$_.MasterType -like "DatabaseAvailabilityGroup"}
$dbst | ForEach-Object{
$st = Get-MailboxDatabase $_.Name -status
$dbname = $st.Name
$dbbkprg = $st.BackupInProgress
$dbsnpl = $st.SnapshotLastFullBackup
$dbsnplc = $st.SnapshotLastCopyBackup
$dblfb = $st.LastFullBackup
$dbrd = $st.RetainDeletedItemsUntilBackup
Add-Content $report "<tr>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$dbname</B></td>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$dbbkprg</B></td>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$dbsnpl</B></td>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$dbsnplc</B></td>"
if($dblfb -lt $hrs)
{Add-Content $report "<td bgcolor= 'Red' align=center> <B>$dblfb</B></td>"
if($Action -eq "Alert") { Send-MailMessage -SmtpServer $smtpserver -From $from -To $to -Subject "Open Critical - $dbname Backup Unhealthy"}
}
else
{Add-Content $report "<td bgcolor= 'Aquamarine' align=center> <B>$dblfb</B></td>"}
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$dbrd</B></td>"
Add-Content $report "</tr>"
}
##################################################################################################################
############################################## Test mail Flow For DAG ############################################
Add-Content $report "<tr bgcolor='Lavender'>"
Add-Content $report "<td colspan='7' height='25' align='center'>"
Add-Content $report "<font face='tahoma' color='#003399' size='4'><strong>Mail Flow Test Report</strong></font>"
Add-Content $report "</td>"
Add-Content $report "</tr>"
Add-Content $report "</tr>"
Add-Content $report "</table>"
Add-Content $report "<table width='100%'>"
Add-Content $report "<tr bgcolor='IndianRed'>"
Add-Content $report "<td width='25%' align='center'><B>Server</B></td>"
Add-Content $report "<td width='25%' align='center'><B>Result</B></td>"
Add-Content $report "<td width='25%' align='center'><B>Message Latency Time</B></td>"
Add-Content $report "<td width='25%' align='center'><B>IsRemoteTest</B></td>"
Add-Content $report "</tr>"
Foreach ($server in $TestMailFlow)
{
$server = $server.Name
Write-Host "Test Mail flow...$server"
$url = "http://" + $server + "/" + "Powershell"
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $url
$flow = Invoke-Command -Session $Session {Test-Mailflow}
#test-mailflow $server
if($flow -ne $null)
{
$result = $flow.TestMailflowResult
$time = $flow.MessageLatencyTime
$remote = $flow.IsRemoteTest
Add-Content $report "<tr>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$server</B></td>"
if ($result -eq "Success")
{Add-Content $report "<td bgcolor= 'Aquamarine' align=center> <B> $result</B></td>"}
else
{Add-Content $report "<td bgcolor= 'Red' align=center> <B> $result</B></td>"
if($Action -eq "Alert") { Send-MailMessage -SmtpServer $smtpserver -From $from -To $to -Subject "Open Critical - $server MailFlow Test failed"}
}
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$time</B></td>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$remote</B></td>"
Add-Content $report "</tr>"
}
}
##################################################################################################################
############################################## Get Queue For HUB Servers ############################################
Add-Content $report "<tr bgcolor='Lavender'>"
Add-Content $report "<td colspan='7' height='25' align='center'>"
Add-Content $report "<font face='tahoma' color='#003399' size='4'><strong>Mail Queue Status</strong></font>"
Add-Content $report "</td>"
Add-Content $report "</tr>"
Add-Content $report "</tr>"
Add-Content $report "</table>"
Add-Content $report "<table width='100%'>"
Add-Content $report "<tr bgcolor='IndianRed'>"
Add-Content $report "<td width='10%' align='center'><B>Identity</B></td>"
Add-Content $report "<td width='10%' align='center'><B>Delivery Type</B></td>"
Add-Content $report "<td width='5%' align='center'><B>Status</B></td>"
Add-Content $report "<td width='5%' align='center'><B>Message Count</B></td>"
Add-Content $report "<td width='10%' align='center'><B>Next Hop Domain</B></td>"
Add-Content $report "</tr>"
$GetHub = Get-TransportService
foreach ($hub in $GetHub){
$server = $hub.Name
Write-Host "Get Queue...$server"
$url = "http://" + $server + "/" + "Powershell"
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $url
$queues = Invoke-Command -Session $Session {get-queue}
foreach ($queue in $queues){
$Identity = $Queue.Identity
$DeliveryType = $Queue.DeliveryType
$status = $Queue.Status
$MSgCount = $Queue.Messagecount
$NextHopDomain = $Queue.NextHopDomain
Add-Content $report "<tr>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B> $Identity</B></td>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$DeliveryType</B></td>"
if(($DeliveryType -notlike "*Shadow*") -and ($MSgCount -gt $QueueMonitor)){
Add-Content $report "<td bgcolor= 'yellow' align=center> <B>$status</B></td>"
Add-Content $report "<td bgcolor= 'yellow' align=center> <B>$MSgCount</B></td>"
if($Action -eq "Alert") { Send-MailMessage -SmtpServer $smtpserver -From $from -To $to -Subject "Open Critical - $Identity Count is $MSgCount"}
}else{
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$status</B></td>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$MSgCount</B></td>"
}
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$NextHopDomain</B></td>"
}
Add-Content $report "</tr>"
}
##################################################################################################################
Get-PSSession | where{$_.ConfigurationName -eq "Microsoft.Exchange"} | Remove-PSSession
###########################################################################################################################
######################################################### Send Mail #######################################################
Add-Content $report "</table>"
Add-Content $report "</body>"
Add-Content $report "</html>"
if($Action -eq "Report"){
$body = Get-Content $report
Send-MailMessage -SmtpServer $smtpserver -From $from -To $to -Subject "Exchange Status Check Report" -Body ($body|out-string) -BodyAsHtml
}
###################################################Exchange Test Complete##################################################