1

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

mcavanaugh418
  • 127
  • 1
  • 12
  • 1
    Just get rid of the brackets when calling the function: $members = GetUserOnPage $accessToken [int]$I – Owain Esau Nov 14 '18 at 02:04
  • i tried this and i got the exact same result. also tried with and without the comma. – mcavanaugh418 Nov 14 '18 at 02:09
  • 1
    as Owain Esau pointed out, you are not passing in what you THINK you are passing in. [*grin*] you are passing in an array, not two items. to fix that = remove the parens AND remove the comma. ///// to avoid this really common error in the future, use the parameter names when you call the function. this `GetUserOnPage -AccessToken $accessToken -I $I` would have let you avoid that error. [*grin*] ///// also, PLEASE do not use the same $VarName in a function that you are using outside the function. it makes it far too easy to confuse what goes where. – Lee_Dailey Nov 14 '18 at 02:10
  • Declaring the parameter names solved this. i was a little bit confused on why i absolutely had to do that to make it work as I've never had to in the past but it seems to have resolved this issue. Thank you! – mcavanaugh418 Nov 14 '18 at 02:14
  • @mcavanaugh418 - you are most welcome! glad to have helped ... and the habit of using full parameter names has helped me avoid foot-gunning myself more than once! [*grin*] – Lee_Dailey Nov 14 '18 at 02:57
  • **In PowerShell, functions are invoked _like console (terminal) programs_** - `foo arg1 arg2` - _not_ like C# _methods_ - `foo(arg1, arg2)` - see [`Get-Help about_Parsing`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing). If you accidentally use `,` to separate your arguments, you'll construct an _array_ that a function sees as a _single_ argument. To help avoid accidental use of method syntax, you can use [`Set-StrictMode -Version 2`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode) or higher. – mklement0 Nov 14 '18 at 03:05

1 Answers1

0

Change the below lines and it should clear up as well.

GetUserOnPage($accessToken, [int]$I)

Should be

GetUserOnPage $accessToken $I

Changed answer because of comment

Drew
  • 3,814
  • 2
  • 9
  • 28
  • I set I to read-host and then i had to enter a user prompt to continue. the prompt was automatically a string for some reason even though it is casted as an int and i input a number and it didn't allow another input after that. I've set it to 1 instead of 0 because in the API call i need to call page 1 first. – mcavanaugh418 Nov 14 '18 at 02:10