Getting unexpected Int value when passing value to powershell function as parameter [duplicate]










1
















This question already has an answer here:



  • How do I pass multiple parameters into a function in PowerShell?

    14 answers



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












share|improve this question













marked as duplicate by mklement0 powershell
Users with the  powershell badge can single-handedly close powershell questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 3:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    Just get rid of the brackets when calling the function: $members = GetUserOnPage $accessToken [int]$I

    – Owain Esau
    Nov 14 '18 at 2:04












  • i tried this and i got the exact same result. also tried with and without the comma.

    – mcavanaugh418
    Nov 14 '18 at 2: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 2: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 2: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 2:57















1
















This question already has an answer here:



  • How do I pass multiple parameters into a function in PowerShell?

    14 answers



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












share|improve this question













marked as duplicate by mklement0 powershell
Users with the  powershell badge can single-handedly close powershell questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 3:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    Just get rid of the brackets when calling the function: $members = GetUserOnPage $accessToken [int]$I

    – Owain Esau
    Nov 14 '18 at 2:04












  • i tried this and i got the exact same result. also tried with and without the comma.

    – mcavanaugh418
    Nov 14 '18 at 2: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 2: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 2: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 2:57













1












1








1









This question already has an answer here:



  • How do I pass multiple parameters into a function in PowerShell?

    14 answers



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












share|improve this question















This question already has an answer here:



  • How do I pass multiple parameters into a function in PowerShell?

    14 answers



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







This question already has an answer here:



  • How do I pass multiple parameters into a function in PowerShell?

    14 answers







powershell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 1:47









mcavanaugh418mcavanaugh418

778




778




marked as duplicate by mklement0 powershell
Users with the  powershell badge can single-handedly close powershell questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 3:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by mklement0 powershell
Users with the  powershell badge can single-handedly close powershell questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 3:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1





    Just get rid of the brackets when calling the function: $members = GetUserOnPage $accessToken [int]$I

    – Owain Esau
    Nov 14 '18 at 2:04












  • i tried this and i got the exact same result. also tried with and without the comma.

    – mcavanaugh418
    Nov 14 '18 at 2: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 2: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 2: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 2:57












  • 1





    Just get rid of the brackets when calling the function: $members = GetUserOnPage $accessToken [int]$I

    – Owain Esau
    Nov 14 '18 at 2:04












  • i tried this and i got the exact same result. also tried with and without the comma.

    – mcavanaugh418
    Nov 14 '18 at 2: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 2: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 2: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 2:57







1




1





Just get rid of the brackets when calling the function: $members = GetUserOnPage $accessToken [int]$I

– Owain Esau
Nov 14 '18 at 2:04






Just get rid of the brackets when calling the function: $members = GetUserOnPage $accessToken [int]$I

– Owain Esau
Nov 14 '18 at 2:04














i tried this and i got the exact same result. also tried with and without the comma.

– mcavanaugh418
Nov 14 '18 at 2:09





i tried this and i got the exact same result. also tried with and without the comma.

– mcavanaugh418
Nov 14 '18 at 2:09




1




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 2:10





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 2: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 2:14





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 2: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 2:57





@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 2:57












1 Answer
1






active

oldest

votes


















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






share|improve this answer

























  • 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 2:10

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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






share|improve this answer

























  • 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 2:10















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






share|improve this answer

























  • 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 2:10













0












0








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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 2:14

























answered Nov 14 '18 at 2:01









DrewDrew

1,285416




1,285416












  • 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 2:10

















  • 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 2:10
















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 2:10





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 2:10



Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線