如何在 PowerShell 中使用 Rest API 创建服务变更

How to create a Service Change using Rest API in PowerShell

我需要使用 PowerShell 中的 Rest API 在 Azure DevOps (TFS WorkItems) 中创建 Service Change

我当前的代码可以很好地创建任务。但我必须创建服务变更。我不确定 Resource Area IDs。我应该使用哪个 URI?

$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

实际上,我仔细查看了可用字段,并且能够使用 PowerShell 创建服务变更。我休息一下。感谢您的帮助。

我们可以使用下面的WorkItemTrackingAreaId来创建服务变更。

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