Public/Instances/Get-ABInstances.ps1

function Get-ABInstances {
    [CmdletBinding()]
    param(
        
        [Parameter(Mandatory = $true)]
        [int]
        $TemplateId,

        [datetime]
        $StartDate,

        [datetime]
        $EndDate,

        [string[]]
        $States,

        [string[]]
        $StateDetails,

        [int]
        $PageSize,

        [int]
        $PageIndex,
        
        [switch]$IncludeVariables,

        [switch]$OldestFirst,

        [switch]$All
    )
    
    begin {
        if ($All) { 
            $pageSize = 0xFFFFFF
            $pageIndex = 1 
        }     
    }
    
    process {
        $Path = Get-ABPath -Path "GetInstances" 

        [System.UriBuilder]$Uri = "$($_ABSession.Uri)$Path"

        $HttpQSCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)

        if ($TemplateId) { $HttpQSCollection.Add('templateId', $TemplateId) }
        if ($PageSize) { $HttpQSCollection.Add('pageSize', $PageSize) }
        if ($PageIndex) { $HttpQSCollection.Add('pageIndex', $PageIndex) }
        if ($StartDate) { $HttpQSCollection.Add('startDate', $StartDate) }
        if ($EndDate) { $HttpQSCollection.Add('endDate', $EndDate) }
        if ($States) { $HttpQSCollection.Add('states', ($States -join ",")) }
        if ($StateDetails) { $HttpQSCollection.Add('stateDetails', ($StateDetails -join ",")) }        
        if ($IncludeVariables) { $HttpQSCollection.Add('includeVariables', 'true') }            
        if ($OldestFirst) { $HttpQSCollection.Add('oldestFirst', 'true') } 

        $uri.Query = $HttpQSCollection.ToString()
                
        Write-Verbose $Uri.Uri.OriginalString 
        Invoke-ABRestMethod -Uri $Uri.Uri.OriginalString -method "GET"
    }
    
    end {
        
    }
}