Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Azure PowerShell中的循环问题?_Azure_Powershell_Azure Devops_Azure Pipelines_Pipeline - Fatal编程技术网

Azure PowerShell中的循环问题?

Azure PowerShell中的循环问题?,azure,powershell,azure-devops,azure-pipelines,pipeline,Azure,Powershell,Azure Devops,Azure Pipelines,Pipeline,我试图从各种Azure API端点提取一些数据,但没有得到我所需要的确切结果,因为我试图利用循环。我正确地提取了所有项目名称,但没有正确地点击“GetPipelines runs”来尝试提取AzDo中每个管道最后一次运行的id和各个时间以及id。$id和times都是数组,因为它捕获多个运行,因此使用“[0]”来获取索引中的第一个运行。我能做些什么来清理它,使它更有效…并真正起作用。在此状态下,它仅成功生成项目列表 $personalToken = "*****************

我试图从各种Azure API端点提取一些数据,但没有得到我所需要的确切结果,因为我试图利用循环。我正确地提取了所有项目名称,但没有正确地点击“GetPipelines runs”来尝试提取AzDo中每个管道最后一次运行的id和各个时间以及id。$id和times都是数组,因为它捕获多个运行,因此使用“[0]”来获取索引中的第一个运行。我能做些什么来清理它,使它更有效…并真正起作用。在此状态下,它仅成功生成项目列表

$personalToken = "****************************"
 
#Write-Host "Initialize authentication context" -ForegroundColor Yellow
$token = [System.Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$header = @{authorization = "Basic $token" }

# Get projects 
$projecturl = "https://dev.azure.com/*******/_apis/projects?api-version=6.0"
$projects = Invoke-RestMethod -Uri $projecturl -Method Get -ContentType "application/json" -Headers $header

# Get pipeline runs
$runurl = "https://dev.azure.com/*********/$projectname/_apis/pipelines/$id/runs?api-version=6.0-preview.1"
$runs = Invoke-RestMethod -Uri $runurl -Method Get -ContentType "application/json" -Headers $header

foreach($project in $projects.value) {
    Write-Output $project.name
    $projectname = $project.name

foreach($run in $runs.value.pipeline.id) {
    Write-Output $runs.value.pipeline.id
    $lastrundate = $runs.value.pipeline.id
    $id = $runs.value.pipeline.id[0]

# Builds API call
$url = "https://dev.azure.com/********/$projectname/_apis/pipelines?api-version=6.0-preview.1"
$output = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
$output.value | Sort-Object id -Descending | ForEach-Object {
        Write-Host $_.name - $_.folder - $_.id - $_._links.web

        $obj = New-Object System.Object
        $obj | Add-Member -type NoteProperty -name LastRunDate -Value $lastrundate
        $obj | Add-Member -type NoteProperty -name ProjectName -Value $projectname
        $obj | Add-Member -type NoteProperty -name PipelineName -Value $_.name
        $obj | Add-Member -type NoteProperty -name PipelineFolder -Value $_.folder 
        $obj | Add-Member -type NoteProperty -name PipelineURL -Value $_._links.web
        $obj | Add-Member -type NoteProperty -name PipelineID -Value $_.id
        $obj | Select-Object LastRunDate, ProjectName, PipelineName , PipelineFolder , PipelineURL , PipelineID | Export-Csv -Append c:\Temp\****.csv
       }
    }

这一部分很奇怪:

foreach($run in $runs.value.pipeline.id) {
    Write-Output $runs.value.pipeline.id
    $lastrundate = $runs.value.pipeline.id
    $id = $runs.value.pipeline.id[0]
检查此处的运行定义:

您可以使用类似的方法:

foreach($run in $runs.value) {
    Write-Output "Run Id" $run.id "Pipeline Id" $run.pipeline.id "Finidh Date" $run.finishedDate
    $lastrundate = $run.createdDate


#something here

}
测试结果:


谢谢你,休!这正是我所需要的!
$personalToken = "xx"
 
#Write-Host "Initialize authentication context" -ForegroundColor Yellow
$token = [System.Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$header = @{authorization = "Basic $token" }   

# Get projects 
$projecturl = "https://dev.azure.com/xxx/_apis/projects?api-version=6.0"
$projects = Invoke-RestMethod -Uri $projecturl -Method Get -ContentType "application/json" -Headers $header
       
foreach($project in $projects.value) {
    #Write-Output $project.name
    $projectname = $project.name

    # List pipeline ID via org name and project name
    $url = "https://dev.azure.com/xxx/$($projectname)/_apis/pipelines?api-version=6.0-preview.1"
    $pipelines = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header   

    foreach($pipeline in $pipelines.value){
        Write-Host $pipeline.id
        $pipelineId = $pipeline.id
        #List run details via pipeline id and get the latest run info
        $runrul = "https://dev.azure.com/xxx/$($projectname)/_apis/pipelines/$($pipelineId)/runs?api-version=6.0-preview.1"
        $runResult = Invoke-RestMethod -Uri $runrul -Method Get -ContentType "application/json" -Headers $header
        $output = $runResult.value[0]
        $runID = $output.id
        $lastrundate = $output.createdDate
        $pipelineName = $output.pipeline.name
        $pipelineFolder = $output.pipeline.folder
        $pipelineURL = $output.pipeline.url
        $pipelineId
        #Write-Host $runID $lastrundate $pipelineName $pipelineFolder $pipelineURL $pipelineId
        $output = New-Object -TypeName PSObject -Property @{
            runID = $output.id
            lastrundate = $output.createdDate
            pipelineName = $output.pipeline.name
            pipelineFolder = $output.pipeline.folder
            pipelineURL = $output.pipeline.url
            pipelineId = $pipelineId
          } | Select-Object runID, lastrundate,pipelineName,pipelineFolder,pipelineURL,pipelineId
        $output | Export-Csv E:\test\testx.csv -Append -Force
    }
}