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
#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
    )

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

    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 = @()
            foreach($d in $databases)
            {
                Write-Verbose ("`tGetting mailboxes of database " + $d.DatabaseName)
                $mailboxes += Get-Mailbox -Database $d.DatabaseName
            }

            # Create a custom object
            $obj = New-Object PSObject -Property @{
                                                    "Server" = $s
                                                    "Mailboxes" = $mailboxes.Count
                                                 }
            $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
    )

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

    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)

                # 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


                $obj
            }
        }
    }

    End {}
}
#endregion

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