powershellazure-devopstfstfs-workitem

Azure Devops Get package Workitem state


I'm creating a package work Item using Powershell script in Azure Devops workitem and updating the same on successful completion of deployment as Completed.

Question: How do i Query the package state? I tried using the below powershell, which queries the exisitng package but unable to find the logic to get system.state of the package

My Powershell:

function TestState{
Param(
    [Parameter(Mandatory)]
    [string[]]
    $package,
    [Parameter(Mandatory)]
    [string[]]
    $secret,
    [Parameter(Mandatory)]
    [string[]]
    $DevopsProject

)
$header = prepareHeader $secret
$query = "SELECT [System.Id] FROM WorkItems WHERE [System.TeamProject] = '$DevopsProject' AND [System.WorkItemType] = 'Package' AND [System.Title] = '$PackageName' "
$json = @{ query = $query }
$jsonBody = ConvertTo-Json $json
$url  = "http://10.0.0.5/Test/$DevopsProject/_apis/wit/wiql?api-version=6.0"
$output = Invoke-RestMethod -Uri $url -Method Post -ContentType "application/json" -Headers $header -Body $jsonbody
$testurl = $output.workitems.url
$testurl = $testurl.Split('')
$test_url = $testurl[0]+'?api-version=6.0'
$output1 = Invoke-webrequest -uri "$test_url" -useBaseParsing -Method Get -ContentType "application/json" -Headers $header | ConvertFrom-json
return $output1
}    
                                                                                   

My Pipeline:

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
        $test = checkPackagestate "$PackageName" "$(secret)" $(DevopsProject)" 

Solution

  • You may update your script to this version

    function TestState{
    Param(
        [Parameter(Mandatory)]
        [string[]]
        $package,
        [Parameter(Mandatory)]
        [string[]]
        $secret,
        [Parameter(Mandatory)]
        [string[]]
        $DevopsProject
    
    )
    $header = prepareHeader $secret
    $query = "SELECT [System.Id] FROM WorkItems WHERE [System.TeamProject] = '$DevopsProject' AND [System.WorkItemType] = 'Package' AND [System.Title] = '$PackageName' "
    $json = @{ query = $query }
    $jsonBody = ConvertTo-Json $json
    $url  = "http://10.0.0.5/Test/$DevopsProject/_apis/wit/wiql?api-version=6.0"
    $output = Invoke-RestMethod -Uri $url -Method Post -ContentType "application/json" -Headers $header -Body $jsonbody
    
    $outputres = ""
    
    foreach ($wi in $output.workitems)
    {
      $test_url = $wi.url +'?api-version=6.0'
      $output1 = Invoke-webrequest -uri "$test_url" -Method Get -ContentType "application/json" -Headers $header | ConvertFrom-json
      $outputres += "$($output1.id) : $($output1.fields.'System.Title') : $($output1.fields.'System.State')`n"
    }    
    
    return $outputres
    }