MailboxDistribution.psm1

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
#region Get-MailboxesPerServer
Function Get-MailboxesPerServer
{
    <#
    .SYNOPSIS
 
    Get the number of mailboxes per server.
    .DESCRIPTION
 
    The Get-MailboxesPerServer cmdlet returns the total number of mailboxes per server.
    .PARAMETER Server
 
    The name of the exchange server.
    .EXAMPLE
    Get-MailboxesPerServer -Server Exchange2013
 
    This command will return the number of mailboxes on server Exchange2013
 
    .EXAMPLE
    Get-MailboxServer | Get-MailboxesPerServer
 
    This command will return the number of mailboxes on all mailbox servers, per server.
    #>


    [cmdletBinding()]

    Param
    (
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('Name')]
        [string[]]$Server = $env:COMPUTERNAME,

        # Include AutoSuspended mailboxes
        [Parameter(Mandatory = $false, Position = 1)]
        [switch]$IncludeAutoSuspended
    )

    Begin
    {
        try
        {
            Get-Command "Get-Mailbox" -ErrorAction Stop |
                Out-Null
        }
        catch
        {
            Throw "Could not find the Exchange cmdlets."
        }

        #region Get the move requests
        if($IncludeAutoSuspended)
        {
            Write-Verbose "Getting AutoSuspended move requests."
            $moveRequests = Get-MoveRequest -MoveStatus "AutoSuspended" -ResultSize Unlimited
        }
        #endregion

    }

    Process
    {
        # Loop through the servers
        foreach($s in $Server)
        {
            # Get the databases
            Write-Verbose ("Getting databases of server " + $s)
            $databases = Get-MailboxDatabaseCopyStatus -Server $s | Where-Object {$_.Status -eq "Mounted"}

            # Get the mailboxes on throse databases
            Write-Verbose ("Getting mailboxes of databases")
            $mailboxes = @()
            $autosuspended = 0
            foreach($d in $databases)
            {
                Write-Verbose ("`tGetting mailboxes of database " + $d.DatabaseName)
                $mailboxes += Get-Mailbox -Database $d.DatabaseName

                if($IncludeAutoSuspended)
                {
                    $autosuspended += @($moveRequests | Where-Object {$_.TargetDatabase -eq $d.DatabaseName}).Count
                }
            }

            # Create a custom object
            $obj = New-Object PSObject -Property @{
                                                    "Server" = $s
                                                    "Mailboxes" = $mailboxes.Count
                                                 }

            #region Get the autosuspended mailboxes of the server
            if($IncludeAutoSuspended)
            {
                Write-Verbose "Getting AutoSuspended mailboxes of $s"
                if($autosuspended -eq $null)
                {
                    $autosuspended = 0
                }
                $obj | Add-Member NoteProperty "AutoSuspendedMailboxes" $autosuspended
            }
            #endregion
            $obj
        }
    }

    End {}
}
#endregion

#region Get-MailboxesPerDatabase
Function Get-MailboxesPerDatabase
{
    <#
    .SYNOPSIS
 
    Get the number of mailboxes per database.
    .DESCRIPTION
 
    The Get-MailboxesPerDatabase cmdlet returns the total number of mailboxes per database on an exchange server.
    .PARAMETER Server
 
    The name of the exchange server.
    .EXAMPLE
    Get-MailboxesPerDatabase -Server Exchange2013
 
    This command will return the number of mailboxes per database on server Exchange2013
 
    .EXAMPLE
    Get-MailboxServer | Get-MailboxesPerDatabase
 
    This command will return the number of mailboxes per database on all mailbox servers.
    #>


    [cmdletBinding()]

    Param
    (
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string[]]$Server = $env:COMPUTERNAME,

        # Include AutoSuspended mailboxes
        [Parameter(Mandatory = $false, Position = 1)]
        [switch]$IncludeAutoSuspended
    )

    Begin
    {
        try
        {
            Get-Command "Get-Mailbox" -ErrorAction Stop |
                Out-Null
        }
        catch
        {
            Throw "Could not find the Exchange cmdlets."
        }    

        #region Get the move requests
        if($IncludeAutoSuspended)
        {
            Write-Verbose "Getting AutoSuspended move requests."
            $moveRequests = Get-MoveRequest -MoveStatus "AutoSuspended" -ResultSize Unlimited
        }
        #endregion
    }

    Process
    {
        # Loop through the servers
        foreach($s in $Server)
        {
            # Get the databases
            Write-Verbose ("Getting databases of server " + $s)
            $databases = Get-MailboxDatabaseCopyStatus -Server $s | Where-Object {$_.Status -eq "Mounted"}

            # Loop through databases
            Write-Verbose ("Getting mailboxes of databases")
            foreach($d in $databases)
            {
                # Get the mailboxes
                Write-Verbose ("`tGetting mailboxes of database " + $d.DatabaseName)
                $mailboxes = @(Get-Mailbox -Database $d.DatabaseName)

                # The autosuspended mailboxes
                $autosuspended = 0

                # Create a custom object
                $obj = New-Object PSObject
                $obj | Add-Member NoteProperty "Server" $s
                $obj | Add-Member NoteProperty "Database" $d.databasename
                $obj | Add-Member NoteProperty "Mailboxes" $mailboxes.Count


                if($IncludeAutoSuspended)
                {
                    $autosuspended = @($moveRequests | Where-Object {$_.TargetDatabase -eq $d.DatabaseName}).Count
                    if($autosuspended -eq $null)
                    {
                        $autosuspended = 0
                    }
                    $obj | Add-Member NoteProperty "AutoSuspendedMailboxes" $autosuspended
                }

                $obj
            }
        }
    }

    End {}
}
#endregion

