ActiveUsers.psm1

#region Functions

#region Get-OWAUsers
function Get-OWAUsers
{
    <#
        .SYNOPSIS
        Gets the number of users currectly connected to Outlook Web App.
 
        .PARAMETER ComputerName
        The computer to query.
    #>


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

    Begin{}

    Process
    {

        foreach($c in $ComputerName)
        {            
            $counter = Get-Counter -Counter "\MSExchange OWA\Current Unique Users" -MaxSamples 1 -ComputerName $c

            $obj = New-Object psobject
            $obj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $c
            $obj | Add-Member -MemberType NoteProperty -Name OWAUsers -Value $counter.CounterSamples[0].CookedValue
            $obj
        }
    }
        
    End{}
}
#endregion

#region Get-RPCUsers
function Get-RPCUsers
{
    <#
        .SYNOPSIS
        Gets the number of rpc users currently connected.
 
        .PARAMETER ComputerName
        The computer to query.
    #>


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

    Begin{}

    Process
    {

        foreach($c in $ComputerName)
        {            
            $counter = Get-Counter "\MSExchange RpcClientAccess\User Count" -MaxSamples 1 -ComputerName $c

            $obj = New-Object psobject
            $obj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $c
            $obj | Add-Member -MemberType NoteProperty -Name RPCUsers -Value $counter.CounterSamples[0].CookedValue
            $obj
        }
    }
        
    End{}
}
#endregion

#region Get-ActiveSyncUsers
function Get-ActiveSyncUsers
{
    <#
        .SYNOPSIS
        Gets the number of currently connected active sync users
 
        .PARAMETER ComputerName
        The computer to query.
    #>


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

    Begin{}

    Process
    {

        foreach($c in $ComputerName)
        {            
            $counter = Get-Counter "\MSExchange ActiveSync\Current Requests" -MaxSamples 1 -ComputerName $c

            $obj = New-Object psobject
            $obj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $c
            $obj | Add-Member -MemberType NoteProperty -Name ActiveSyncUsers -Value $counter.CounterSamples[0].CookedValue
            $obj
        }
    }
        
    End{}
}
#endregion

#region Get-ExchangeUsers
function Get-ExchangeUsers
{
    <#
        .SYNOPSIS
        Gets the number of connected users on an Exchange server.
 
        .PARAMETER ComputerName
        The computer to query.
    #>


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

    Begin{}

    Process
    {

        foreach($c in $ComputerName)
        {
            $owa = Get-OWAUsers -ComputerName $c
            $rpc = Get-RPCUsers -ComputerName $c
            $activesync = Get-ActiveSyncUsers -ComputerName $c

            $obj = New-Object psobject
            $obj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $c
            $obj | Add-Member -MemberType NoteProperty -Name OWAUsers -Value $owa.owausers
            $obj | Add-Member -MemberType NoteProperty -Name RPCUsers -Value $rpc.rpcusers
            $obj | Add-Member -MemberType NoteProperty -Name ActiveSyncUsers -Value $activesync.activesyncusers
            $obj
        }
    }
        
    End{}
}
#endregion

#endregion

#region Exports
Export-ModuleMember -Function Get-OWAUsers
Export-ModuleMember -Function Get-RPCusers
Export-ModuleMember -Function Get-ActiveSyncUsers
Export-ModuleMember -Function Get-ExchangeUsers
#endregion