AnsweredAssumed Answered

How to retrieve filtered asset list w/ pagination?

Question asked by Nate Olsen on Jan 30, 2020
Latest reply on Feb 4, 2020 by Pablo Quiroga

I am writing powershell to retrieve an asset list using filter operatingSystem.category1:Windows.

 

When I pass this filter via the URL query string, I receive a batch of assets matching the filter and the response parameter gives me the lastSeenAssetId.

 

I cannot get this API endpoint to return the next batch of assets matching this filter. I *always* get a null response when retrieving the second batch of asset data.

 

If I use the unfiltered API endpoint, I can retrieve ALL assets using pagination exactly as described in the API docs.

 

What a clusterF***.

 

current powershell looks like:

Function Get-AssetList {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [hashtable]
        $AuthToken
    )
    $filter = @filter = "operatingSystem.category1:Windows" }
    $total_count = (Get-AssetCount -AuthToken $AuthToken -filter $filter).count
    $fetch_count = 0
    $asset_uri = Get-AssetURI("/assets/host/filter/list")
    $asset_hdrs = @Accept = "*/*" 
                     ContentType = "application/json" }
    $asset_hdrs += $AuthToken
    Write-Verbose "Headers:`t$asset_hdrs`n"
    Write-Verbose "URI:`t`t$asset_uri`n"
    Write-Verbose "Query:`t`t$query`n"
    $hasMore = $true
    try {
        while ($hasMore) {
            Write-Progress -Activity "Qualys" -Status "Fetching Asset List..." -PercentComplete ($fetch_count/$total_count*100
            $url_query = [string](Convert-HostListParametersHashtableToQueryString -hashtable $filter)
            $response = Invoke-RestMethod -Method Post -Headers $asset_hdrs -Uri $($asset_uri + $url_query) -UserAgent "powershell"
            $output += ($response.assetListData.asset | 
                ForEach-Object { 
                        [PSCustomObject@AssetID = $_.AssetIDAssetName = (Get-NormalizedAssetName -name $_.AssetName) }
                })
            $hasMore = $response.hasMore
            $fetch_count += $response.count
            if ($filter.ContainsKey("lastSeenAssetId")) { $filter.Remove("lastSeenAssetId") }
            $filter.add("lastSeenAssetId"$response.lastSeenAssetId)
        }
    } catch {
        $response
        $err = $_
        Write-Host $err.CategoryInfo -ForegroundColor Red
        
        #$hasMore = $false
    }
    return $output
}

Outcomes