如何在PowerShell中获取Azure DevOps新建工作项的ID用于后续更新
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:
Using Azure DevOps CLI (Recommended)
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-Credentialor Azure Key Vault) instead of hardcoding it in scripts.
内容的提问来源于stack exchange,提问作者Kate

