cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
9412
Views
8
Helpful
11
Replies

Using Powershell how to paginate thru pages.

smeyer1
Community Member

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

Accepted Solutions

JasperAspect
Community Member

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

11 Replies 11

riduque
Cisco Employee
Cisco 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

smeyer1
Community Member

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

Philip D'Ath
Meraki Community All-Star
Meraki Community All-Star

Did you spot this bit from the link @riduque 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: first, prev, next, 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.

riduque
Cisco Employee
Cisco Employee

Have you tried using this parameter in your query?

perPage=1000&startingAfter=1001

smeyer1
Community Member

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

This doesnt work, does the same thing as perPage=1000

Philip D'Ath
Meraki Community All-Star
Meraki Community All-Star

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
Community Member

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
}

smeyer1
Community Member

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.