0

I am trying to find a better solution to run the Do loop, Until all contacts are Collected. Currently I have over estimated the amount of pages to just go until 20 pages. Is there a better method.

I tried $responseheader.HAS_MORE_RECORDS -notcontains "TRUE" but that was not returning the results.

$results = [System.Collections.ArrayList]@()
do {

    #Starting URL, 50 contacts per page
    $url = "api/v1/contacts/&page=$Page"
    

    $response = Invoke-RestMethod $url -Method Get -Headers $Header -ResponseHeadersVariable responseheader

    # Adds each Response to Results array
    $response.foreach({$results.add($_)}) | out-null

    #increases Page number
    $page++

#Ends Do loop at page 20
} until ($Page -eq 20)
Danimal
  • 11
  • 1
  • Can you show `$responseheader | Get-Member` on your question? – Santiago Squarzon Mar 30 '22 at 00:18
  • Sorry I am a little newer to Powershell, do you want the methods returned when i run that at the end of the script? – Danimal Mar 30 '22 at 11:56
  • Just the properties, if `HAS_MORE_RECORDS` is an actual property you could definitely use it – Santiago Squarzon Mar 30 '22 at 12:07
  • Ahh, I see what you are saying, it looks like The only properties I have, though there are also Methods, if you would like to see those too: Item Comparer Count IsFixedSize IsReadOnly IsSynchronized Keys SyncRoot Values – Danimal Mar 30 '22 at 17:11
  • If `HAS_MORE_RECORDS` is a `bool` property then you should be able to use it to stop your loop. You can check the property type using `Get-Member` as I showed in previous comment – Santiago Squarzon Mar 30 '22 at 17:43
  • it looks like I don't have that property on the API I am currently working on. Thank you for the help that explains why I am unable to use that property. – Danimal Apr 01 '22 at 12:18

1 Answers1

0

The api doesn't return the number of pages in its response?

Loop until the number of returned items is less than 50.

jeroenflvr
  • 316
  • 2
  • 4