This repository has been archived by the owner on Sep 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathGet-ExchangeEnvironmentReport.ps1
1502 lines (1225 loc) · 55.4 KB
/
Get-ExchangeEnvironmentReport.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
Creates an HTML report describing the On-Premises Exchange environment.
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 2.5 August 2023
Based on the original 1.6.2 version by Steve Goodman
.DESCRIPTION
This script creates an HTML report showing the following information about an Exchange
2019, 2016, 2013, 2010, and, to a lesser extent, 2007 and 2003 environment.
Requirements
* Exchange Server Management Shell 2010 or newer
* WMI and Remote Registry access from the computer running the script to all internal Exchange Servers
* CSS file for HTML formatting
The reports shows the following:
* Report Generation Time
* Total Servers per Exchange Version (2003 > 2010 or 2007 > 2019)
* Total Mailboxes per Exchange Version, Office 365, and Organisation
* Total Roles in the environment
Then, per site:
* Total Mailboxes per site
* Internal, External and CAS Array Hostnames
* Exchange Servers with:
o Exchange Server Version
o Service Pack
o Number of preferred and maximum active databases
o Update Rollup and rollup version
o Roles installed on server and mailbox counts
o OS Version and Service Pack
Then, per Database availability group (Exchange 2010/2013/2016/2019):
* Total members per DAG
* Member list
* Databases, detailing:
o Mailbox Count and Average Size
o Archive Mailbox Count and Average Size (Only shown if DAG includes Archive Mailboxes)
o Database Size and whitespace
o Database and log disk free
o Last Full Backup (Only shown if one or more DAG database has been backed up)
o Circular Logging Enabled (Only shown if one or more DAG database has Circular Logging enabled)
o Mailbox server hosting active copy
o List of mailbox servers hosting copies and number of copies
Finally, per Database (Non DAG DBs/Exchange 2007/Exchange 2003)
* Databases, detailing:
o Storage Group (if applicable) and DB name
o Server hosting database
o Mailbox Count and Average Size
o Archive Mailbox Count and Average Size (Only shown if DAG includes Archive Mailboxes)
o Database Size and whitespace
o Database and log disk free
o Last Full Backup (Only shown if one or more DAG database has been backed up)
o Circular Logging Enabled (Only shown if one or more DAG database has Circular Logging enabled)
This does not detail public folder infrastructure, or examine Exchange 2007/2003 CCR/SCC clusters
(although it attempts to detect Clustered Exchange 2007/2003 servers, signified by ClusMBX).
IMPORTANT NOTE: The script requires WMI and Remote Registry access to Exchange servers from the server
it is run from to determine OS version, Update Rollup, Exchange 2007/2003 cluster and DB size information.
.LINK
https://github.com/Apoc70/Get-ExchangeEnvironmentReport
.PARAMETER HTMLReport
Filename to write HTML Report to
.PARAMETER SendMail
Send Mail after completion. Set to $True to enable. If enabled, -MailFrom, -MailTo, -MailServer are mandatory
.PARAMETER MailFrom
Email address to send from. Passed directly to Send-MailMessage as -From
.PARAMETER MailTo
Email address to send to. Passed directly to Send-MailMessage as -To
.PARAMETER MailServer
SMTP Mail server to attempt to send through. Passed directly to Send-MailMessage as -SmtpServer
.PARAMETER ViewEntireForest
By default, true. Set the option in Exchange 2007 or 2010 to view all Exchange servers and recipients in the forest.
.PARAMETER ServerFilter
Use a text based string to filter Exchange Servers by, e.g., NL-*
Note the use of the wildcard (*) character to allow for multiple matches.
.PARAMETER ShowDriveNames
Include drive names of EDB file path and LOG file folder in database report table
.PARAMETER CssFileName
The filename containing the Cascading Style Sheet (CSS) information fpr the HTML report
Default: EnvironmentReport.css
.EXAMPLE
Generate the HTML report
.\Get-ExchangeEnvironmentReport.ps1 -HTMLReport .\report.html
.EXAMPLE
Generate the HTML report using a custom CSS file
.\Get-ExchangeEnvironmentReport.ps1 -HTMLReport .\report.html -CssFileName MyCustomCSSFile.css
.EXAMPLE
Generate am HTML report and send the result as HTML email with attachment to the specified recipient using a dedicated smart host
.\Get-ExchangeEnvironmentReport.ps1 -HTMReport ExchangeEnvironment.html -SendMail -ViewEntireForet $true -MailFrom [email protected] -MailTo [email protected] -MailServer relay.mcsmemail.de
.EXAMPLE
Generate the HTML report including EDB and LOG drive names
.\Get-ExchangeEnvironmentReport.ps1 -ShowDriveNames -HTMLReport .\report.html
#>
[CmdletBinding()]
param(
[parameter(Position = 0, Mandatory, HelpMessage = 'Filename to write HTML report to')]
[string]$HTMLReport,
[switch]$SendMail,
[string]$MailFrom = '',
[string]$MailTo = '',
[string]$MailServer = '',
[bool]$ViewEntireForest = $true,
[string]$ServerFilter = '*',
[switch]$ShowDriveNames,
[string]$CssFileName = 'EnvironmentReport.css'
)
# Warning Limits, adjust as needed
$MinFreeDiskspace = 10 # Mark free space less than this value (%) in red
$MaxDatabaseSize = 250 # Mark database larger than this value (GB) in red
# Default variables
$NotAvailable = 'N/A'
$ScriptDir = Split-Path -Path $script:MyInvocation.MyCommand.Path
# Set TLS version o TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Sub-Function to Get Database Information. Shorter than expected..
function Get-DatabaseAvailabilityGroupInformation {
[CmdletBinding()]
param(
$DAG
)
@{Name = $DAG.Name.ToUpper()
MemberCount = $DAG.Servers.Count
Members = [array]($DAG.Servers | ForEach-Object { $_.Name })
Databases = @()
}
}
# Sub-Function to Get Database Information
function Get-DatabaseInformation {
[CmdletBinding()]
param(
$Database,
$ExchangeEnvironment,
$Mailboxes,
$ArchiveMailboxes,
$E2010
)
# Circular Logging, Last Full Backup
if ($Database.CircularLoggingEnabled) { $CircularLoggingEnabled = 'Yes' } else { $CircularLoggingEnabled = 'No' }
if ($Database.LastFullBackup) { $LastFullBackup = $Database.LastFullBackup.ToString() } else { $LastFullBackup = 'Not Available' }
# Drive Letter, GitHub issue #4
$DriveNameEdb = ''
try {
$DriveNameEdb = $Database.EdbFilePath.DriveName
}
catch {
$DriveNameEdb = $NotAvailable
}
$DriveNameLog = ''
try {
$DriveNameLog = $Database.LogFolderPath.DriveName
}
catch {
$DriveNameLog = $NotAvailable
}
# Mailbox Average Sizes
$MailboxStatistics = [array]($ExchangeEnvironment.Servers[$Database.Server.Name].MailboxStatistics | Where-Object { $_.Database -eq $Database.Identity })
if ($MailboxStatistics) {
[long]$MailboxItemSizeB = 0
$MailboxStatistics | ForEach-Object { $MailboxItemSizeB += $_.TotalItemSizeB }
[long]$MailboxAverageSize = $MailboxItemSizeB / $MailboxStatistics.Count
}
else {
$MailboxAverageSize = 0
}
# Free Disk Space Percentage
if ($ExchangeEnvironment.Servers[$Database.Server.Name].Disks) {
foreach ($Disk in $ExchangeEnvironment.Servers[$Database.Server.Name].Disks) {
if ($Database.EdbFilePath.PathName -like ('{0}*' -f $Disk.Name)) {
$FreeDatabaseDiskSpace = $Disk.FreeSpace / $Disk.Capacity * 100
}
if ($Database.ExchangeVersion.ExchangeBuild.Major -ge 14) {
if ($Database.LogFolderPath.PathName -like ('{0}*' -f $Disk.Name)) {
$FreeLogDiskSpace = ($Disk.FreeSpace / $Disk.Capacity) * 100
}
}
else {
$StorageGroupDN = $Database.DistinguishedName.Replace(('CN={0},' -f $Database.Name), '')
$Adsi = [adsi]"LDAP://$($Database.OriginatingServer)/$($StorageGroupDN)"
if ($Adsi.msExchESEParamLogFilePath -like ('{0}*' -f $Disk.Name)) {
$FreeLogDiskSpace = $Disk.FreeSpace / $Disk.Capacity * 100
}
}
}
}
else {
$FreeLogDiskSpace = $null
$FreeDatabaseDiskSpace = $null
}
if ($Database.ExchangeVersion.ExchangeBuild.Major -ge 14 -and $E2010) {
# Exchange 2010 Database Only
$CopyCount = [int]$Database.Servers.Count
if ($Database.MasterServerOrAvailabilityGroup.Name -ne $Database.Server.Name) {
$Copies = [array]($Database.Servers | ForEach-Object { $_.Name })
}
else {
$Copies = @()
}
# Archive Info
$ArchiveMailboxCount = [int]([array]($ArchiveMailboxes | Where-Object { $_.ArchiveDatabase -eq $Database.Name })).Count
$ArchiveStatistics = [array]($ArchiveMailboxes | Where-Object { $_.ArchiveDatabase -eq $Database.Name } | Get-MailboxStatistics -Archive )
if ($ArchiveStatistics) {
[long]$ArchiveItemSizeB = 0
$ArchiveStatistics | ForEach-Object { $ArchiveItemSizeB += $_.TotalItemSize.Value.ToBytes() }
[long]$ArchiveAverageSize = $ArchiveItemSizeB / $ArchiveStatistics.Count
}
else {
$ArchiveAverageSize = 0
}
# DB Size / Whitespace Info
[long]$Size = $Database.DatabaseSize.ToBytes()
[long]$Whitespace = $Database.AvailableNewMailboxSpace.ToBytes()
$StorageGroup = $null
}
else {
$ArchiveMailboxCount = 0
$CopyCount = 0
$Copies = @()
# 2003 & 2007, Use WMI (Based on code by Gary Siepser, http://bit.ly/kWWMb3)
$Size = [long](get-wmiobject -Class cim_datafile -ComputerName $Database.Server.Name -Filter ('name=''' + $Database.edbfilepath.pathname.replace('\', '\\') + '''')).filesize
if (!$Size) {
Write-Warning -Message ('Cannot detect database size via WMI for {0}' -f $Database.Server.Name)
[long]$Size = 0
[long]$Whitespace = 0
}
else {
[long]$MailboxDeletedItemSizeB = 0
if ($MailboxStatistics) {
$MailboxStatistics | ForEach-Object { $MailboxDeletedItemSizeB += $_.TotalDeletedItemSizeB }
}
# Calculate database whitespace
$Whitespace = $Size - $MailboxItemSizeB - $MailboxDeletedItemSizeB
if ($Whitespace -lt 0) { $Whitespace = 0 }
}
$StorageGroup = $Database.DistinguishedName.Split(',')[1].Replace('CN=', '')
}
@{
Name = $Database.Name
StorageGroup = $StorageGroup
ActiveOwner = $Database.Server.Name.ToUpper()
MailboxCount = [long]([array]($Mailboxes | Where-Object { $_.Database -eq $Database.Identity })).Count
MailboxAverageSize = $MailboxAverageSize
ArchiveMailboxCount = $ArchiveMailboxCount
ArchiveAverageSize = $ArchiveAverageSize
CircularLoggingEnabled = $CircularLoggingEnabled
LastFullBackup = $LastFullBackup
Size = $Size
Whitespace = $Whitespace
Copies = $Copies
CopyCount = $CopyCount
FreeLogDiskSpace = $FreeLogDiskSpace
FreeDatabaseDiskSpace = $FreeDatabaseDiskSpace
DriveNameEdb = $DriveNameEdb
DriveNameLog = $DriveNameLog
}
}
# Sub-Function to get mailbox count per server.
# New in 1.5.2
function Get-ExchangeServerMailboxCount {
[CmdletBinding()]
param(
$Mailboxes,
$ExchangeServer,
$Databases
)
# The following *should* work, but it doesn't. Apparently, ServerName is not always returned correctly which may be the cause of
# reports of counts being incorrect
#([array]($Mailboxes | Where {$_.ServerName -eq $ExchangeServer.Name})).Count
# ..So as a workaround, I'm going to check what databases are assigned to each server and then get the mailbox counts on a per-
# database basis and return the resulting total. As we already have this information resident in memory it should be cheap, just
# not as quick.
$MailboxCount = 0
foreach ($Database in [array]($Databases | Where-Object { $_.Server -eq $ExchangeServer.Name })) {
$MailboxCount += ([array]($Mailboxes | Where-Object { $_.Database -eq $Database.Identity })).Count
}
$MailboxCount
}
# 2021-12-23 Function added to handle empty virtual directory hostname strings (Issue #9)
function Test-vDirHost {
[CmdletBinding()]
param(
$VDirHost
)
[string]$Hostname = 'None'
if ($null -ne $VDirHost) {
$Hostname = ([string]$VDirHost).Trim()
}
$Hostname
}
# Sub-Function to Get Exchange Server information
function Get-ExchangeServerInformation {
[CmdletBinding()]
param(
$E2010,
$ExchangeServer,
$Mailboxes,
$Databases,
$Hybrids
)
# Set Basic Variables
$MailboxCount = 0
$RollupLevel = 0
$RollupVersion = ''
$ExtNames = @()
$IntNames = @()
$CASArrayName = ''
# 2019-05-20 TST Added to handle max preferred/active databases per server
$MaxPrefDatabases = 0
$MaxActiveDatabases = 0
$NotSet = '--'
# Get WMI Information: Operatin System
$tWMI = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ExchangeServer.Name -ErrorAction SilentlyContinue
if ($tWMI) {
$OSVersion = $tWMI.Caption.Replace('(R)', '').Replace('Microsoft ', '').Replace('Enterprise', 'Ent').Replace('Standard', 'Std').Replace(' Edition', '')
$OSServicePack = $tWMI.CSDVersion
$RealName = $tWMI.CSName.ToUpper()
}
else {
Write-Warning -Message ('Cannot detect OS information via WMI for {0}' -f $ExchangeServer.Name)
$OSVersion = $NotAvailable
$OSServicePack = $NotAvailable
$RealName = $ExchangeServer.Name.ToUpper()
}
# Get WMI Information: Disk Space
$tWMI = Get-WmiObject -Query 'Select * from Win32_Volume' -ComputerName $ExchangeServer.Name -ErrorAction SilentlyContinue
if ($tWMI) {
$Disks = $tWMI | Select-Object -Property Name, Capacity, FreeSpace | Sort-Object -Property Name
}
else {
Write-Warning -Message ('Cannot detect OS information via WMI for {0}' -f $ExchangeServer.Name)
$Disks = $null
}
# Get Exchange Version
if ($ExchangeServer.AdminDisplayVersion.Major -eq 6) {
$ExchangeMajorVersion = [double]('{0}.{1}' -f $ExchangeServer.AdminDisplayVersion.Major, $ExchangeServer.AdminDisplayVersion.Minor)
$ExchangeSPLevel = $ExchangeServer.AdminDisplayVersion.FilePatchLevelDescription.Replace('Service Pack ', '')
}
elseif ($ExchangeServer.AdminDisplayVersion.Major -eq 15 -and $ExchangeServer.AdminDisplayVersion.Minor -ge 1) {
$ExchangeMajorVersion = [double]('{0}.{1}' -f $ExchangeServer.AdminDisplayVersion.Major, $ExchangeServer.AdminDisplayVersion.Minor)
$ExchangeSPLevel = 0
}
else {
$ExchangeMajorVersion = $ExchangeServer.AdminDisplayVersion.Major
$ExchangeSPLevel = $ExchangeServer.AdminDisplayVersion.Minor
}
# Exchange 2007+
if ($ExchangeMajorVersion -ge 8) {
# Get Roles
$MailboxStatistics = $null
[array]$Roles = $ExchangeServer.ServerRole.ToString().Replace(' ', '').Split(',')
# Add Hybrid "Role" for report
if ($Hybrids -contains $ExchangeServer.Name) {
$Roles += 'Hybrid'
}
if ($Roles -contains 'Mailbox') {
$MailboxCount = Get-ExchangeServerMailboxCount -Mailboxes $Mailboxes -ExchangeServer $ExchangeServer -Databases $Databases
if ($ExchangeServer.Name.ToUpper() -ne $RealName) {
$Roles = [array]($Roles | Where-Object { $_ -ne 'Mailbox' })
$Roles += 'ClusteredMailbox'
}
# Get Mailbox Statistics the normal way, return in a consitent format
# 2019-05-20 TST, try/catch added
try {
$MailboxStatistics = Get-MailboxStatistics -Server $ExchangeServer -ErrorAction SilentlyContinue | Select-Object -Property DisplayName, @{Name = 'TotalItemSizeB'; Expression = { $_.TotalItemSize.Value.ToBytes() } }, @{Name = 'TotalDeletedItemSizeB'; Expression = { $_.TotalDeletedItemSize.Value.ToBytes() } }, Database
}
catch {
$MailboxStatistics = $null
Write-Warning -Message ('Cannot get mailbox statistics for server {0}' -f $ExchangeServer)
}
if ($ExchangeMajorVersion -ge 14) {
$mailboxServer = Get-MailboxServer -Identity $($ExchangeServer.Name)
# 2019-05-20 TST Gather max active/max preferred database config
if ($ExchangeMajorVersion -lt 15) {
# Exchange 2010
$MaxActiveDatabases = $mailboxServer.MaximumActiveDatabases
}
else {
# Exchange 2013+
if ($null -ne $mailboxServer.MaximumPreferredActiveDatabases) {
$MaxPrefDatabases = $mailboxServer.MaximumPreferredActiveDatabases
}
else {
$MaxPrefDatabases = $NotSet
}
if ($null -ne $mailboxServer.MaximumActiveDatabases) {
$MaxActiveDatabases = $mailboxServer.MaximumActiveDatabases
}
else {
$MaxActiveDatabases = $NotSet
}
}
}
}
# Get HTTPS Names (Exchange 2010 only due to time taken to retrieve data)
# 2019-05-16 TST | Update to support 'Mailbox' role for gathering namespace information
if (($Roles -contains 'ClientAccess' -and $E2010) -or ($Roles -contains 'Mailbox' -and $E2010)) {
Get-OWAVirtualDirectory -Server $ExchangeServer -ADPropertiesOnly | ForEach-Object { $ExtNames += (Test-vDirHost -VDirHost $_.ExternalURL.Host); $IntNames += (Test-vDirHost -VDirHost $_.InternalURL.Host) }
Get-WebServicesVirtualDirectory -Server $ExchangeServer -ADPropertiesOnly | ForEach-Object { $ExtNames += (Test-vDirHost -VDirHost $_.ExternalURL.Host); $IntNames += (Test-vDirHost -VDirHost $_.InternalURL.Host) }
Get-OABVirtualDirectory -Server $ExchangeServer -ADPropertiesOnly | ForEach-Object { $ExtNames += (Test-vDirHost -VDirHost $_.ExternalURL.Host); $IntNames += (Test-vDirHost -VDirHost $_.InternalURL.Host) }
Get-ActiveSyncVirtualDirectory -Server $ExchangeServer -ADPropertiesOnly | ForEach-Object { $ExtNames += (Test-vDirHost -VDirHost $_.ExternalURL.Host); $IntNames += (Test-vDirHost -VDirHost $_.InternalURL.Host) }
if (Get-Command -Name Get-MAPIVirtualDirectory -ErrorAction SilentlyContinue) {
Get-MAPIVirtualDirectory -Server $ExchangeServer -ADPropertiesOnly | ForEach-Object { $ExtNames += (Test-vDirHost -VDirHost $_.ExternalURL.Host); $IntNames += (Test-vDirHost -VDirHost $_.InternalURL.Host) }
}
if (Get-Command -Name Get-ClientAccessService -ErrorAction SilentlyContinue) {
$IntNames += (Test-vDirHost -VDirHost (Get-ClientAccessService -Identity $ExchangeServer.Name).AutoDiscoverServiceInternalURI.Host)
}
else {
# Fallback to use Get-ClientAccessServer cmdlet
$IntNames += (Test-vDirHost -VDirHost (Get-ClientAccessServer -Identity $ExchangeServer.Name).AutoDiscoverServiceInternalURI.Host)
}
if ($ExchangeMajorVersion -ge 14) {
Get-ECPVirtualDirectory -Server $ExchangeServer -ADPropertiesOnly | ForEach-Object { $ExtNames += (Test-vDirHost -VDirHost $_.ExternalURL.Host); $IntNames += (Test-vDirHost -VDirHost $_.InternalURL.Host); }
}
$IntNames = $IntNames | Sort-Object -Unique
$ExtNames = $ExtNames | Sort-Object -Unique
$CASArray = Get-ClientAccessArray -Site $ExchangeServer.Site.Name
if ($CASArray) {
$CASArrayName = $CASArray.Fqdn
}
}
# Rollup Level / Versions (Thanks to Bhargav Shukla https://bhargavs.com/index.php/2009/12/14/how-do-i-check-update-rollup-version-on-exchange-20xx-server/)
switch ([string]$ExchangeMajorVersion) {
# Exchange Server 2016 / 2019
'15.2' { $RegKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products\\442189DC8B9EA5040962A6BED9EC1F1F\\Patches" }
'15.1' { $RegKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products\\442189DC8B9EA5040962A6BED9EC1F1F\\Patches" }
# Exchange Server 2010 / 2013
'15' { $RegKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products\\AE1D439464EB1B8488741FFA028E291C\\Patches" }
'14' { $RegKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products\\AE1D439464EB1B8488741FFA028E291C\\Patches" }
# Exchange 2007
default { $RegKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products\\461C2B4266EDEF444B864AD6D9E5B613\\Patches" }
}
# 2019-05-17 Thomas Stensitzki, try/catch added
try {
$RemoteRegistry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $ExchangeServer.Name)
}
catch {
$RemoteRegistry = $null
}
if ($null -ne $RemoteRegistry) {
$RUKeys = $RemoteRegistry.OpenSubKey($RegKey).GetSubKeyNames() | ForEach-Object { "$RegKey\\$_" }
if ($RUKeys) {
[array]($RUKeys | ForEach-Object { $RemoteRegistry.OpenSubKey($_).GetValue('DisplayName') }) | `
ForEach-Object {
if ($_ -like 'Update Rollup *') {
$tRU = $_.Split(' ')[2]
if ($tRU -like '*-*') { $tRUV = $tRU.Split('-')[1]; $tRU = $tRU.Split('-')[0] } else { $tRUV = '' }
if ([int]$tRU -ge [int]$RollupLevel) { $RollupLevel = $tRU; $RollupVersion = $tRUV }
}
}
}
}
else {
Write-Warning -Message ('Cannot detect Rollup Version via Remote Registry for {0}' -f $ExchangeServer.Name)
}
# Exchange 2013+ CU or SP Level
if ($ExchangeMajorVersion -ge 15) {
$RegKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Microsoft Exchange v15"
# 2019-05-17 Thomas Stensitzki, try/catch added
try {
$RemoteRegistry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $ExchangeServer.Name)
}
catch {
$RemoteRegistry = $null
}
if ($RemoteRegistry) {
$ExchangeSPLevel = $RemoteRegistry.OpenSubKey($RegKey).GetValue('DisplayName')
if ($ExchangeSPLevel -like '*Service Pack*' -or $ExchangeSPLevel -like '*Cumulative Update*') {
$ExchangeSPLevel = $ExchangeSPLevel.Replace('Microsoft Exchange Server 2013 ', '')
$ExchangeSPLevel = $ExchangeSPLevel.Replace('Microsoft Exchange Server 2016 ', '')
$ExchangeSPLevel = $ExchangeSPLevel.Replace('Microsoft Exchange Server 2019 ', '')
$ExchangeSPLevel = $ExchangeSPLevel.Replace('Service Pack ', 'SP')
$ExchangeSPLevel = $ExchangeSPLevel.Replace('Cumulative Update ', 'CU')
}
else {
$ExchangeSPLevel = 0
}
}
else {
Write-Warning -Message ('Cannot detect CU/SP via Remote Registry for {0}' -f $ExchangeServer.Name)
}
}
}
# Exchange 2003
if ($ExchangeMajorVersion -eq 6.5) {
# Mailbox Count
$MailboxCount = Get-ExchangeServerMailboxCount -Mailboxes $Mailboxes -ExchangeServer $ExchangeServer -Databases $Databases
# Get Role via WMI
$tWMI = Get-WMIObject -Class Exchange_Server -Namespace 'root\microsoftexchangev2' -ComputerName $ExchangeServer.Name -Filter "Name='$($ExchangeServer.Name)'"
if ($tWMI) {
if ($tWMI.IsFrontEndServer) { $Roles = @('FE') } else { $Roles = @('BE') }
}
else {
Write-Warning -Message ('Cannot detect Front End/Back End Server information via WMI for {0}' -f $ExchangeServer.Name)
$Roles += 'Unknown'
}
# Get Mailbox Statistics using WMI, return in a consistent format
$tWMI = Get-WMIObject -class Exchange_Mailbox -Namespace ROOT\MicrosoftExchangev2 -ComputerName $ExchangeServer.Name -Filter ("ServerName='$($ExchangeServer.Name)'")
if ($tWMI) {
$MailboxStatistics = $tWMI | Select-Object -Property @{Name = 'DisplayName'; Expression = { $_.MailboxDisplayName } }, @{Name = 'TotalItemSizeB'; Expression = { $_.Size } }, @{Name = 'TotalDeletedItemSizeB'; Expression = { $_.DeletedMessageSizeExtended } }, @{Name = 'Database'; Expression = { ((Get-MailboxDatabase -Identity "$($_.ServerName)\$($_.StorageGroupName)\$($_.StoreName)").Identity) } }
}
else {
Write-Warning -Message ('Cannot retrieve Mailbox Statistics via WMI for {0}' -f $ExchangeServer.Name)
$MailboxStatistics = $null
}
}
# Exchange 2000
if ($ExchangeMajorVersion -eq '6.0') {
# Mailbox Count
$MailboxCount = Get-ExchangeServerMailboxCount -Mailboxes $Mailboxes -ExchangeServer $ExchangeServer -Databases $Databases
# Get Role via ADSI
$tADSI = [ADSI]"LDAP://$($ExchangeServer.OriginatingServer)/$($ExchangeServer.DistinguishedName)"
if ($tADSI) {
if ($tADSI.ServerRole -eq 1) { $Roles = @('FE') } else { $Roles = @('BE') }
}
else {
Write-Warning -Message ('Cannot detect Front End/Back End Server information via ADSI for {0}' -f $ExchangeServer.Name)
$Roles += 'Unknown'
}
$MailboxStatistics = $null
}
# Return Hashtable
@{
Name = $ExchangeServer.Name.ToUpper()
RealName = $RealName
ExchangeMajorVersion = $ExchangeMajorVersion
ExchangeSPLevel = $ExchangeSPLevel
Edition = $ExchangeServer.Edition
Mailboxes = $MailboxCount
OSVersion = $OSVersion;
OSServicePack = $OSServicePack
Roles = $Roles
RollupLevel = $RollupLevel
RollupVersion = $RollupVersion
Site = $ExchangeServer.Site.Name
MailboxStatistics = $MailboxStatistics
Disks = $Disks
IntNames = $IntNames
ExtNames = $ExtNames
CASArrayName = $CASArrayName
MaximumPreferredDatabases = $MaxPrefDatabases
MaximumActiveDatabases = $MaxActiveDatabases
}
}
# Sub Function to Get Totals by Version
function Get-TotalsByVersion {
[CmdletBinding()]
param(
$ExchangeEnvironment
)
# Create empty hash table
$TotalMailboxesByVersion = @{}
if ($ExchangeEnvironment.Sites) {
foreach ($Site in $ExchangeEnvironment.Sites.GetEnumerator()) {
foreach ($Server in $Site.Value) {
if (!$TotalMailboxesByVersion["$($Server.ExchangeMajorVersion).$($Server.ExchangeSPLevel)"]) {
$TotalMailboxesByVersion.Add("$($Server.ExchangeMajorVersion).$($Server.ExchangeSPLevel)", @{ServerCount = 1; MailboxCount = $Server.Mailboxes })
}
else {
$TotalMailboxesByVersion["$($Server.ExchangeMajorVersion).$($Server.ExchangeSPLevel)"].ServerCount++
$TotalMailboxesByVersion["$($Server.ExchangeMajorVersion).$($Server.ExchangeSPLevel)"].MailboxCount += $Server.Mailboxes
}
}
}
}
if ($ExchangeEnvironment.Pre2007) {
foreach ($FakeSite in $ExchangeEnvironment.Pre2007.GetEnumerator()) {
foreach ($Server in $FakeSite.Value) {
if (!$TotalMailboxesByVersion["$($Server.ExchangeMajorVersion).$($Server.ExchangeSPLevel)"]) {
$TotalMailboxesByVersion.Add("$($Server.ExchangeMajorVersion).$($Server.ExchangeSPLevel)", @{ServerCount = 1; MailboxCount = $Server.Mailboxes })
}
else {
$TotalMailboxesByVersion["$($Server.ExchangeMajorVersion).$($Server.ExchangeSPLevel)"].ServerCount++
$TotalMailboxesByVersion["$($Server.ExchangeMajorVersion).$($Server.ExchangeSPLevel)"].MailboxCount += $Server.Mailboxes
}
}
}
}
$TotalMailboxesByVersion
}
# Sub Function to Get Totals by Role
function Get-TotalsByRole {
[CmdletBinding()]
param(
$ExchangeEnvironment
)
# Add Roles We Always Show
$TotalServersByRole = @{
'ClientAccess' = 0
'HubTransport' = 0
'UnifiedMessaging' = 0
'Mailbox' = 0
'Edge' = 0
}
if ($ExchangeEnvironment.Sites) {
foreach ($Site in $ExchangeEnvironment.Sites.GetEnumerator()) {
foreach ($Server in $Site.Value) {
foreach ($Role in $Server.Roles) {
if ($null -eq $TotalServersByRole[$Role]) {
$TotalServersByRole.Add($Role, 1)
}
else {
$TotalServersByRole[$Role]++
}
}
}
}
}
if ($ExchangeEnvironment.Pre2007['Pre 2007 Servers']) {
foreach ($Server in $ExchangeEnvironment.Pre2007['Pre 2007 Servers']) {
foreach ($Role in $Server.Roles) {
if ($null -eq $TotalServersByRole[$Role]) {
$TotalServersByRole.Add($Role, 1)
}
else {
$TotalServersByRole[$Role]++
}
}
}
}
$TotalServersByRole
}
# Sub Function to return HTML Table for Sites/Pre 2007
function Get-HtmlOverview {
[CmdletBinding()]
param(
$Servers,
$ExchangeEnvironment,
$ExRoleStrings,
$Pre2007 = $False
)
if ($Pre2007) {
$BGColHeader = '#880099'
$BGColSubHeader = '#8800CC'
$Prefix = ''
$IntNamesText = ''
$ExtNamesText = ''
$CASArrayText = ''
$ColClass = 'pre2007overviewcolheader'
$ColSubClass = 'pre2007overviewcolsubheader'
}
else {
$BGColHeader = '#000099'
$BGColSubHeader = '#0000FF'
$Prefix = 'Site:'
$IntNamesText = ''
$ExtNamesText = ''
$CASArrayText = ''
$IntNames = @()
$ExtNames = @()
$CASArrayName = ''
$ColClass = 'overviewcolheader'
$ColSubClass = 'overviewcolsubheader'
foreach ($Server in $Servers.Value) {
$IntNames += $Server.IntNames
$ExtNames += $Server.ExtNames
$CASArrayName = $Server.CASArrayName
}
$IntNames = $IntNames | Sort-Object -Unique
$ExtNames = $ExtNames | Sort-Object -Unique
$IntNames = [string]::Join(',', $IntNames)
$ExtNames = [string]::Join(',', $ExtNames)
$ExtNamesEmptyText = 'At least one of the analysed servers contains an empty ExternalUrl entry.'
if ($IntNames) {
$ExtNamesText = ('External Names: {0}<br />' -f $ExtNames)
if ($ExtNames -notlike '*None*') {
$IntNamesText = ('Internal Names: {0}' -f $IntNames)
}
else {
$IntNamesText = ('Internal Names: {0}<br />{1}' -f $IntNames, $ExtNamesEmptyText)
}
}
if ($CASArrayName) {
$CASArrayText = "CAS Array: $($CASArrayName)"
}
}
$Output = "<table class='overview'>
<col width='20%'>
<col width='20%'>
<colgroup width='25%'>"
$ExchangeEnvironment.TotalServersByRole.GetEnumerator() | Sort-Object -Property Name | ForEach-Object { $Output += "<col width='3%'>" }
$Output += "</colgroup><col width='20%'><col width='20%'>
<tr class=""$($ColClass)"">
<th class='overview'>$($Prefix) $($Servers.Key)</th>
<th class='overview' colspan=""$(($ExchangeEnvironment.TotalServersByRole.Count)+2)"" align=""left"">$($ExtNamesText)$($IntNamesText)</th>
<th class='overview' align=""center"">$($CASArrayText)</th>
<th colspan='2'> </th>
</tr>"
$TotalMailboxes = 0
$Servers.Value | ForEach-Object { $TotalMailboxes += $_.Mailboxes }
$Output += "<tr class=""$($ColSubClass)""><th class='overview'>Mailboxes: $($TotalMailboxes)</th>"
$Output += "<th class='overview'>Exchange Version</th>"
$ExchangeEnvironment.TotalServersByRole.GetEnumerator() | Sort-Object -Property Name | ForEach-Object { $Output += "<th class='overview'>$($ExRoleStrings[$_.Key].Short)</th>" }
# 2016-04-19 Thomas Stensitzki Pref/Max Databases added
$Output += "<th class='overview'>Databases Pref/Max</th><th class='overview'>OS Version</th><th class='overview'>OS Service Pack</th></tr>"
$AlternateRow = 0
foreach ($Server in $Servers.Value) {
$Output += '<tr'
if ($AlternateRow) {
$Output += " class='alternaterow'"
$AlternateRow = 0
}
else {
$AlternateRow = 1
}
$Output += ('><td>{0}' -f $Server.Name)
if ($Server.RealName -ne $Server.Name) {
$Output += (' ({0})' -f $Server.RealName)
}
$Output += "</td><td>$($ExVersionStrings["$($Server.ExchangeMajorVersion).$($Server.ExchangeSPLevel)"].Long)"
if ($Server.RollupLevel -gt 0) {
$Output += (' UR{0}' -f $Server.RollupLevel)
if ($Server.RollupVersion) {
$Output += (' {0}' -f $Server.RollupVersion)
}
}
$Output += '</td>'
$ExchangeEnvironment.TotalServersByRole.GetEnumerator() | Sort-Object -Property Name | ForEach-Object {
$Output += '<td'
if ($Server.Roles -contains $_.Key) {
$Output += " class='roledata'>"
}
else {
$Output += ' />'
}
if (($_.Key -eq 'ClusteredMailbox' -or $_.Key -eq 'Mailbox' -or $_.Key -eq 'BE') -and $Server.Roles -contains $_.Key) {
$Output += ('{0}</td>' -f $Server.Mailboxes)
}
}
# 2016-04-19 Thomas Stensitzki Max Databases added
if ($Server.Roles -contains 'Mailbox') {
$Output += "<td align=""center"">$($Server.MaximumPreferredDatabases) / $($Server.MaximumActiveDatabases)</td><td>$($Server.OSVersion)</td><td>$($Server.OSServicePack)</td></tr>"
}
else {
$Output += ('<td align=""center"">Not Applicable</td><td>{0}</td><td>{1}</td></tr>' -f $Server.OSVersion, $Server.OSServicePack)
}
# $Output+="<td>$($Server.OSVersion)</td><td>$($Server.OSServicePack)</td></tr>";
}
$Output += '<tr />
</table><br />'
$Output
}
# Sub Function to return HTML Table for Databases
function Get-HtmlDatabaseInformationTable {
[CmdletBinding()]
param(
$Databases
)
# Only Show Archive Mailbox Columns, Backup Columns and Circ Logging if at least one DB has an Archive mailbox, backed up or Cir Log enabled.
$ShowArchiveDBs = $False
$ShowLastFullBackup = $False
$ShowCircularLogging = $False
$ShowStorageGroups = $False
$ShowCopies = $False
$ShowFreeDatabaseSpace = $False
$ShowFreeLogDiskSpace = $False
foreach ($Database in $Databases) {
if ($Database.ArchiveMailboxCount -gt 0) {
$ShowArchiveDBs = $True
}
if ($Database.LastFullBackup -ne 'Not Available') {
$ShowLastFullBackup = $True
}
if ($Database.CircularLoggingEnabled -eq 'Yes') {
$ShowCircularLogging = $True
}
if ($Database.StorageGroup) {
$ShowStorageGroups = $True
}
if ($Database.CopyCount -gt 0) {
$ShowCopies = $True
}
if ($null -ne $Database.FreeDatabaseDiskSpace) {
$ShowFreeDatabaseSpace = $true
}
if ($null -ne $Database.FreeLogDiskSpace) {
$ShowFreeLogDiskSpace = $true
}
}
$Output = "<table class='databases'>"
#region database table header
$Output += "<tr class='databases'>
<th>Server</th>"
if ($ShowStorageGroups) {
$Output += '<th>Storage Group</th>'
}
$Output += '<th>Database Name</th>
<th>Standard Mailboxes</th>
<th>Av. Mailbox Size</th>'
if ($ShowArchiveDBs) {
$Output += '<th>Archive Mailboxes</th><th>Av. Archive Size</th>'
}
$Output += '<th>DB Size</th><th>DB Whitespace</th>'
if ($ShowFreeDatabaseSpace) {
$Output += '<th>Database Disk Free</th>'
}
if ($ShowFreeLogDiskSpace) {
$Output += '<th>Log Disk Free</th>'
}
if ($ShowLastFullBackup) {
$Output += '<th>Last Full Backup</th>'
}
if ($ShowCircularLogging) {
$Output += '<th>Circular Logging</th>'
}
if ($ShowCopies) {
$Output += '<th>DB Copies (n)</th>'
}
# Drive names, issue #4
if ($ShowDriveNames) {
$Output += '<th>EDB / LOG</th>'
}
$Output += '</tr>'
#endregion
$AlternateRow = 0
foreach ($Database in $Databases) {
$Output += '<tr'
if ($AlternateRow) {
$Output += " class='alternaterow'"
$AlternateRow = 0
}
else {
$AlternateRow = 1
}
# Close open <tr tag
$Output += ('><td>{0}</td>' -f $Database.ActiveOwner)
if ($ShowStorageGroups) {
$Output += ('><td>{0}</td>' -f $Database.StorageGroup)
}
$Output += "<td>$($Database.Name)</td>
<td class='center'>$($Database.MailboxCount)</td>
<td class='center'>$("{0:N2}" -f ($Database.MailboxAverageSize/1MB)) MB</td>"
if ($ShowArchiveDBs) {
$Output += "<td class=""center"">$($Database.ArchiveMailboxCount)</td>
<td class='center'>$("{0:N2}" -f ($Database.ArchiveAverageSize/1MB)) MB</td>"
}
if ([double]($Database.Size / 1GB) -le $MaxDatabaseSize) {
$Output += "<td class=""center"">$("{0:N2}" -f ($Database.Size/1GB)) GB </td>"
}