MailboxDistribution.psm1

#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