powershellazure-devopstfs-workitem

How to create a Service Change using Rest API in PowerShell


I have a requirement to create a Service Change in Azure DevOps (TFS WorkItems) using Rest API in PowerShell.

My current code is working fine for Task Creation. But I have to create a Service Change. I am not sure about the Resource Area IDs. Which URI should I use?

$relDefUrl = $DOConfig.tfsBaseUrl + $DOConfig.project + "/_apis/wit/workitems/`$task?api-version=5.1"

function Init-Devops
{
  [CmdletBinding()]
  param(
      $Pat
    )
    PROCESS
    {
        $orgUrl = "https://dev.azure.com/XXXXXX"
        $project = "XXXX"
        $Team = "XXXX"
        $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($Pat)"))
        $header = @{authorization = "Basic $token"}
        $WorkItemTrackingAreaId = "85f8c7b6-92fe-4ba6-8b6d-fbb67c809341"
        $tfsBaseUrl = GetUrl -orgUrl $orgUrl -header $header -AreaId $WorkItemTrackingAreaId
        $DOConfig=@{
            "orgUrl"=$orgUrl;
            "project"=$project;
            "token"=$token;
            "header"=$header;
            "WorkItemTrackingAreaId"=$TFSQuery;
            "TFSQuery"=$TFSQuery;
            "tfsBaseUrl"=$tfsBaseUrl
            "Team"=$Team
        }
    return $DOConfig
    }
}

function GetUrl() {
    param(
        [string]$orgUrl, 
        [hashtable]$header, 
        [string]$AreaId
    )
    $orgResourceAreasUrl = [string]::Format("{0}/_apis/resourceAreas/{1}?api-preview=5.0-preview.1", $orgUrl, $AreaId)
    $results = Invoke-RestMethod -Uri $orgResourceAreasUrl -Headers $header
    if ("null" -eq $results) {
        $areaUrl = $orgUrl
    }
    else {
        $areaUrl = $results.locationUrl
    }
    return $areaUrl
}

function Add-DevopsTask
{
  [CmdletBinding()]
  param(
    $Body,
    $DOConfig,
    $Live
    )
    PROCESS
    {
        $relDefUrl = $DOConfig.tfsBaseUrl + $DOConfig.project + "/_apis/wit/workitems/`$task?api-version=5.1"
        $RestParams = @{
            Uri         = $relDefUrl   
            ContentType = 'application/json-patch+json'
            Headers     = $DOConfig.header
            Method      = "POST"
            Body        = $Body
        }
        Invoke-RestMethod @RestParams -Verbose          
    }
}

$pat = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$DOConfig=Init-Devops $pat

$xbody=@()
$vState="Proposed"
$xState = @{
            op    = "add"
            path  = "/fields/System.State"
            value = $vState
           }
$xbody += $xState

$vAssignedto = 'venkatag@microsoft.com'
$xAssignedto = @{
                op    = "add"
                path  = "/fields/System.AssignedTo"
                value = $vAssignedto
                }
$xbody += $xAssignedto

$vTitle = "Compliance Project Test Task"
$xTitle = @{
            op    = "add"
            path  = "/fields/System.Title"
            value = $vTitle
            }
$xbody += $xTitle

$vTags = "CPXCompliance"
$xTags = @{
            op    = "add"
            path  = "/fields/System.Tags"
            value = $vTags
          }
$xbody += $xTags

$vAreaPath = "OSGS\CRE\Store Core\CSTSRE"
$xAreaPath = @{
                op    = "add"
                path  = "/fields/System.AreaPath"
                value = $vAreaPath
            }
$xbody += $xAreaPath
$body=$xbody | ConvertTo-Json

Add-DevopsTask $body $DOConfig


Solution

  • Actually I poked around on available fields and I was able create Service Change using PowerShell. I rest my case. thanks for the help.

    We can use below WorkItemTrackingAreaId to create Service Change.

    $WorkItemTrackingAreaId = "85f8c7b6-92fe-4ba6-8b6d-fbb67c809341"