Public/Get-JuribaApplicationV1.ps1

#requires -Version 7
function Get-JuribaApplicationV1 {
    <#
        .SYNOPSIS
        Gets Dashworks application from V1 API.

        .DESCRIPTION
        Gets Dashworks application from V1 API.

        .PARAMETER Filter

        Optional. Query string appended to the request URL to filter applications, for example '$listid=123'.

        .PARAMETER Instance

        Optional. Juriba instance to be provided if not authenticating using Connect-Juriba. For example, https://myinstance.dpc.juriba.app

        .PARAMETER APIKey

        Optional. API key to be provided if not authenticating using Connect-Juriba.

        .EXAMPLE
        PS> Get-JuribaApplicationV1 -Instance "https://myinstance.dpc.juriba.app" -APIKey "xxxxx"

        .EXAMPLE
        PS> Get-JuribaApplicationV1 -Instance "https://myinstance.dpc.juriba.app" -APIKey "xxxxx" -Filter '$listid=123'

    #>

    [alias("Get-DwApplicationV1")]
    param (
        [parameter(Mandatory=$false)]
        [string]$Filter,
        [Parameter(Mandatory=$false)]
        [string]$Instance,
        [Parameter(Mandatory=$false)]
        [string]$APIKey
    )

    if ((Get-Variable 'dwConnection' -Scope 'Global' -ErrorAction 'Ignore') -and !$APIKey -and !$Instance) {
        $APIKey = ConvertFrom-SecureString -SecureString $dwConnection.secureAPIKey -AsPlainText
        $Instance = $dwConnection.instance
    }

    if ($APIKey -and $Instance) {
        $limit = 5000
        $uri = "{0}/apiv1/applications" -f $Instance

        $uri += "?"
        $uri += $Filter
        $uri += "&limit={0}" -f $limit

        $headers = @{'x-api-key' = $APIKey}

        try {
            $result = Invoke-WebRequest -Uri $uri -Method GET -Headers $headers -ContentType "application/json"
            return (($result.content) | ConvertFrom-Json)
        }
        catch {
            if ($_.Exception.Response.StatusCode.Value__ -eq 404) {
                # 404 means the application was not found, don't treat this as an error
                # as we expect this function to be used to check if a application exists
                Write-Verbose "applications not found"
            }
            else {
                Write-Error $_
            }
        }
    } else {
        Write-Error "No connection found. Please ensure `$APIKey and `$Instance is provided or connect using Connect-Juriba before proceeding."
    }
}