Private/WhatsUpGold/_GetWUGDevices.ps1

function _GetWUGDevices {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$GroupID,

        [Parameter(Mandatory = $false)]
        [ValidateSet('id', 'basic', 'card', 'overview')]
        [string]$View = 'card',

        [Parameter(Mandatory = $false)]
        [switch]$ReturnHierarchy,

        [Parameter(Mandatory = $false)]
        [ValidateSet('Unknown', 'Up', 'Down', 'Maintenance', 'Any', 'UpWithDownMonitors')]
        [string]$State,

        [switch]$ShowQuery
    )
    begin {
        $Method = 'Get'

        $APICall = "device-groups/$($GroupID)/devices/-"
        $APICall += "?view=$View"
        if ($State) {
            $APICall += "&state=$State"
        }

        $Data = @()
    }
    process {
        # This part is generic for most queries except for the line that is similar to: $Data += $PagedData.data.****
        try {
            do {
                if ($PagedData) {
                    $PagedAPICall = "${APICall}&pageid=$($PagedData.paging.nextPageId)"
                }
                else {
                    # first request
                    $PagedAPICall = "${APICall}"
                }
                if ($ShowQuery) {
                    Write-Host $PagedAPICall
                }
                $PagedData = _InvokeSTTWUGRestMethod -APICall $PagedAPICall -Method $Method

                # combine the previous object with the new entries
                try {
                    $Data += $PagedData.data.devices
                }
                catch {
                    Write-Error "Unable to combine data. $_"
                }
            } while ($PagedData.paging.nextPageId)
        }
        catch {
            throw
        }
    }
    end {
        return $Data
    }
}