-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtfs.work.item.lib.psm1
51 lines (45 loc) · 2.54 KB
/
tfs.work.item.lib.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.WorkItemTracking.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")
Add-PSSnapin Microsoft.TeamFoundation.PowerShell
Function Get-WorkitemFieldValue([string]$TFSURL, [string]$WorkitemId, [string]$FieldName)
{
$teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($TFSURL)
$wit = $teamProjectCollection.GetService([Type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")
$items = $wit.GetWorkItem($WorkitemId)
$value = $items.Fields[$FieldName].Value
return $value
}
Function Update-WorkitemField([string]$TFSURL, [string]$WorkitemId, [string]$FieldName, $FieldValue)
{
$teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($TFSURL)
$wit = $teamProjectCollection.GetService([Type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")
$items = $wit.GetWorkItem($WorkitemId)
Write-Host "Before change, $FieldName is $($items.Fields[$FieldName].Value)"
$items.Fields[$FieldName].Value = $FieldValue
$wit.BatchSave($items)
}
Function Append-WorkitemField([string]$TFSURL, [string]$WorkitemId, [string]$FieldName, [string]$TextString)
{
$val = Get-WorkitemFieldValue $TFSURL $WorkitemId $FieldName
$val += $TextString
Update-WorkitemField $TFSURL $WorkitemId $FieldName $val
}
Function QueryAndBulkEdit([string]$TFSURL, [string]$FieldNameForPath, [string]$WorkItemPath, [string]$FieldName, [string]$SearchString, [string]$FieldNameForUpdate, [string]$UpdateValue)
{
$teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($TFSURL)
$wit = $teamProjectCollection.GetService([Type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")
$WIQL = @"
SELECT [System.Id], [System.WorkItemType], [System.State], [System.AssignedTo], [System.Title]
FROM WorkItems
WHERE [System.$FieldNameForPath] = '$WorkItemPath' AND [System.$FieldName] Contains '$SearchString'
ORDER BY [System.WorkItemType], [System.Id]
"@
Write-Host $WIQL
$collection = $wit.Query($WIQL)
foreach($item in $collection)
{
Write-Host "Update $($item.Id) $FieldNameForUpdate Field with $UpdateValue"
Update-WorkitemField $TFSURL $item.Id $FieldNameForUpdate $UpdateValue
}
}