Using Powershell how to paginate thru pages.

Solved
StephenMeyer
Here to help

Using Powershell how to paginate thru pages.

I am trying to get network events for last 30 days, the /network/{networkid}/events has a perPage value of 3 to 1000. The documentation is unclear how to pageinate the calls. Does anyone have a working example of pulling several pages of events and placing into a spreadsheet. 

1 Accepted Solution
JasperAspect
New here

Just happend to have a similar issue. I came up with this function. Hope this helps

 

 
 
Function Get-Clients {
param (
        [Parameter(mandatory=$true)]$MerakiApiKey,
        [Parameter(mandatory=$true)]$networkId,
$perPage = "50"
    )
 
 
$headers = @{
"X-Cisco-Meraki-API-Key" = $MerakiapiKey
"Content-Type" = "application/json"
"Accept" = "application/json"
}
 
$ClientBody = @{
perPage = $perPage
}
 
$AllClientData = $null
    do {
        Write-Host "Requesting clients from $clientsurl"
        $result = Invoke-WebRequest -Uri $clientsurl -Headers $Headers -Body $ClientBody
        $AllClientData += $result.Content | ConvertFrom-Json
        
        $clientsurl = $result.RelationLink.next
 
    } until ($clientsurl -notlike "https://api.meraki.com/*")
 
return $AllClientData
    
}

 

 

View solution in original post

10 Replies 10
RicardoD
Meraki Employee
Meraki Employee

You can include this parameter to reduce the amount of pages:

perPage: Range is 3 - 1000. Default is 10.

 

The entire explanation about how Pagination works in Meraki APIs, including an example, is here: https://developer.cisco.com/meraki/api-v1/pagination/#pagination

I have maxed out the permanent. WhatsApp need is how to get to the next 1000 entries

Did you spot this bit from the link @RicardoD supplied?

"When you send an API request to a paginated operation, the number of records that are actually queried in the database is equal to the value of perPage. Then, the HTTP response will contain a custom header named Link. The Link header is a comma-separated list of up to 4 links: firstprevnext, and last. These links represent subsequent requests that can be used to navigate the paginated records. These links will include appropriate values for the startingAfter or endingBefore parameters in order to achieve this navigation. The exact format of the Link header might look something like:"

There is no links returned in the response. This is why documentation is not very well.

RicardoD
Meraki Employee
Meraki Employee

Have you tried using this parameter in your query?

perPage=1000&startingAfter=1001

 

Get GetBlueToothclients and Get NetworkEvents, no change same data coming down each time.

PhilipDAth
Kind of a big deal
Kind of a big deal

Going sideways - have you considered using the Meraki Python SDK for this?  It does all the pagination automatically for you, so you can ignore the issue.

Unforturnately is has to be written in powershell.

JasperAspect
New here

Just happend to have a similar issue. I came up with this function. Hope this helps

 

 
 
Function Get-Clients {
param (
        [Parameter(mandatory=$true)]$MerakiApiKey,
        [Parameter(mandatory=$true)]$networkId,
$perPage = "50"
    )
 
 
$headers = @{
"X-Cisco-Meraki-API-Key" = $MerakiapiKey
"Content-Type" = "application/json"
"Accept" = "application/json"
}
 
$ClientBody = @{
perPage = $perPage
}
 
$AllClientData = $null
    do {
        Write-Host "Requesting clients from $clientsurl"
        $result = Invoke-WebRequest -Uri $clientsurl -Headers $Headers -Body $ClientBody
        $AllClientData += $result.Content | ConvertFrom-Json
        
        $clientsurl = $result.RelationLink.next
 
    } until ($clientsurl -notlike "https://api.meraki.com/*")
 
return $AllClientData
    
}

 

 

StephenMeyer
Here to help

thanks, what i found out from your answer is that Invoke-RestMethod only returns a JSON object, and does not allow you access to the Headers in the response. Thanks.

Get notified when there are additional replies to this discussion.
Welcome to the Meraki Community!
To start contributing, simply sign in with your Cisco account. If you don't yet have a Cisco account, you can sign up.