我正在 Postman 中运行一些 Azure DevOps API 查询,这些查询成功返回了预期结果,下面共享了其中的一个片段,它仅代表数十个类似迭代输出中的一个。为了说明这一点,让我们假设下面的 ID 值 66 的代码片段只是 100 之一。
"id": 66,
"buildNumber": "20210401.7",
"status": "completed",
"result": "succeeded",
"queueTime": "2021-04-01T16:50:04.9218848Z",
"startTime": "2021-04-01T16:50:15.3583291Z",
"finishTime": "2021-04-01T16:50:53.7221605Z",
我现在希望使用 Powershell foreach 循环迭代解析 100 多条记录,将“id”、“buildNumber”、“status”和“result”值逐行输出到输出文件中。
所需的输出应如下所示:
"id": 66,
"buildNumber": "20210401.7",
"status": "completed",
"result": "succeeded",
"id": 65,
"buildNumber": "20210331.5",
"status": "completed",
"result": "failed",
"id": 64,
"buildNumber": "20210331.4",
"status": "completed",
"result": "succeeded",
任何有关如何实现这一目标的建议或想法将不胜感激。
[[我当前的 POWERSHELL 脚本,其中的个人变量已被编辑]]
$connectionToken="[MY-PAT]"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$url = 'https://dev.azure.com/[MY-AZURE-ORG]/[MY-TEAM-PROJECT]/_apis/build/builds?api-version=6.0'
$PackageInfo = (Invoke-RestMethod -Uri $url -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$LatestVersion= $PackageInfo.value.buildnumber | Select-Object -first 1
$BuildResult= $PackageInfo.value.result | Select-Object -first 1
foreach ($item in $PackageInfo)
{
Write-Host " "
Write-Host "Latest build = $LatestVersion" "|" "Build Result = $BuildResult"
Write-Host " "
}
[[以上脚本的输出]]
从上图可以看出,通过检索最后的构建记录,我得到了 Powershell 脚本返回的上述输出,这非常棒。
然而,我不仅仅追求最后的构建记录。我想要逐行列出所有构建记录。因此,对我现有的脚本进行任何改进来实现这一点在理想情况下都是最合适的,因为我认为它只需要进行一些小的调整。
省略 Select-Object 将返回所有预期数据,但采用以下格式,该格式不会以我首选的单独行格式显示。
您正在获取构建信息,因此我假设您正在使用以下 Azure DevOps API:
https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=6.0
使用PowerShell,首先您需要进行身份验证, 您可以使用 OAuth2 身份验证: 请参阅:https://learn.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops
可以使用PAT访问方式,但需要从UI手动生成token。
function getBearer([string]$TenantID, [string]$ClientID, [string]$ClientSecret)
{
$TokenEndpoint = {https://login.windows.net/{0}/oauth2/token} -f $TenantID
$ARMResource = "https://management.core.windows.net/";
$Body = @{
'resource'= $ARMResource
'client_id' = $ClientID
'grant_type' = 'client_credentials'
'client_secret' = $ClientSecret
}
$params = @{
ContentType = 'application/x-www-form-urlencoded'
Headers = @{'accept'='application/json'}
Body = $Body
Method = 'Post'
URI = $TokenEndpoint
}
$token = Invoke-RestMethod @params
Return "Bearer " + ($token.access_token).ToString()
}
$header = @{
Authorization = getBearer $TenantId $ClientId $ClientSecret
content-type = 'application/json'
}
$ApiUri = "https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=6.0"
$data = (Invoke-RestMethod -Uri $ApiUri -Method Get -Headers $header).value
$OutputFile = "<file-name>"
Class BuildObject {
id [String]
buildNumber [String]
status [String]
result [String]
}
$BuildObj = New-Object $BuildObject
foreach($buildItem in $data) {
$BuildObj.id = $buildItem.id
$BuildObj.buildNumber = $buildItem.buildNumber
$BuildObj.status = $buildItem.status
$BuildObj.result = $buildItem.result
$BuildObj | OutFile -FilePath $OutputFile -Append
}
$datepoint = "MAIN_20230601"
$accountingTypesArray = "AccruedExternalInterest","AccruedInternalInterest","AccruedFee"
foreach ($item in $accountingTypesArray)
{
$finalUrl = "https://encx-eodd-test.net/api/Delivery/Trigger?accountingPoint="+$datepoint+"&productSystem=WSO&accountingType="+$item
$response = Invoke-RestMethod $finalUrl -Method 'GET'
Write-Output $response.slideshow
}