Public/Get-BrewZilla.ps1

function Get-BrewZilla {
    <#
        .NAME
        Get-BrewZilla
        .SYNOPSIS
        Get information about your BrewZilla(s)
        .PARAMETER Id
        Id of your BrewZilla
        .OUTPUTS
        System.Object
        .EXAMPLE
        Get-BrewZilla
        .EXAMPLE
        Get-BrewZilla -Id $id
        .RELATED LINKS
        https://api.rapt.io/index.html
    #>

    [CmdletBinding()][OutputType('System.Management.Automation.PSObject')]
    Param (
        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string]$Id
    )
    Begin {
        # Check if connection to RAPT portal exists
        If (-not (Get-Variable -Name raptObj -Scope Script -ErrorAction SilentlyContinue)) {
            throw "Please connect to the RAPT.io portal using the Connect-Rapt cmdlet"
        }
        If ($PSBoundParameters.ContainsKey('Id')) {
            $uri = "$($Script:raptObj.baseUri)/api/BrewZillas/GetBrewZilla"
        } else {
            $uri = "$($Script:raptObj.baseUri)/api/BrewZillas/GetBrewZillas"
        }
        # Check time left on token; if less than 2 minutes, refresh it.
        $timeLeft = New-TimeSpan $(Get-Date) $Script:raptObj.expireTime
        If ($timeLeft.Minutes -LT 2 -and $Script:raptObj) {
            Connect-Rapt -username $Script:raptObj.username -apiKey $Script:raptObj.apiKey
        }
        # Use the script-scoped token
        $token = $Script:raptObj.accessToken
        $header = @{
            'Accept' = 'application/json'
            'Accept-Encoding' = 'gzip, deflate, br'
            'Authorization' = "Bearer ${token}"
        }
        If ($PSBoundParameters.ContainsKey('Id')) {
            $body = @{
                brewZillaId = $Id
            }
        }
    }
    Process {
        try {
            $params = @{
                uri = $uri
                headers = $header
                method = 'GET'
            }
            If ($PSBoundParameters.ContainsKey('Id')) {
                $params.body = $body
            }
            $response = Invoke-RestMethod @params
        }
        catch [Exception] {
            throw $_.Exception
        }
        return $response
    }
    End {
    }
}