-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-MailboxDatabaseConsumption.ps1
191 lines (158 loc) · 7.95 KB
/
Get-MailboxDatabaseConsumption.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
<#
.Synopsis
Get database size, whitespace, should-be-whitespace (in stubbing environments),
mailbox count, and consumption breakdown by:
- Active mailboxes
- Disconnected mailboxes (disabled, soft deleted)
.Parameter Database
Target one of more specific databases, rather than all databases, which is
the default behavior.
.Parameter IncludePreExchange2013
Set this to $false to override the default behavior, which includes
pre-Exchange 2013 databases.
#>
#Requires -Version 4
[CmdletBinding()]
param(
[string[]]$Database,
[bool]$IncludePreExchange2013 = $true
)
Write-Verbose -Message "Determining the connected Exchange environment."
$ExPSSession = @()
$ExPSSession += Get-PSSession |
Where-Object {
$_.ConfigurationName -eq 'Microsoft.Exchange' -and
$_.State -eq 'Opened'
}
if ($ExPSSession.Count -eq 1) {
$Exchange = $null
# Check if we're in Exchange Online or On-Premises.
switch ($ExPSSession.ComputerName) {
outlook.office365.com {
$Exchange = 'Exchange Online'
}
default {
$Exchange = 'Exchange On-Premises'
# Set scope to entire forest (important for multi-domain forests).
Set-ADServerSettings -ViewEntireForest:$true
# Determine if the connected Exchange server's version is 2010 or 2013 and newer.
$ExOnPSrv = Get-ExchangeServer -Identity "$($ExPSSession.ComputerName)"
switch ($ExOnPSrv.AdminDisplayVersion) {
{ $_ -match 'Version 14' } { $LegacyExchange = $true }
{ $_ -match 'Version 15' } { $LegacyExchange = $false }
default {
throw "Unable to determine connect Exchange On-Premises server version. Only Exchange 2010 and newer are supported by this script."
}
}
}
}
Write-Verbose -Message "Connected environment is $($Exchange)."
}
else {
Write-Warning -Message "Requires a single** active (State: Opened) remote session to an Exchange server."
break
}
if ($Exchange -eq 'Exchange Online') {
Write-Warning -Message "The active session is with Exchange Online, which is not supported by this script."
break
}
if ($PSBoundParameters.ContainsKey('Database')) {
$Databases = @()
$Databases +=
if ($LegacyExchange) { foreach ($DB in $Database) { Get-MailboxDatabase -Identity $DB -Status } }
else { foreach ($DB in $Database) { Get-MailboxDatabase -Identity $DB -Status -IncludePreExchange2013:$IncludePreExchange2013 } }
}
else {
$Databases = @()
$Databases +=
if ($LegacyExchange) { Get-MailboxDatabase -Status }
else { Get-MailboxDatabase -Status -IncludePreExchange2013:$IncludePreExchange2013 }
}
if ($Databases.Count -ge 1) {
$Databases = $Databases | Where-Object { ($_.Recovery -eq $false) -and ($_.Mounted -eq $true) }
foreach ($DB in $Databases) {
$MailboxStatistics = Get-MailboxStatistics -Database $DB.Name
$ActiveMailboxes = @()
$ActiveMailboxes += $MailboxStatistics | Where-Object { $null -eq $_.DisconnectReason }
$ArchiveMailboxes = @()
$ArchiveMailboxes += $MailboxStatistics | Where-Object { $_.IsArchiveMailbox -eq $true }
$DisconnectedMailboxes = @()
$DisconnectedMailboxes += $MailboxStatistics | Where-Object { $null -ne $_.DisconnectReason }
$DisabledMailboxes = @()
$DisabledMailboxes += $DisconnectedMailboxes | Where-Object { $_.DisconnectReason -like 'Disabled' }
$SoftDeletedMailboxes = @()
$SoftDeletedMailboxes += $DisconnectedMailboxes | Where-Object { $_.DisconnectReason -like 'SoftDeleted' }
$DBConsumption = [PSCustomObject]@{
Database = $DB.Name
DBSize_GB = [math]::Round(($DB.DatabaseSize -replace '.*\s\(' -replace ',' -replace '\sb.*') / 1024 / 1024 / 1024, 2)
Whitespace_GB = [math]::Round(($DB.AvailableNewMailboxSpace -replace '.*\s\(' -replace ',' -replace '\sb.*') / 1024 / 1024 / 1024, 2)
Mbx_Count = $MailboxStatistics.Count
Mbx_GB = ($MailboxStatistics |
Select-Object @{
Name = 'MSizeGB'
Expression = { (
[math]::Round((
[decimal]($_.TotalItemSize -replace '.*\s\(' -replace ',' -replace '\sb.*') +
[decimal]($_.TotalDeletedItemSize -replace '.*\s\(' -replace ',' -replace '\sb.*')
) / 1024 / 1024 / 1024, 2)
) }
} | Measure-Object -Property MSizeGB -Sum).Sum
ActiveMbx_Count = $ActiveMailboxes.Count
ActiveMbx_GB = ($ActiveMailboxes |
Select-Object @{
Name = 'MSizeGB'
Expression = { (
[math]::Round((
[decimal]($_.TotalItemSize -replace '.*\s\(' -replace ',' -replace '\sb.*') +
[decimal]($_.TotalDeletedItemSize -replace '.*\s\(' -replace ',' -replace '\sb.*')
) / 1024 / 1024 / 1024, 2)
) }
} | Measure-Object -Property MSizeGB -Sum).Sum
ArchiveMbx_Count = $ArchiveMailboxes.Count
ArchiveMbx_GB = ($ArchiveMailboxes |
Select-Object @{
Name = 'MSizeGB'
Expression = { (
[math]::Round((
[decimal]($_.TotalItemSize -replace '.*\s\(' -replace ',' -replace '\sb.*') +
[decimal]($_.TotalDeletedItemSize -replace '.*\s\(' -replace ',' -replace '\sb.*')
) / 1024 / 1024 / 1024, 2)
) }
} | Measure-Object -Property MSizeGB -Sum).Sum
DisconnectedMbx_Count = $DisconnectedMailboxes.Count
DisconnectedMbx_GB = ($DisconnectedMailboxes |
Select-Object @{
Name = 'MSizeGB'
Expression = { (
[math]::Round((
[decimal]($_.TotalItemSize -replace '.*\s\(' -replace ',' -replace '\sb.*') +
[decimal]($_.TotalDeletedItemSize -replace '.*\s\(' -replace ',' -replace '\sb.*')
) / 1024 / 1024 / 1024, 2)
) }
} | Measure-Object -Property MSizeGB -Sum).Sum
DisabledMbx_Count = $DisabledMailboxes.Count
DisabledMbx_GB = ($DisabledMailboxes |
Select-Object @{
Name = 'MSizeGB'
Expression = { (
[math]::Round((
[decimal]($_.TotalItemSize -replace '.*\s\(' -replace ',' -replace '\sb.*') +
[decimal]($_.TotalDeletedItemSize -replace '.*\s\(' -replace ',' -replace '\sb.*')
) / 1024 / 1024 / 1024, 2)
) }
} | Measure-Object -Property MSizeGB -Sum).Sum
SoftDeletedMbx_Count = $SoftDeletedMailboxes.Count
SoftDeletedMbx_GB = ($SoftDeletedMailboxes |
Select-Object @{
Name = 'MSizeGB'
Expression = { (
[math]::Round((
[decimal]($_.TotalItemSize -replace '.*\s\(' -replace ',' -replace '\sb.*') +
[decimal]($_.TotalDeletedItemSize -replace '.*\s\(' -replace ',' -replace '\sb.*')
) / 1024 / 1024 / 1024, 2)
) }
} | Measure-Object -Property MSizeGB -Sum).Sum
}
Write-Output -InputObject $DBConsumption
}
}