Private/WhatsUpGold/_GetWUGDeviceGroups.ps1

function _GetWUGDeviceGroups {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false, ParameterSetName= 'GroupID')]
        [string]$GroupID,

        [Parameter(Mandatory = $false, ParameterSetName= 'All')]
        [switch]$All,

        [Parameter(Mandatory = $false, ParameterSetName= 'GroupID')]
        [Parameter(Mandatory = $false, ParameterSetName= 'All')]
        [ValidateSet('Summary', 'Detail')]
        [string]$View = 'Summary',

        [Parameter(Mandatory = $false, ParameterSetName= 'GroupID')]
        [Parameter(Mandatory = $false, ParameterSetName= 'All')]
        [switch]$ShowQuery
    )
    begin {
        $Method = 'Get'

        if ($All) {
            $APICall = 'device-groups/-'
        } else {
            $APICall = "device-groups/$GroupID"
        }

        $APICall += "?view=$View"

        $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.groups
                }
                catch {
                    Write-Error "Unable to combine data. $_"
                }
            } while ($PagedData.paging.nextPageId)
        }
        catch {
            throw
        }
    }
    end {
        return $Data
    }
}