WorkItem/Tagging/WorkItem_Tagging.ps1

<#
.SYNOPSIS
    Gets one or more work item tags
.DESCRIPTION
     
.EXAMPLE
 
.INPUTS
    Microsoft.TeamFoundation.WorkItemTracking.Client.Project
    System.String
.OUTPUTS
    Microsoft.TeamFoundation.Core.WebApi.WebApiTagDefinition
.NOTES
#>

Function Get-TfsWorkItemTag
{

    [CmdletBinding()]
    [OutputType('Microsoft.TeamFoundation.Core.WebApi.WebApiTagDefinition')]
    Param
    (
        [Parameter(Position=0)]
        [SupportsWildcards()]
        [Alias('Name')]
        [object] 
        $Tag = '*',

        [Parameter()]
        [switch]
        $IncludeInactive,

        [Parameter(ValueFromPipeline=$true)]
        [object]
        $Project,

        [Parameter()]
        [object]
        $Collection
    )

    Begin
    {
        #_ImportRequiredAssembly -AssemblyName 'Microsoft.TeamFoundation.Core.WebApi'
    }

    Process
    {
        if ($Tag -is [Microsoft.TeamFoundation.Core.WebApi.WebApiTagDefinition]) { _Log "Input item is of type Microsoft.TeamFoundation.Core.WebApi.WebApiTagDefinition; returning input item immediately, without further processing."; return $Tag }

        $tp = Get-TfsTeamProject -Project $Project -Collection $Collection; if (-not $tp -or ($tp.Count -ne 1)) {throw "Invalid or non-existent team project $Project."}; $tpc = $tp.Store.TeamProjectCollection

        $client = _GetRestClient 'Microsoft.TeamFoundation.Core.WebApi.TaggingHttpClient' -Collection $tpc

        $task = $client.GetTagsAsync($tp.Guid, $IncludeInactive.IsPresent); $result = $task.Result; if($task.IsFaulted) { _throw "Error retrieving work item tag '$Tag'" $task.Exception.InnerExceptions }

        return $result | Where-Object Name -like $Tag | Add-Member -Name TeamProject -MemberType NoteProperty -Value $TP.Name -PassThru
    }
}
<#
.SYNOPSIS
    Gets one or more work item tags
.DESCRIPTION
     
.EXAMPLE
 
.INPUTS
    Microsoft.TeamFoundation.WorkItemTracking.Client.Project
    System.String
.OUTPUTS
    Microsoft.TeamFoundation.Core.WebApi.WebApiTagDefinition
.NOTES
#>

Function New-TfsWorkItemTag
{

    [CmdletBinding(ConfirmImpact='Medium', SupportsShouldProcess=$true)]
    [OutputType('Microsoft.TeamFoundation.Core.WebApi.WebApiTagDefinition')]
    Param
    (
        [Parameter(Position=0,ValueFromPipeline=$true)]
        [Alias('Name')]
        [string] 
        $Tag,

        [Parameter()]
        [object]
        $Project,

        [Parameter()]
        [object]
        $Collection,

        [Parameter()]
        [switch]
        $Passthru
    )

    Begin
    {
        #_ImportRequiredAssembly -AssemblyName 'Microsoft.TeamFoundation.Core.WebApi'
    }

    Process
    {
        $tp = Get-TfsTeamProject -Project $Project -Collection $Collection; if (-not $tp -or ($tp.Count -ne 1)) {throw "Invalid or non-existent team project $Project."}; $tpc = $tp.Store.TeamProjectCollection

        if(-not $PSCmdlet.ShouldProcess($tp.Name, "Create work item tag '$Tag'"))
        {
            return
        }

        $client = _GetRestClient 'Microsoft.TeamFoundation.Core.WebApi.TaggingHttpClient' -Collection $tpc

        $task = $client.CreateTagAsync($tp.Guid, $Tag); $result = $task.Result; if($task.IsFaulted) { _throw "Error creating work item tag '$Tag'" $task.Exception.InnerExceptions }

        if($Passthru)
        {
            return $result
        }
    }
}
<#
.SYNOPSIS
    Deletes one or more work item tags
