Created
January 10, 2020 15:49
-
-
Save pietergheysens/7d56a60dd1f6acfaa59f3688648b604e to your computer and use it in GitHub Desktop.
Fix Iteration Path of work items based on values of the as of parameter in a Azure DevOps WIQL query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$token = "" | |
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "azdo",$token))) | |
$uri = "https://dev.azure.com/{organisation}/{teamproject}/_apis/wit/wiql?api-version=5.1" | |
$body = @" | |
{ | |
"query": "Select [System.Id], [System.Title], [System.State] From WorkItems | |
Where [System.TeamProject] = {teamproject} AND [System.WorkItemType] <> 'Test Case' | |
asof '2020-01-08T10:00:00.000Z'"} | |
"@ | |
$result = Invoke-RestMethod -Uri $uri -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body | |
foreach ($result in $result.workItems) | |
{ | |
$uriWorkItem = "https://dev.azure.com/{organisation}/{teamproject}/_apis/wit/workitems/" + $result.id + "?asOf=2020-01-08T10:00:00.000Z&api-version=5.1" | |
$workitem = Invoke-RestMethod -Uri $uriWorkItem -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} | |
$oldValue = $workitem.fields.'System.IterationPath' | |
$newValue = ($oldValue -replace "{OldTeamProjectName}", "{NewTeamProjectName}") -replace "\\", "\\" | |
Write-Host $oldValue -ForegroundColor DarkRed | |
Write-Host $newValue -ForegroundColor DarkGreen | |
if ($newValue -ne "{NewTeamProjectName}") | |
{ | |
$uriWorkItemUpdate = "https://dev.azure.com/{organisation}/{teamproject}/_apis/wit/workitems/" + $result.id + "?api-version=5.1" | |
$update = @" | |
[ | |
{ | |
"op": "add", | |
"path": "/fields/System.IterationPath", | |
"value": "$newValue" }] | |
"@ | |
Invoke-RestMethod -Uri $uriWorkItemUpdate -Method Patch -ContentType "application/json-patch+json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $update | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment