Getting unexpected Int value when passing value to powershell function as parameter [duplicate]
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
powershell
marked as duplicate by mklement0
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.
|
show 1 more comment
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
powershell
marked as duplicate by mklement0
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. thisGetUserOnPage -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
|
show 1 more comment
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
powershell
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
powershell
asked Nov 14 '18 at 1:47
mcavanaugh418mcavanaugh418
778
778
marked as duplicate by mklement0
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
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. thisGetUserOnPage -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
|
show 1 more comment
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. thisGetUserOnPage -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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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