Powershell Foreach循环迭代API输出并逐行显示变量值

e1xvtsh3  于 2023-06-29  发布在  Shell
关注(0)|答案(2)|浏览(166)

我正在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",

如有任何关于如何实现这一目标的建议或想法,我们将不胜感激。

Clear-Host $connectionToken="[MY-PAT]”$base64AuthInfo= [系统.转换]::ToBase 64 String([系统.文本.编码]::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|先选择对象1
$BuildResult= $PackageInfo.value.result|先选择对象1
foreach($item in $PackageInfo){ Write-Host““Write-Host“Latest build = $LatestVersion”“|““生成结果= $BuildResult”写入主机““
}

从上图可以看出,通过检索最后一个构建记录,我得到了上面由我的PowerShell脚本返回的输出,这很棒。
然而,我不仅仅是在最后一个构建记录之后。我要所有的建造记录一行一行列出来。因此,任何对我现有脚本的改进都将是最合适的,因为在我看来,它只需要一个小的调整。
省略Select-Object将返回所有预期的数据,但以下面的格式显示,而不是以我喜欢的单行格式显示。

abithluo

abithluo1#

您正在获取构建信息,因此我假设您正在使用以下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手动生成令牌。

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
}
gg58donl

gg58donl2#

$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
}

相关问题