/
Test-ExchangeServerHealth.ps1
2153 lines (1889 loc) · 92.4 KB
/
Test-ExchangeServerHealth.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
Test-ExchangeServerHealth.ps1 - Exchange Server Health Check Script.
.DESCRIPTION
Performs a series of health checks on Exchange servers and DAGs
and outputs the results to screen, and optionally to log file, HTML report,
and HTML email.
Use the ignorelist.txt file to specify any servers, DAGs, or databases you
want the script to ignore (eg test/dev servers).
.OUTPUTS
Results are output to screen, as well as optional log file, HTML report, and HTML email
.PARAMETER Server
Perform a health check of a single server
.PARAMETER ReportMode
Set to $true to generate a HTML report. A default file name is used if none is specified.
.PARAMETER ReportFile
Allows you to specify a different HTML report file name than the default.
.PARAMETER SendEmail
Sends the HTML report via email using the SMTP configuration within the script.
.PARAMETER AlertsOnly
Only sends the email report if at least one error or warning was detected.
.PARAMETER Log
Writes a log file to help with troubleshooting.
.EXAMPLE
.\Test-ExchangeServerHealth.ps1
Checks all servers in the organization and outputs the results to the shell window.
.EXAMPLE
.\Test-ExchangeServerHealth.ps1 -Server HO-EX2010-MB1
Checks the server HO-EX2010-MB1 and outputs the results to the shell window.
.EXAMPLE
.\Test-ExchangeServerHealth.ps1 -ReportMode -SendEmail
Checks all servers in the organization, outputs the results to the shell window, a HTML report, and
emails the HTML report to the address configured in the script.
.LINK
https://practical365.com/exchange-server/powershell-script-exchange-server-health-check-report/
.NOTES
Written by: Paul Cunningham
Find me on:
* My Blog: http://paulcunningham.me
* Twitter: https://twitter.com/paulcunningham
* LinkedIn: http://au.linkedin.com/in/cunninghamp/
* Github: https://github.com/cunninghamp
For more Exchange Server tips, tricks and news
check out Exchange Server Pro.
* Website: https://practical365.com
* Twitter: https://twitter.com/practical365
Additional Credits (code contributions and testing):
- Chris Brown, http://twitter.com/chrisbrownie
- Ingmar Brückner
- John A. Eppright
- Jonas Borelius
- Thomas Helmdach
- Bruce McKay
- Tony Holdgate
- Ryan
- Rob Silver
- andrewcr7, https://github.com/andrewcr7
License:
The MIT License (MIT)
Copyright (c) 2017 Paul Cunningham
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Change Log
V1.00, 05/07/2012 - Initial version
V1.01, 05/08/2012 - Minor bug fixes and removed Edge Tranport checks
V1.02, 05/05/2013 - A lot of bug fixes, updated SMTP to use Send-MailMessage, added DAG health check.
V1.03, 04/08/2013 - Minor bug fixes
V1.04, 19/08/2013 - Added Exchange 2013 compatibility, added option to output a log file, converted many
sections of code to use pre-defined strings, fixed -AlertsOnly parameter, improved summary
sections of report to be more readable and include DAG summary
V1.05, 23/08/2013 - Added workaround for Test-ServiceHealth error for Exchange 2013 CAS-only servers
V1.06, 28/10/2013 - Added workaround for Test-Mailflow error for Exchange 2013 Mailbox servers.
- Added workaround for Exchange 2013 mail test.
- Added localization strings for service health check errors for non-English systems.
- Fixed an uptime calculation bug for some regional settings.
- Excluded recovery databases from active database calculation.
- Fixed bug where high transport queues would not count as an alert.
- Fixed error thrown when Site attribute can't be found for Exchange 2003 servers.
- Fixed bug causing Exchange 2003 servers to be added to the report twice.
V1.07, 24/11/2013 - Fixed bug where disabled content indexes were counted as failed.
V1.08, 29/06/2014 - Fixed bug with DAG reporting in mixed Exchange 2010/2013 orgs.
V1.09, 06/07/2014 - Fixed bug with DAG member replication health reporting for mixed Exchange 2010/2013 orgs.
V1.10, 19/08/2014 - Fixed bug with E14 replication health not testing correct server.
V1.11, 11/02/2015 - Added queue length to Transport queue result in report.
V1.12, 05/03/2015 - Fixed bug with color-coding in report for Transport Queue length.
V1.13, 07/03/2015 - Fixed bug with incorrect function name used sometimes when trying to call Write-LogFile
V1.14, 21/05/2015 - Fixed bug with color-coding in report for Transport Queue length on CAS-only Exchange 2013 servers.
V1.15, 18/11/2015 - Fixed bug with Exchange 2016 version detection.
V1.16, 13/04/2017 - Fixed bugs with recovery DB detection, invalid variables, shadow redundancy queues, and lagged copy detection.
V1.17, 17/05/2017 - Fixed bug with auto-suspended content index detection
#>
#requires -version 2
[CmdletBinding()]
param (
[Parameter( Mandatory=$false)]
[string]$Server,
[Parameter( Mandatory=$false)]
[string]$ServerList,
[Parameter( Mandatory=$false)]
[string]$ReportFile="exchangeserverhealth.html",
[Parameter( Mandatory=$false)]
[switch]$ReportMode,
[Parameter( Mandatory=$false)]
[switch]$SendEmail,
[Parameter( Mandatory=$false)]
[switch]$AlertsOnly,
[Parameter( Mandatory=$false)]
[switch]$Log
)
#...................................
# Variables
#...................................
$now = Get-Date #Used for timestamps
$date = $now.ToShortDateString() #Short date format for email message subject
[array]$exchangeservers = @() #Array for the Exchange server or servers to check
[int]$transportqueuehigh = 100 #Change this to set transport queue high threshold. Must be higher than warning threshold.
[int]$transportqueuewarn = 80 #Change this to set transport queue warning threshold. Must be lower than high threshold.
$mapitimeout = 10 #Timeout for each MAPI connectivity test, in seconds
$pass = "Green"
$warn = "Yellow"
$fail = "Red"
$ip = $null
[array]$serversummary = @() #Summary of issues found during server health checks
[array]$dagsummary = @() #Summary of issues found during DAG health checks
[array]$report = @()
[bool]$alerts = $false
[array]$dags = @() #Array for DAG health check
[array]$dagdatabases = @() #Array for DAG databases
[int]$replqueuewarning = 8 #Threshold to consider a replication queue unhealthy
$dagreportbody = $null
$myDir = Split-Path -Parent $MyInvocation.MyCommand.Path
#...................................
# Modify these Variables (optional)
#...................................
$reportemailsubject = "Exchange Server Health Report"
$ignorelistfile = "$myDir\ignorelist.txt"
$logfile = "$myDir\exchangeserverhealth.log"
#...................................
# Modify these Email Settings
#...................................
$smtpsettings = @{
To = "administrator@exchangeserverpro.net"
From = "exchangeserver@exchangeserverpro.net"
Subject = "$reportemailsubject - $now"
SmtpServer = "smtp.exchangeserverpro.net"
}
#...................................
# Modify these language
# localization strings.
#...................................
# The server roles must match the role names you see when you run Test-ServiceHealth.
$casrole = "Client Access Server Role"
$htrole = "Hub Transport Server Role"
$mbrole = "Mailbox Server Role"
$umrole = "Unified Messaging Server Role"
# This should match the word for "Success", or the result of a successful Test-MAPIConnectivity test
$success = "Success"
#...................................
# Logfile Strings
#...................................
$logstring0 = "====================================="
$logstring1 = " Exchange Server Health Check"
#...................................
# Initialization Strings
#...................................
$initstring0 = "Initializing..."
$initstring1 = "Loading the Exchange Server PowerShell snapin"
$initstring2 = "The Exchange Server PowerShell snapin did not load."
$initstring3 = "Setting scope to entire forest"
#...................................
# Error/Warning Strings
#...................................
$string0 = "Server is not an Exchange server. "
$string1 = "Server is not reachable. "
$string3 = "------ Checking"
$string4 = "Could not test service health. "
$string5 = "required services not running. "
$string6 = "Could not check queue. "
$string7 = "Public Folder database not mounted. "
$string8 = "Skipping Edge Transport server. "
$string9 = "Mailbox databases not mounted. "
$string10 = "MAPI tests failed. "
$string11 = "Mail flow test failed. "
$string12 = "No Exchange Server 2003 checks performed. "
$string13 = "Server not found in DNS. "
$string14 = "Sending email. "
$string15 = "Done."
$string16 = "------ Finishing"
$string17 = "Unable to retrieve uptime. "
$string18 = "Ping failed. "
$string19 = "No alerts found, and AlertsOnly switch was used. No email sent. "
$string20 = "You have specified a single server to check"
$string21 = "Couldn't find the server $server. Script will terminate."
$string22 = "The file $ignorelistfile could not be found. No servers, DAGs or databases will be ignored."
$string23 = "You have specified a filename containing a list of servers to check"
$string24 = "The file $serverlist could not be found. Script will terminate."
$string25 = "Retrieving server list"
$string26 = "Removing servers in ignorelist from server list"
$string27 = "Beginning the server health checks"
$string28 = "Servers, DAGs and databases to ignore:"
$string29 = "Servers to check:"
$string30 = "Checking DNS"
$string31 = "DNS check passed"
$string32 = "Checking ping"
$string33 = "Ping test passed"
$string34 = "Checking uptime"
$string35 = "Checking service health"
$string36 = "Checking Hub Transport Server"
$string37 = "Checking Mailbox Server"
$string38 = "Ignore list contains no server names."
$string39 = "Checking public folder database"
$string40 = "Public folder database status is"
$string41 = "Checking mailbox databases"
$string42 = "Mailbox database status is"
$string43 = "Offline databases: "
$string44 = "Checking MAPI connectivity"
$string45 = "MAPI connectivity status is"
$string46 = "MAPI failed to: "
$string47 = "Checking mail flow"
$string48 = "Mail flow status is"
$string49 = "No active DBs"
$string50 = "Finished checking server"
$string51 = "Skipped"
$string52 = "Using alternative test for Exchange 2013 CAS-only server"
$string60 = "Beginning the DAG health checks"
$string61 = "Could not determine server with active database copy"
$string62 = "mounted on server that is activation preference"
$string63 = "unhealthy database copy count is"
$string64 = "healthy copy/replay queue count is"
$string65 = "(of"
$string66 = ")"
$string67 = "unhealthy content index count is"
$string68 = "DAGs to check:"
$string69 = "DAG databases to check"
#...................................
# Functions
#...................................
#This function is used to generate HTML for the DAG member health report
Function New-DAGMemberHTMLTableCell()
{
param( $lineitem )
$htmltablecell = $null
switch ($($line."$lineitem"))
{
$null { $htmltablecell = "<td>n/a</td>" }
"Passed" { $htmltablecell = "<td class=""pass"">$($line."$lineitem")</td>" }
default { $htmltablecell = "<td class=""warn"">$($line."$lineitem")</td>" }
}
return $htmltablecell
}
#This function is used to generate HTML for the server health report
Function New-ServerHealthHTMLTableCell()
{
param( $lineitem )
$htmltablecell = $null
switch ($($reportline."$lineitem"))
{
$success {$htmltablecell = "<td class=""pass"">$($reportline."$lineitem")</td>"}
"Success" {$htmltablecell = "<td class=""pass"">$($reportline."$lineitem")</td>"}
"Pass" {$htmltablecell = "<td class=""pass"">$($reportline."$lineitem")</td>"}
"Warn" {$htmltablecell = "<td class=""warn"">$($reportline."$lineitem")</td>"}
"Access Denied" {$htmltablecell = "<td class=""warn"">$($reportline."$lineitem")</td>"}
"Fail" {$htmltablecell = "<td class=""fail"">$($reportline."$lineitem")</td>"}
"Could not test service health. " {$htmltablecell = "<td class=""warn"">$($reportline."$lineitem")</td>"}
"Unknown" {$htmltablecell = "<td class=""warn"">$($reportline."$lineitem")</td>"}
default {$htmltablecell = "<td>$($reportline."$lineitem")</td>"}
}
return $htmltablecell
}
#This function is used to write the log file if -Log is used
Function Write-Logfile()
{
param( $logentry )
$timestamp = Get-Date -DisplayHint Time
"$timestamp $logentry" | Out-File $logfile -Append
}
#This function is used to test service health for Exchange 2013 CAS-only servers
Function Test-E15CASServiceHealth()
{
param ( $e15cas )
$e15casservicehealth = $null
$servicesrunning = @()
$servicesnotrunning = @()
$casservices = @(
"IISAdmin",
"W3Svc",
"WinRM",
"MSExchangeADTopology",
"MSExchangeDiagnostics",
"MSExchangeFrontEndTransport",
#"MSExchangeHM",
"MSExchangeIMAP4",
"MSExchangePOP3",
"MSExchangeServiceHost",
"MSExchangeUMCR"
)
try {
$servicestates = @(Get-WmiObject -ComputerName $e15cas -Class Win32_Service -ErrorAction STOP | Where-Object {$casservices -icontains $_.Name} | Select-Object name,state,startmode)
}
catch
{
if ($Log) {Write-LogFile $_.Exception.Message}
Write-Warning $_.Exception.Message
$e15casservicehealth = "Fail"
}
if (!($e15casservicehealth))
{
$servicesrunning = @($servicestates | Where-Object {$_.StartMode -eq "Auto" -and $_.State -eq "Running"})
$servicesnotrunning = @($servicestates | Where-Object {$_.Startmode -eq "Auto" -and $_.State -ne "Running"})
if ($($servicesnotrunning.Count) -gt 0)
{
Write-Verbose "Service health check failed"
Write-Verbose "Services not running:"
foreach ($service in $servicesnotrunning)
{
Write-Verbose "- $($service.Name)"
}
$e15casservicehealth = "Fail"
}
else
{
Write-Verbose "Service health check passed"
$e15casservicehealth = "Pass"
}
}
return $e15casservicehealth
}
#This function is used to test mail flow for Exchange 2013 Mailbox servers
Function Test-E15MailFlow()
{
param ( $e15mailboxserver )
$e15mailflowresult = $null
Write-Verbose "Creating PSSession for $e15mailboxserver"
$url = (Get-PowerShellVirtualDirectory -Server $e15mailboxserver -AdPropertiesOnly | Where-Object {$_.Name -eq "Powershell (Default Web Site)"}).InternalURL.AbsoluteUri
if ($url -eq $null)
{
$url = "http://$e15mailboxserver/powershell"
}
try
{
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $url -ErrorAction STOP
}
catch
{
Write-Verbose "Something went wrong"
if ($Log) {Write-LogFile $_.Exception.Message}
Write-Warning $_.Exception.Message
$e15mailflowresult = "Fail"
}
try
{
Write-Verbose "Running mail flow test on $e15mailboxserver"
$result = Invoke-Command -Session $session {Test-Mailflow} -ErrorAction STOP
$e15mailflowresult = $result.TestMailflowResult
}
catch
{
Write-Verbose "An error occurred"
if ($Log) {Write-LogFile $_.Exception.Message}
Write-Warning $_.Exception.Message
$e15mailflowresult = "Fail"
}
Write-Verbose "Mail flow test: $e15mailflowresult"
Write-Verbose "Removing PSSession"
Remove-PSSession $session.Id
return $e15mailflowresult
}
#This function is used to test replication health for Exchange 2010 DAG members in mixed 2010/2013 organizations
Function Test-E14ReplicationHealth()
{
param ( $e14mailboxserver )
$e14replicationhealth = $null
#Find an E14 CAS in the same site
$ADSite = (Get-ExchangeServer $e14mailboxserver).Site
$e14cas = (Get-ExchangeServer | Where-Object {$_.IsClientAccessServer -and $_.AdminDisplayVersion -match "Version 14" -and $_.Site -eq $ADSite} | Select-Object -first 1).FQDN
Write-Verbose "Creating PSSession for $e14cas"
$url = (Get-PowerShellVirtualDirectory -Server $e14cas -AdPropertiesOnly | Where-Object {$_.Name -eq "Powershell (Default Web Site)"}).InternalURL.AbsoluteUri
if ($url -eq $null)
{
$url = "http://$e14cas/powershell"
}
Write-Verbose "Using URL $url"
try
{
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $url -ErrorAction STOP
}
catch
{
Write-Verbose "Something went wrong"
if ($Log) {Write-LogFile $_.Exception.Message}
Write-Warning $_.Exception.Message
#$e14replicationhealth = "Fail"
}
try
{
Write-Verbose "Running replication health test on $e14mailboxserver"
#$e14replicationhealth = Invoke-Command -Session $session {Test-ReplicationHealth} -ErrorAction STOP
$e14replicationhealth = Invoke-Command -Session $session -Args $e14mailboxserver.Name {Test-ReplicationHealth $args[0]} -ErrorAction STOP
}
catch
{
Write-Verbose "An error occurred"
if ($Log) {Write-LogFile $_.Exception.Message}
Write-Warning $_.Exception.Message
#$e14replicationhealth = "Fail"
}
#Write-Verbose "Replication health test: $e14replicationhealth"
Write-Verbose "Removing PSSession"
Remove-PSSession $session.Id
return $e14replicationhealth
}
#...................................
# Initialize
#...................................
#Log file is overwritten each time the script is run to avoid
#very large log files from growing over time
if ($Log) {
$timestamp = Get-Date -DisplayHint Time
"$timestamp $logstring0" | Out-File $logfile
Write-Logfile $logstring1
Write-Logfile " $now"
Write-Logfile $logstring0
}
Write-Host $initstring0
if ($Log) {Write-Logfile $initstring0}
#Add Exchange 2010 snapin if not already loaded in the PowerShell session
if (!(Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"}))
{
Write-Verbose $initstring1
if ($Log) {Write-Logfile $initstring1}
try
{
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction STOP
}
catch
{
#Snapin was not loaded
Write-Verbose $initstring2
if ($Log) {Write-Logfile $initstring2}
Write-Warning $_.Exception.Message
EXIT
}
. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
Connect-ExchangeServer -auto -AllowClobber
}
#Set scope to include entire forest
Write-Verbose $initstring3
if ($Log) {Write-Logfile $initstring3}
if (!(Get-ADServerSettings).ViewEntireForest)
{
Set-ADServerSettings -ViewEntireForest $true -WarningAction SilentlyContinue
}
#...................................
# Script
#...................................
#Check if a single server was specified
if ($server)
{
#Run for single specified server
[bool]$NoDAG = $true
Write-Verbose $string20
if ($Log) {Write-Logfile $string20}
try
{
$exchangeservers = Get-ExchangeServer $server -ErrorAction STOP
}
catch
{
#Exit because single server name was specified and couldn't be found in the organization
Write-Verbose $string21
if ($Log) {Write-Logfile $string21}
Write-Error $_.Exception.Message
EXIT
}
}
elseif ($serverlist)
{
#Run for a list of servers in a text file
[bool]$NoDAG = $true
Write-Verbose $string23
if ($Log) {Write-Logfile $string23}
try
{
$tmpservers = @(Get-Content $serverlist -ErrorAction STOP)
$exchangeservers = @($tmpservers | Get-ExchangeServer)
}
catch
{
#Exit because file could not be found
Write-Verbose $string24
if ($Log) {Write-Logfile $string24}
Write-Error $_.Exception.Message
EXIT
}
}
else
{
#This is the list of servers, DAGs, and databases to never alert for
try
{
$ignorelist = @(Get-Content $ignorelistfile -ErrorAction STOP)
if ($Log) {Write-Logfile $string28}
if ($Log) {
if ($($ignorelist.count) -gt 0)
{
foreach ($line in $ignorelist)
{
Write-Logfile "- $line"
}
}
else
{
Write-Logfile $string38
}
}
}
catch
{
Write-Warning $string22
if ($Log) {Write-Logfile $string22}
}
#Get all servers
Write-Verbose $string25
if ($Log) {Write-Logfile $string25}
$GetExchangeServerResults = @(Get-ExchangeServer | Sort-Object site,name)
#Remove the servers that are ignored from the list of servers to check
Write-Verbose $string26
if ($Log) {Write-Logfile $string26}
foreach ($tmpserver in $GetExchangeServerResults)
{
if (!($ignorelist -icontains $tmpserver.name))
{
$exchangeservers = $exchangeservers += $tmpserver.identity
}
}
if ($Log) {Write-Logfile $string29}
if ($Log) {
foreach ($server in $exchangeservers)
{
Write-Logfile "- $server"
}
}
}
### Check if any Exchange 2013 servers exist
if ($GetExchangeServerResults | Where-Object {$_.AdminDisplayVersion -like "Version 15.*"})
{
[bool]$HasE15 = $true
}
### Begin the Exchange Server health checks
Write-Verbose $string27
if ($Log) {Write-Logfile $string27}
foreach ($server in $exchangeservers)
{
Write-Host -ForegroundColor White "$string3 $server"
if ($Log) {Write-Logfile "$string3 $server"}
#Find out some details about the server
try
{
$serverinfo = Get-ExchangeServer $server -ErrorAction Stop
}
catch
{
Write-Warning $_.Exception.Message
if ($Log) {Write-Logfile $_.Exception.Message}
$serverinfo = $null
}
if ($serverinfo -eq $null )
{
#Server is not an Exchange server
Write-Host -ForegroundColor $warn $string0
if ($Log) {Write-Logfile $string0}
}
elseif ( $serverinfo.IsEdgeServer )
{
Write-Host -ForegroundColor White $string8
if ($Log) {Write-Logfile $string8}
}
else
{
#Server is an Exchange server, continue the health check
#Custom object properties
$serverObj = New-Object PSObject
$serverObj | Add-Member NoteProperty -Name "Server" -Value $server
#Skip Site attribute for Exchange 2003 servers
if ($serverinfo.AdminDisplayVersion -like "Version 6.*")
{
$serverObj | Add-Member NoteProperty -Name "Site" -Value "n/a"
}
else
{
$site = ($serverinfo.site.ToString()).Split("/")
$serverObj | Add-Member NoteProperty -Name "Site" -Value $site[-1]
}
#Null and n/a the rest, will be populated as script progresses
$serverObj | Add-Member NoteProperty -Name "DNS" -Value $null
$serverObj | Add-Member NoteProperty -Name "Ping" -Value $null
$serverObj | Add-Member NoteProperty -Name "Uptime (hrs)" -Value $null
$serverObj | Add-Member NoteProperty -Name "Version" -Value $null
$serverObj | Add-Member NoteProperty -Name "Roles" -Value $null
$serverObj | Add-Member NoteProperty -Name "Client Access Server Role Services" -Value "n/a"
$serverObj | Add-Member NoteProperty -Name "Hub Transport Server Role Services" -Value "n/a"
$serverObj | Add-Member NoteProperty -Name "Mailbox Server Role Services" -Value "n/a"
$serverObj | Add-Member NoteProperty -Name "Unified Messaging Server Role Services" -Value "n/a"
$serverObj | Add-Member NoteProperty -Name "Transport Queue" -Value "n/a"
$serverObj | Add-Member NoteProperty -Name "Queue Length" -Value "n/a"
$serverObj | Add-Member NoteProperty -Name "PF DBs Mounted" -Value "n/a"
$serverObj | Add-Member NoteProperty -Name "MB DBs Mounted" -Value "n/a"
$serverObj | Add-Member NoteProperty -Name "Mail Flow Test" -Value "n/a"
$serverObj | Add-Member NoteProperty -Name "MAPI Test" -Value "n/a"
#Check server name resolves in DNS
if ($Log) {Write-Logfile $string30}
Write-Host "DNS Check: " -NoNewline;
try
{
$ip = @([System.Net.Dns]::GetHostByName($server).AddressList | Select-Object IPAddressToString -ExpandProperty IPAddressToString)
}
catch
{
Write-Host -ForegroundColor $warn $_.Exception.Message
if ($Log) {Write-Logfile $_.Exception.Message}
$ip = $null
}
if ( $ip -ne $null )
{
Write-Host -ForegroundColor $pass "Pass"
if ($Log) {Write-Logfile $string31}
$serverObj | Add-Member NoteProperty -Name "DNS" -Value "Pass" -Force
#Is server online
if ($Log) {Write-Logfile $string32}
Write-Host "Ping Check: " -NoNewline;
$ping = $null
try
{
$ping = Test-Connection $server -Quiet -ErrorAction Stop
}
catch
{
Write-Host -ForegroundColor $warn $_.Exception.Message
if ($Log) {Write-Logfile $_.Exception.Message}
}
switch ($ping)
{
$true {
Write-Host -ForegroundColor $pass "Pass"
$serverObj | Add-Member NoteProperty -Name "Ping" -Value "Pass" -Force
if ($Log) {Write-Logfile $string33}
}
default {
Write-Host -ForegroundColor $fail "Fail"
$serverObj | Add-Member NoteProperty -Name "Ping" -Value "Fail" -Force
$serversummary += "$server - $string18"
if ($Log) {Write-Logfile $string18}
}
}
#Uptime check, even if ping fails
if ($Log) {Write-Logfile $string34}
[int]$uptime = $null
#$laststart = $null
$OS = $null
try
{
#$laststart = [System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject -Class Win32_OperatingSystem -computername $server -ErrorAction Stop).LastBootUpTime)
$OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $server -ErrorAction STOP
}
catch
{
Write-Host -ForegroundColor $warn $_.Exception.Message
if ($Log) {Write-Logfile $_.Exception.Message}
}
Write-Host "Uptime (hrs): " -NoNewline
if ($OS -eq $null)
{
[string]$uptime = $string17
if ($Log) {Write-Logfile $string17}
switch ($ping)
{
$true { $serversummary += "$server - $string17" }
default { $serversummary += "$server - $string17" }
}
}
else
{
$timespan = $OS.ConvertToDateTime($OS.LocalDateTime) – $OS.ConvertToDateTime($OS.LastBootUpTime)
[int]$uptime = "{0:00}" -f $timespan.TotalHours
Switch ($uptime -gt 23) {
$true { Write-Host -ForegroundColor $pass $uptime }
$false { Write-Host -ForegroundColor $warn $uptime; $serversummary += "$server - Uptime is less than 24 hours" }
default { Write-Host -ForegroundColor $warn $uptime; $serversummary += "$server - Uptime is less than 24 hours" }
}
}
if ($Log) {Write-Logfile "Uptime is $uptime hours"}
$serverObj | Add-Member NoteProperty -Name "Uptime (hrs)" -Value $uptime -Force
if ($ping -or ($uptime -ne $string17))
{
#Determine the friendly version number
$ExVer = $serverinfo.AdminDisplayVersion
Write-Host "Server version: " -NoNewline;
if ($ExVer -like "Version 6.*")
{
$version = "Exchange 2003"
}
if ($ExVer -like "Version 8.*")
{
$version = "Exchange 2007"
}
if ($ExVer -like "Version 14.*")
{
$version = "Exchange 2010"
}
if ($ExVer -like "Version 15.0*")
{
$version = "Exchange 2013"
}
if ($ExVer -like "Version 15.1*")
{
$version = "Exchange 2016"
}
Write-Host $version
if ($Log) {Write-Logfile "Server is running $version"}
$serverObj | Add-Member NoteProperty -Name "Version" -Value $version -Force
if ($version -eq "Exchange 2003")
{
Write-Host $string12
if ($Log) {Write-Logfile $string12}
}
#START - Exchange 2013/2010/2007 Health Checks
if ($version -ne "Exchange 2003")
{
Write-Host "Roles:" $serverinfo.ServerRole
if ($Log) {Write-Logfile "Server roles: $($serverinfo.ServerRole)"}
$serverObj | Add-Member NoteProperty -Name "Roles" -Value $serverinfo.ServerRole -Force
$IsEdge = $serverinfo.IsEdgeServer
$IsHub = $serverinfo.IsHubTransportServer
$IsCAS = $serverinfo.IsClientAccessServer
$IsMB = $serverinfo.IsMailboxServer
#START - General Server Health Check
#Skipping Edge Transports for the general health check, as firewalls usually get
#in the way. If you want to include them, remove this If.
if ($IsEdge -ne $true)
{
#Service health is an array due to how multi-role servers return Test-ServiceHealth status
if ($Log) {Write-Logfile $string35}
$servicehealth = @()
$e15casservicehealth = @()
try {
$servicehealth = @(Test-ServiceHealth $server -ErrorAction Stop)
}
catch {
#Workaround for Test-ServiceHealth problem with CAS-only Exchange 2013 servers
#More info: http://exchangeserverpro.com/exchange-2013-test-servicehealth-error/
if ($_.Exception.Message -like "*There are no Microsoft Exchange 2007 server roles installed*")
{
if ($Log) {Write-Logfile $string52}
$e15casservicehealth = Test-E15CASServiceHealth($server)
}
else
{
$serversummary += "$server - $string4"
Write-Host -ForegroundColor $warn $string4 ":" $_.Exception
if ($Log) {Write-Logfile $_.Exception}
$serverObj | Add-Member NoteProperty -Name "Client Access Server Role Services" -Value $string4 -Force
$serverObj | Add-Member NoteProperty -Name "Hub Transport Server Role Services" -Value $string4 -Force
$serverObj | Add-Member NoteProperty -Name "Mailbox Server Role Services" -Value $string4 -Force
$serverObj | Add-Member NoteProperty -Name "Unified Messaging Server Role Services" -Value $string4 -Force
}
}
if ($servicehealth)
{
foreach($s in $servicehealth)
{
$roleName = $s.Role
Write-Host $roleName "Services: " -NoNewline;
switch ($s.RequiredServicesRunning)
{
$true {
$svchealth = "Pass"
Write-Host -ForegroundColor $pass "Pass"
}
$false {
$svchealth = "Fail"
Write-Host -ForegroundColor $fail "Fail"
$serversummary += "$server - $rolename $string5"
}
default {
$svchealth = "Warn"
Write-Host -ForegroundColor $warn "Warning"
$serversummary += "$server - $rolename $string5"
}
}
switch ($s.Role)
{
$casrole { $serverinfoservices = "Client Access Server Role Services" }
$htrole { $serverinfoservices = "Hub Transport Server Role Services" }
$mbrole { $serverinfoservices = "Mailbox Server Role Services" }
$umrole { $serverinfoservices = "Unified Messaging Server Role Services" }
}
if ($Log) {Write-Logfile "$serverinfoservices status is $svchealth"}
$serverObj | Add-Member NoteProperty -Name $serverinfoservices -Value $svchealth -Force
}
}
if ($e15casservicehealth)
{
$serverinfoservices = "Client Access Server Role Services"
if ($Log) {Write-Logfile "$serverinfoservices status is $e15casservicehealth"}
$serverObj | Add-Member NoteProperty -Name $serverinfoservices -Value $e15casservicehealth -Force
Write-Host $serverinfoservices ": " -NoNewline;
switch ($e15casservicehealth)
{
"Pass" { Write-Host -ForegroundColor $pass "Pass" }
"Fail" { Write-Host -ForegroundColor $fail "Fail" }
}
}
}
#END - General Server Health Check
#START - Hub Transport Server Check
if ($IsHub)
{
$q = $null
if ($Log) {Write-Logfile $string36}
Write-Host "Total Queue: " -NoNewline;
try {
$q = Get-Queue -server $server -ErrorAction Stop | Where-Object {$_.DeliveryType -ne "ShadowRedundancy"}
}
catch {
$serversummary += "$server - $string6"
Write-Host -ForegroundColor $warn $string6
Write-Warning $_.Exception.Message
if ($Log) {Write-Logfile $string6}
if ($Log) {Write-Logfile $_.Exception.Message}
}
if ($q)
{
$qcount = $q | Measure-Object MessageCount -Sum
[int]$qlength = $qcount.sum
$serverObj | Add-Member NoteProperty -Name "Queue Length" -Value $qlength -Force
if ($Log) {Write-Logfile "Queue length is $qlength"}
if ($qlength -le $transportqueuewarn)
{
Write-Host -ForegroundColor $pass $qlength
$serverObj | Add-Member NoteProperty -Name "Transport Queue" -Value "Pass ($qlength)" -Force
}
elseif ($qlength -gt $transportqueuewarn -and $qlength -lt $transportqueuehigh)
{
Write-Host -ForegroundColor $warn $qlength
$serversummary += "$server - Transport queue is above warning threshold"
$serverObj | Add-Member NoteProperty -Name "Transport Queue" -Value "Warn ($qlength)" -Force
}
else
{
Write-Host -ForegroundColor $fail $qlength
$serversummary += "$server - Transport queue is above high threshold"
$serverObj | Add-Member NoteProperty -Name "Transport Queue" -Value "Fail ($qlength)" -Force
}
}
else