You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PowerShell中获取Azure DevOps新建工作项的ID用于后续更新

Capturing Azure DevOps Work Item ID for Subsequent Updates in PowerShell

Great question! When you create a work item via PowerShell in Azure DevOps, capturing the generated ID is straightforward—here are the most reliable ways to do it, depending on your tooling choice:

The Azure DevOps CLI (az boards work-item create) returns structured JSON output by default (if you specify --output json), making it easy to extract the ID directly as a property.

Example Workflow:

# 1. Create the work item and capture the JSON output
$newWorkItem = az boards work-item create `
    --title "Fix login button alignment" `
    --type Bug `
    --project "MyProductProject" `
    --output json | ConvertFrom-Json

# 2. Extract the work item ID from the captured object
$workItemId = $newWorkItem.id
Write-Host "Created work item with ID: $workItemId"

# 3. Use the ID in a subsequent update command
az boards work-item update `
    --id $workItemId `
    --fields "System.Description=Button misaligns on mobile view; repro steps attached"

If you don't use --output json, the CLI will print a human-readable message like Work item created with ID: 12345. You can still capture this with regex, though JSON parsing is more reliable:

$cliOutput = az boards work-item create --title "Test Task" --type Task --project "MyProject"
$workItemId = [regex]::Match($cliOutput, 'ID: (\d+)').Groups[1].Value

Using Azure DevOps REST API

If you're using Invoke-RestMethod to call the REST API directly, the response object includes an id property you can access immediately.

Example Workflow:

# Set up your Azure DevOps details
$orgUrl = "https://dev.azure.com/YourOrganization"
$projectName = "YourProject"
$patToken = "YourPersonalAccessToken" # Store this securely in production!
$base64Auth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$patToken"))

# 1. Define the work item payload (JSON Patch format)
$createPayload = @(
    @{
        op = "add"
        path = "/fields/System.Title"
        value = "Implement new user dashboard"
    },
    @{
        op = "add"
        path = "/fields/System.AssignedTo"
        value = "John Doe <john.doe@company.com>"
    }
) | ConvertTo-Json

# 2. Create the work item and capture the response
$createResponse = Invoke-RestMethod `
    -Uri "$orgUrl/$projectName/_apis/wit/workitems/`$User Story?api-version=7.1-preview.3" `
    -Method Post `
    -Headers @{ Authorization = "Basic $base64Auth" } `
    -Body $createPayload `
    -ContentType "application/json-patch+json"

# 3. Extract the ID
$workItemId = $createResponse.id
Write-Host "Created user story with ID: $workItemId"

# 4. Use the ID to update the work item later
$updatePayload = @(
    @{
        op = "replace"
        path = "/fields/System.State"
        value = "In Progress"
    }
) | ConvertTo-Json

Invoke-RestMethod `
    -Uri "$orgUrl/$projectName/_apis/wit/workitems/$workItemId?api-version=7.1-preview.3" `
    -Method Patch `
    -Headers @{ Authorization = "Basic $base64Auth" } `
    -Body $updatePayload `
    -ContentType "application/json-patch+json"

Key Notes:

  • Always prefer capturing structured output (JSON) over regex parsing—it's less prone to breaking if Azure DevOps changes its CLI output format.
  • Store your PAT token securely (e.g., using Get-Credential or Azure Key Vault) instead of hardcoding it in scripts.

内容的提问来源于stack exchange,提问作者Kate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 08:57:54