我正在尝试使用powershell脚本调用API,以返回所有用户的分页转储,那里有值。我得到一个页面计数,该计数填充了我需要调用的页面数。我将该值放入for循环中,并且int随循环的每次运行而增加。在循环的中间,当我将$ I传递给函数时,该函数将获得0而不是要传递的数字。
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
下面是我与写主机一起检查我的值的回报
加数1
0已加载页面
https://mycompany.myapplication.com/api/member?page=0
检查1
数2
0已加载页面
https://mycompany.myapplication.com/api/member?page=0
检查2
数3
0已加载页面
https://mycompany.myapplication.com/api/member?page=0
检查3
数4
0已加载页面
https://mycompany.myapplication.com/api/member?page=0
检查4
数5
0已加载页面
https://mycompany.myapplication.com/api/member?page=0
检查5
数6
0已加载页面
https://mycompany.myapplication.com/api/member?page=0
检查6
答案 0 :(得分:0)
更改以下几行,它也应该清除。
GetUserOnPage($accessToken, [int]$I)
应该是
GetUserOnPage $accessToken $I
由于评论更改了答案