I'm trying to call an API with a powershell script to return a paginated dump of all users and there values. I get a page count which populates the number of pages I need to call. I put that value into a for loop and the int increases with each run of the loop. In the middle of the Loop when I pass $I into my function the Function gets 0 instead of the number being passed in.
Function GetUserOnPage ([string]$AccessToken, [int]$I)
{
write-host $I 'the loaded page'
$Header=$null
$Header = @{};
$Header.Add("Authorization",'Bearer '+ $AccessToken)
$URL='https://mycompany.myapplication.com/api/member?page='+ $I
write-host $URL
$request = Invoke-webrequest -UseDefaultCredentials -Method Get -uri $URL -Headers $Header -ContentType application/x-www-form-urlencoded
$JsonParameters = ConvertFrom-Json -InputObject $request.content
$memberList = $JsonParameters.member_list
return $memberList
}
Function Execute()
{
BuildDataTable
$accessToken = LogintoBI
$pageCount = GetUserPageCount($accessToken)
$pages = $pageCount
For($I = 1; $I -le $pages; $I++)
{
Write-host 'counting up' $I
$members = GetUserOnPage($accessToken, [int]$I)
write-host 'checking' $I
Foreach($member in $members)
{
AddMemberToTable($member)
}
}
}
Execute
Below is the returns i'm putting in with the write-host to check my values
counting up 1
0 the loaded page
https://mycompany.myapplication.com/api/member?page=0
checking 1
counting up 2
0 the loaded page
https://mycompany.myapplication.com/api/member?page=0
checking 2
counting up 3
0 the loaded page
https://mycompany.myapplication.com/api/member?page=0
checking 3
counting up 4
0 the loaded page
https://mycompany.myapplication.com/api/member?page=0
checking 4
counting up 5
0 the loaded page
https://mycompany.myapplication.com/api/member?page=0
checking 5
counting up 6
0 the loaded page
https://mycompany.myapplication.com/api/member?page=0
checking 6