.DESCRIPTION
     
.EXAMPLE
 
.INPUTS
    Microsoft.TeamFoundation.Core.WebApi.WebApiTagDefinition
    System.String
.NOTES
#>

Function Remove-TfsWorkItemTag
{

    [CmdletBinding(ConfirmImpact='High', SupportsShouldProcess=$true)]
    Param
    (
        [Parameter(Position=0,ValueFromPipeline=$true)]
        [SupportsWildcards()]
        [Alias('Name')]
        [object] 
        $Tag = '*',

        [Parameter()]
        [switch]
        $IncludeInactive,

        [Parameter()]
        [object]
        $Project,

        [Parameter()]
        [object]
        $Collection
    )

    Begin
    {
        #_ImportRequiredAssembly -AssemblyName 'Microsoft.TeamFoundation.Core.WebApi'
    }

    Process
    {
        $tags = Get-TfsWorkItemTag -Tag $Tag -Project $Project -Collection $Collection

        foreach($t in $tags)
        {
            if($t.TeamProject) {$Project = $t.TeamProject}; $tp = Get-TfsTeamProject -Project $Project -Collection $Collection; if (-not $tp -or ($tp.Count -ne 1)) {throw "Invalid or non-existent team project $Project."}; $tpc = $tp.Store.TeamProjectCollection

            if(-not $PSCmdlet.ShouldProcess($tp.Name, "Delete work item tag [$($t.Name)]"))
            {
                continue
            }

            $client = _GetRestClient 'Microsoft.TeamFoundation.Core.WebApi.TaggingHttpClient' -Collection $tpc

            $task = $client.DeleteTagAsync($tp.Guid, $t.Id); $result = $task.Result; if($task.IsFaulted) { _throw "Error deleting work item tag [$($t.Name)]'" $task.Exception.InnerExceptions }
        }
    }
}
<#
.SYNOPSIS
    Deletes one or more work item tags
.DESCRIPTION
     
.EXAMPLE
 
.INPUTS
    Microsoft.TeamFoundation.Core.WebApi.WebApiTagDefinition
    System.String
.NOTES
#>

Function Rename-TfsWorkItemTag
{

    [CmdletBinding(ConfirmImpact='Medium', SupportsShouldProcess=$true)]
    Param
    (
        [Parameter(Position=0,ValueFromPipeline=$true)]
        [Alias('Name')]
        [object] 
        $Tag,

        [Parameter(Position=1)]
        [string]
        $NewName,

        [Parameter()]
        [object]
        $Project,

        [Parameter()]
        [object]
        $Collection,

        [Parameter()]
        [switch]
        $Passthru
    )

    Begin
    {
        #_ImportRequiredAssembly -AssemblyName 'Microsoft.TeamFoundation.Core.WebApi'
    }

    Process
    {
        $tags = Get-TfsWorkItemTag -Tag $Tag -Project $Project -Collection $Collection

        foreach($t in $tags)
        {
            if(-not $PSCmdlet.ShouldProcess($t.Name, "Rename work item tag to [$NewName]"))
            {
                continue
            }

            if($t.TeamProject) {$Project = $t.TeamProject}; $tp = Get-TfsTeamProject -Project $Project -Collection $Collection; if (-not $tp -or ($tp.Count -ne 1)) {throw "Invalid or non-existent team project $Project."}; $tpc = $tp.Store.TeamProjectCollection

            $client = _GetRestClient 'Microsoft.TeamFoundation.Core.WebApi.TaggingHttpClient' -Collection $tpc

            $task = $client.UpdateTagAsync($tp.Guid, $t.Id, $NewName, $t.Active); $result = $task.Result; if($task.IsFaulted) { _throw "Error renaming work item tag [$($t.Name)]'" $task.Exception.InnerExceptions }

            if($Passthru.IsPresent)
            {
                return $result
            }
        }
    }
}