Public/CloudFactory/Get-CFCustomers.ps1

function Get-CFCustomers {
    [CmdletBinding()]
    param (
        [Parameter()]
        [switch]$All = $false,
        [Parameter()]
        [string]$Filter = $null

        ##TODO REFACTOR FILTERING
        ## NEED search by id, name or external service
        ## Need to be able to filter by external service, e.g. Acronis,Microsoft tenant id, etc.
        ## CF does not support those filters except for id
    )

    # Invoke CloudFactory API to get customers
    try {
        if( $All ) {
            $PageSize = 250
        }
        else {
            $PageSize = 25
        }

        $customersEndpoint = "v2/customers/Customers"

        Write-ModuleLog -Message "Fetching customers from CloudFactory API..." -Level Verbose -Component 'CloudFactoryCustomers'
        $CFcustomers = @()
        $nextLink = $customersEndpoint + "?PageSize=${PageSize}&PageIndex=1"
        if($Filter) {
            $nextLink += "&Filter.Name=${Filter}"
            Write-ModuleLog -Message "Using filter: $Filter - Nextlink is now: ${nextLink}" -Level Verbose -Component 'CloudFactoryCustomers'

        }
        do {
            $response = Invoke-CFApi -Uri $nextLink
            $CFcustomers += $response.results

            if (-not $All) {
                Write-ModuleLog -Message "Fetched $($CFcustomers.Count) customers from CloudFactory API." -Level Verbose -Component 'CloudFactoryCustomers'
                return $CFcustomers
            }
        
            $metadata = $response.metadata
            if([int]$metadata.page -lt [int]$metadata.totalPages) {
                $nextLink = $customersEndpoint +"?PageSize=${PageSize}&PageIndex=$($metadata.page + 1)"
                Write-ModuleLog -Message "Fetching page $($metadata.page + 1) via Next page: $nextLink" -Level Verbose -Component 'CloudFactoryCustomers'
            } else {
                $nextLink = $null
            }
        } while ($null -ne $nextLink)

        Write-ModuleLog -Message "Fetched $($CFcustomers.Count) customers from CloudFactory API." -Level Verbose -Component 'CloudFactoryCustomers'
        return $CFcustomers
    }
    catch {
        Write-ModuleLog -Message "Failed to retrieve customers for account $Account" -Level Error -Component 'CloudFactoryCustomers' -ErrorRecord $_
        throw
    }
}