Scripts/Get-BPAConsoleOutput.ps1

function Get-BPAConsoleOutput {
    <#
        .SYNOPSIS
            Gets AutoMate BPA console output.
 
        .DESCRIPTION
            Get-BPAConsoleOutput gets the console output.
 
        .PARAMETER MaxItems
            The maximum number of events in the console output to retrieve.
 
        .PARAMETER BPAServer
            The AutoMate BPA management server.
 
        .EXAMPLE
            # Get console output from server "bpa01"
            Get-BPAConsoleOutput -BPAServer "bpa01"
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 09/13/2016
            Date Modified : 02/28/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

    [CmdletBinding()]
    param(
        [ValidateNotNullOrEmpty()]
        [int]$MaxItems = 20,

        [int]$PollIntervalSeconds,

        [ValidateNotNullOrEmpty()]
        [switch]$Colorize = $false,

        [ValidateNotNullOrEmpty()]
        [string]$BPAServer
    )

    if ($BPAServer) {
        $thisConnectionInfo = $BPAConnectionInfo | Where-Object {$_.Server -eq $BPAServer}
    } else {
        $thisConnectionInfo = $BPAConnectionInfo
    }
    if ($null -eq $thisConnectionInfo) {
        throw "Invalid server specified or no servers are currently connected!"
    }

    $pollContinuously = $PSBoundParameters.ContainsKey("PollIntervalSeconds")
    $serverStartIndex = @{}
    $serverLastMessageCount = @{}
    do {
        foreach ($connection in $thisConnectionInfo) {
            if (-not $serverStartIndex.ContainsKey($connection.Server)) {
                $serverStartIndex += @{$connection.Server = -1}
            }
            if (-not $serverLastMessageCount.ContainsKey($connection.Server)) {
                $serverLastMessageCount += @{$connection.Server = 0}
            }
            if ($serverStartIndex[$connection.Server] -eq -1) { # First poll
                $serverIndex = (Invoke-BPARestMethod -Resource "output/list" -RestMethod "Get" -BPAServer $connection.Server).IndexOfLastMessage -as [int]
                if (($serverIndex - $MaxItems) -ge 0) {
                    $serverStartIndex[$connection.Server] = $serverIndex - $MaxItems
                } else {
                    $serverStartIndex[$connection.Server] = 0
                }
            } else {
                $indexAdd = $serverLastMessageCount[$connection.Server]
                if ($indexAdd -gt 0) { $indexAdd += 1 }
                $serverStartIndex[$connection.Server] = $serverStartIndex[$connection.Server] + $indexAdd
            }
            Write-Verbose "Using index $($serverStartIndex[$connection.Server]) for server $($connection.Server) (last query returned $($serverLastMessageCount[$connection.Server]) messages)."
            $temp = Invoke-BPARestMethod -Resource "output/list?start_index=$($serverStartIndex[$connection.Server])&page_size=$MaxItems" -RestMethod "Get" -BPAServer $connection.Server
            $serverLastMessageCount[$connection.Server] = ($temp.Messages | Measure-Object).Count
            foreach ($message in $temp.Messages) {
                $message.TimeStamp = $message.TimeStamp.ToLocalTime()
                $message | Add-Member -MemberType NoteProperty -Name "BPAServer" -Value $connection.Server
                if ($Colorize.ToBool()) {
                    switch ($message.Type) {
                        1       { Write-Host "$(Get-Date $message.TimeStamp -Format G) - $($message.Text) ($($message.BPAServer))" -ForegroundColor Red }
                        default { Write-Host "$(Get-Date $message.TimeStamp -Format G) - $($message.Text) ($($message.BPAServer))"}
                    }
                } else {
                    $message
                }
            }
        }
        if ($pollContinuously) {
            Start-Sleep -Seconds $PollIntervalSeconds
        }
    } until (-not $pollContinuously)
}