#region Get-MilboxesPerDatabaseAvailabilityGroup
Function Get-MailboxesPerDatabaseAvailabilityGroup
{
    <#
    .Synopsis
       Get the mailbox distribution on DAGs
    .DESCRIPTION
       Get the mailbox distribution on Database Availability Groups, optionaly including AutoSuspended mailboxes.
    .EXAMPLE
        [PS] C:\>Get-MailboxesPerDatabaseAvailabilityGroup -DatabaseAvailabilityGroup DAG1
 
        DAG Servers Databases Mailboxes
        --- ------- --------- ---------
        DAG1 8 18 851
 
        Get the mailboxes on DAG1.
     
    .EXAMPLE
        [PS] C:\>Get-DatabaseAvailabilityGroup | Get-MailboxesPerDatabaseAvailabilityGroup
        DAG Servers Databases Mailboxes
        --- ------- --------- ---------
        DAG1 8 18 851
        DAG2 4 9 259
         
        Get the mailboxes on all Database Availability Groups
 
    .EXAMPLE
        [PS] C:\>Get-DatabaseAvailabilityGroup DAG1 | Get-MailboxesPerDatabaseAvailabilityGroup -IncludeAutoSuspended | Format-Table
 
        DAG Servers Databases Mailboxes AutoSuspendedMailboxes
        --- ------- --------- --------- ----------------------
        DAG1 8 18 851 34
 
        Get the mailboxes on DAG1 including the AutoSuspended.
    .NOTES
       The AutoSuspended mailboxes are not included in the database mailboxes.
    #>


    [cmdletBinding()]

    Param
    (
        # The DAG to query
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('Name')]
        [string[]]$DatabaseAvailabilityGroup,

        # Include AutoSuspended mailboxes
        [Parameter(Mandatory = $false, Position = 1)]
        [switch]$IncludeAutoSuspended        
    )

    Begin
    {
        #region Environment Check
        # Check if Exchange cmdlets are available
        Write-Verbose "Checking for Exchange cmdlets."
        try
        {
            Get-Command "Get-DatabaseAvailabilityGroup" -ErrorAction Stop |
                Out-Null
        }
        catch
        {
            Throw "Could not find Exchange cmdlets."
        }
        #endregion

        #region Get the move requests
        if($IncludeAutoSuspended)
        {
            Write-Verbose "Getting AutoSuspended move requests."
            $moveRequests = Get-MoveRequest -MoveStatus "AutoSuspended" -ResultSize Unlimited
        }
        #endregion
    }

    Process
    {
        #region Get Database Availability Groups
        if($DatabaseAvailabilityGroup)
        {
            $dags = @()
            foreach($dagp in $DatabaseAvailabilityGroup)
            {
                try
                {
                    Write-Verbose "Getting Database Availability Group $dagp."
                    $dags += Get-DatabaseAvailabilityGroup $dagp -ErrorAction Stop
                }
                catch
                {
                    Write-Error "Could not find a Database Availability Group for $dagp"
                }
            }
        }
        else
        {
            Write-Verbose "Getting Database Availability Groups."
            $dags = Get-DatabaseAvailabilityGroup
        }
        #endregion

        #region Process Each DAG
        foreach($d in $dags)
        {
            Write-Verbose "`tProcessing DAG $($d.Name)"

            #region Create a custom object per DAG
            $obj = New-Object psObject
            $obj | Add-Member NoteProperty "DAG" $d.Name
            #endregion

            #region Get the servers of the DAG
            Write-Verbose "`tGetting servers of $($d.Name)"
            # Exchange Shell and Implicit Remoting return different objects
            # and we need to check both
            $srvs = $d.Servers
            if($srvs.Name -ne $null)
            {
                $servers = $srvs.Name
            }
            else
            {
                $servers = $srvs
            }
            $obj | Add-Member NoteProperty "Servers" $servers.Count
            #endregion

            #region Get the databases on these servers
            Write-Verbose "`tGetting databases of $($d.Name)"
            $databases = $servers | %{ Get-MailboxDatabaseCopyStatus -Server $_ | Where-Object {$_.Status -eq "Mounted"} } | % DatabaseName
            $obj | Add-Member NoteProperty "Databases" $databases.count
            #endregion

            #region Get the mailboxes of the database
            Write-Verbose "`tGetting mailboxes of $($d.Name)"
            $mailboxes = $databases | %{ Get-mailbox -Database $_ -ResultSize unlimited}
            $obj | Add-Member NoteProperty "Mailboxes" $mailboxes.count
            #endregion

            #region Get the autosuspended mailboxes of the dag
            if($IncludeAutoSuspended)
            {
                Write-Verbose "Getting AutoSuspended mailboxes of $($d.Name)"
                $autosuspended = $moveRequests | Where-Object {$_.TargetDatabase -in $databases}
                $obj | Add-Member NoteProperty "AutoSuspendedMailboxes" $autosuspended.Count
            }
            #endregion

            # Send the custom object to the pipeline
            $obj
        }
        #endregion
    }

    End{}
}
#endregion

#region Exports
Export-ModuleMember -Function Get-MailboxesPerDatabase
Export-ModuleMember -Function Get-MailboxesPerServer
Export-ModuleMember -Function Get-MailboxesPerDatabaseAvailabilityGroup
#endregion