我正在尝试让所有用户都参与几个项目。情况是,在用户众多的情况下,我需要使用 continuationToken 。结果是混乱的,因为我的逻辑不好。我不知道如何使用第二个foreach。
$outputItems = @()
foreach ($project in $projects) {
$uriProjectDescriptor = "https://vssps.dev.azure.com/$OrganizationName/_apis/graph/descriptors/$($project.id)?api-version=5.0-preview.1"
$projectDescriptor = Invoke-RestMethod -Uri $uriProjectDescriptor -Method Get -Headers $AzureDevOpsAuthenicationHeader
$descriptors = $projectDescriptor.value
foreach ($descriptor in $descriptors) {
do
{
$uriUsers="https://vssps.dev.azure.com/$OrganizationName/_apis/graph/users?continuationToken=$continuationToken&scopeDescriptor=$($descriptor)&api-version=6.0-preview.1"
$responseUsers=Invoke-WebRequest -Uri $uriUser -Method Get -ContentType "application/json" -Headers $AzureDevOpsAuthenicationHeader -UseBasicParsing -MaximumRedirection 0
$continuationToken = $responseUsers.Headers.'x-ms-continuationtoken'
$userSet = $responseUsers.content | ConvertFrom-Json
$users += $userSet.value.Count
$arealList = New-Object -TypeName PSObject -Property @{
CountUsers = $users.Count
} | Select-Object "CountUsers"
$outputItems += $arealList
$arealList = $null
}
while (($continuationToken))
$ProgressPreference = 'Continue'
}
} $outputItems
答案 0 :(得分:0)
这是未经测试的,因为我不在工作机器上,因此如果我不在这里或弄乱了某些东西,则表示歉意,但是我认为这里的功能与您重复执行的操作会使您尝试执行的操作更清晰。
# Output array
$OutputItems = @()
# Repeatable function for obtaining the users for the descriptor - Define this before the main part of your code.
function Get-ProjectUsers {
Param (
[Parameter(Mandatory=$true)][string]$Descriptor,
[Parameter(Mandatory=$false)][string]$ContinuationToken
)
# If the continuation token has been passed, we need to add it to the API URI.
if ($PSBoundParameters.ContainsKey("ContinuationToken")) {
$UriUsers = "https://vssps.dev.azure.com/$script:OrganizationName/_apis/graph/users?continuationToken=$ContinuationToken&scopeDescriptor=$Descriptor&api-version=6.0-preview.1"
}
else {
$UriUsers = "https://vssps.dev.azure.com/$script:OrganizationName/_apis/graph/users?scopeDescriptor=$Descriptor&api-version=6.0-preview.1"
}
# Invoke the web request and add the custom object to the output array.
$Response = Invoke-WebRequest -Uri $UriUsers -Method Get -ContentType "application/json" -Headers $script:AzureDevOpsAuthenicationHeader -UseBasicParsing -MaximumRedirection 0
$UserSet = $Response.Content | ConvertFrom-Json
$script:OutputItems += [PSCustomObject]@{
CountUsers = $UserSet.Value.Count
}
# If the continuation token exists return it so that we can carry on calling the API until there are no further records.
if (![string]::IsNullOrEmpty($Response.Headers.'x-ms-continuationtoken')) {
return $Response.Headers.'x-ms-continuationtoken'
}
else {
return $null
}
}
# Main code.
foreach ($Project in $Projects) {
$UriProjectDescriptor = "https://vssps.dev.azure.com/$OrganizationName/_apis/graph/descriptors/$($Project.id)?api-version=5.0-preview.1"
$ProjectDescriptor = Invoke-RestMethod -Uri $UriProjectDescriptor -Method Get -Headers $AzureDevOpsAuthenicationHeader
$Descriptors = $ProjectDescriptor.value
# We do not need to run the do loop until there is a continuation token.
foreach ($Descriptor in $Descriptors) {
$Repeat = Get-ProjectUsers -Descriptor $Descriptor
# If Repeat is not null, we can use the do loop to call our function until it does return null.
if ($null -ne $Repeat) {
do {
$Repeat = Get-ProjectUsers -Descriptor $Descriptor -ContinuationToken $Repeat
} until ($null -eq $Repeat)
}
}
}
答案 1 :(得分:0)
您可以尝试以下Powershell脚本:
$token = "PAT"
$url="https://dev.azure.com/{Organizationname}/_apis/projects?api-version=6.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$responses = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json
ForEach ($response in $responses.value.id)
{
$url1="https://vssps.dev.azure.com/{Organizationname}/_apis/graph/descriptors/$($response)?api-version=6.0-preview.1"
$Descriptor = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json
ForEach ($Descriptor1 in $Descriptor.value)
{
do
{
$url2="https://vssps.dev.azure.com/{Organizationname}/_apis/graph/users?scopeDescriptor=$($Descriptor1)&api-version=6.0-preview.1"
$UserLists = Invoke-RestMethod -Uri $url2 -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json
$continuationToken = $UserLists.Headers.'x-ms-continuationtoken'
}
while ($continuationToken -ne $null)
Write-Host "result = $($UserLists | ConvertTo-Json -Depth 100)"
}
}
说明:
first Rest API用于获取项目名称。
然后使用second Rest API获取描述符。
third Rest api用于获取用户列表。
我使用foreach嵌套来实现对Rest API的响应循环。
更新:
使用continuationToken进行测试,项目返回的对象的最大值为500。
我找到了另一个Rest Api User Entitlements - List来吸引用户。
https://vsaex.dev.azure.com/Organization Name/_apis/userentitlements?top=10000&select=project&api-version=4.1-preview.1
此Rest APi可以直接返回组织(项目范围)下的用户。最大值是10000。