最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Want to do a mass update an Azure DevOps work items with lookup and replace with PowerShell? - Stack Overflow

programmeradmin2浏览0评论

I have created a non-standard customized work item in ADO called "Resource". I want to a method (assumed PowerShell script with ADO REST API) to do the following:

  1. Pull or loop through all work items of this "Resource" type within a Project.
  2. For each work item, Get the value on field "Value A" on that work item, cross reference to a table (hard coded or external) with both "Value A" and "Value B".
  3. Lookup "Value A" then get "Value B" field on the table and update "Value B" on the "Resource" work item.

Basically, it is a lookup request. This table of information may change monthly and would need to rerun.

My assumption is that Powershell is likely the best option, but not sure. Not sure if I have to hardcode the table or if I should call to an external table/database. This table would range from 100 to 500 rows with two columns..Value A and Value B.

Old programmer, but new to Powershell, seen examples of replace a single work item but nothing for a mass-changes, loop, or lookup/replace.

What I am asking seems possible but not sure if I am suggesting something that is simple through other methods. Powershell it seems possible, Power Automate another, or there might be something built into ADO I am not thinking of.

Again, hoping for insight of my problem if a Powershell is the best method, if it is possible, and if so...an example of the code.

I have created a non-standard customized work item in ADO called "Resource". I want to a method (assumed PowerShell script with ADO REST API) to do the following:

  1. Pull or loop through all work items of this "Resource" type within a Project.
  2. For each work item, Get the value on field "Value A" on that work item, cross reference to a table (hard coded or external) with both "Value A" and "Value B".
  3. Lookup "Value A" then get "Value B" field on the table and update "Value B" on the "Resource" work item.

Basically, it is a lookup request. This table of information may change monthly and would need to rerun.

My assumption is that Powershell is likely the best option, but not sure. Not sure if I have to hardcode the table or if I should call to an external table/database. This table would range from 100 to 500 rows with two columns..Value A and Value B.

Old programmer, but new to Powershell, seen examples of replace a single work item but nothing for a mass-changes, loop, or lookup/replace.

What I am asking seems possible but not sure if I am suggesting something that is simple through other methods. Powershell it seems possible, Power Automate another, or there might be something built into ADO I am not thinking of.

Again, hoping for insight of my problem if a Powershell is the best method, if it is possible, and if so...an example of the code.

Share Improve this question asked Jan 30 at 17:57 Ben NelsonBen Nelson 1 1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Bot Commented Jan 30 at 21:24
Add a comment  | 

1 Answer 1

Reset to default 0

Here is the demo how to get the field and update it.

$token = "Personal Access Token"

$url="https://dev.azure/{}/{project}/_apis/wit/wiql?api-version=7.1"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$JSON = @'
{
   "query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType]  = 'Epic'"
}
'@

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json


ForEach( $workitem in $response.workItems ) 
{

$workitemid= $workitem.id

$url1="https://dev.azure/{}/{project}/_apis/wit/workitems/$($workitemid)?api-version=7.1"

$response1 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Get 

echo $response1.fields.'System.Title'


$body = @(
    @{
        "op" = "add"
        "path" = "/fields/System.Title"
        "value" = "Updated Title"
    },
    @{
        "op" = "add"
        "path" = "/fields/System.Description"
        "value" = "Updated Description"
    }
) | ConvertTo-Json -Depth 10

$response = Invoke-RestMethod -Uri $url1 -Method Patch -Headers @{
    Authorization = "Basic $token"
    "Content-Type" = "application/json-patch+json"
} -Body $body

$response



}

In the ForEach( $workitem in $response.workItems ) , you could get the Value B from hard coded or external. And update it in

$body = @(
        @{
            "op" = "add"
            "path" = "/fields/System.Title"
            "value" = "Updated Title"
        },
        @{
            "op" = "add"
            "path" = "/fields/System.Description"
            "value" = "Updated Description"
        }
    ) | ConvertTo-Json -Depth 10
发布评论

评论列表(0)

  1. 暂无评论