Team/Team.psm1

Function _GetTeam
{
    param
    (
        [Parameter()]
        [string] 
        $ProjectName,
    
        [Parameter()]
        [string] 
        $Name
    )

    Begin
    {
        $tpc = Get-TfsTeamProjectCollection -Current
        $teamService = $tpc.GetService([type]"Microsoft.TeamFoundation.Client.TfsTeamService")
    }

    Process
    {
        $teamProject = Get-TfsTeamProject $projectName
        $team = ($teamService.QueryTeams($teamProject.Uri) | where Name -eq $name)

        return $team
    }
}

Function _CreateTeam
{
    param
    (
    )

    Process
    {
        $teamService = $tpc.GetService([type]"Microsoft.TeamFoundation.Client.TfsTeamService")
        return $teamService.CreateTeam($teamProject.Uri, $Name, $Description, $null)
    }
}

Function _SetTeamSettings
{
    param
    (
        $team, 
        
        [string[]]
        $teamFieldValues        
    )
}

function _GetTfsTeamSettingsConfigurationService {
    Param(
        [Microsoft.TeamFoundation.Client.TfsTeamProjectCollection] $TfsCollection
        )
    return $TfsCollection.GetService([ Microsoft.TeamFoundation.ProcessConfiguration.Client.TeamSettingsConfigurationService]);
} 
 
function _AddTfsTeamField {
Param(
       [parameter(Mandatory=$true)][Microsoft.TeamFoundation.Client.TfsTeamProjectCollection] $TfsCollection,
       [parameter(Mandatory=$true)][Microsoft.TeamFoundation.Server.ProjectInfo] $TfsTeamProject,
       [parameter(Mandatory=$true)][Microsoft.TeamFoundation.Client.TeamFoundationTeam] $TfsTeam,
       [parameter(Mandatory=$true)][String] $TeamFieldValue
       )
 
    $TfsTeamConfigService = Get-TfsTeamSettingsConfigurationService $TfsCollection
    $TfsTeamConfig = $TfsTeamConfigService.GetTeamConfigurations([Guid[]]($TfsTeam.Identity.TeamFoundationId))
 
    $newTeamFieldValue = New-Object Microsoft.TeamFoundation.ProcessConfiguration.Client.TeamFieldValue
    $newTeamFieldValue.Value = $TeamFieldValue
 
    $TfsTeamConfig.TeamSettings.TeamFieldValues = [Microsoft.TeamFoundation.ProcessConfiguration.Client.TeamFieldValue[]]($newTeamFieldValue)
    $TfsTeamConfig.TeamSettings.BacklogIterationPath = "$($TfsTeamProject.Name)" 
    #$TfsTeamConfig.TeamSettings.IterationPaths = [string[]]("$($TfsTeamProject.Name)")
 
    $TfsTeamConfigService.SetTeamSettings($TfsTeam.Identity.TeamFoundationId,$TfsTeamConfig.TeamSettings)
} 
<#
 
.SYNOPSIS
    Gets information about one or more teams.
 
.PARAMETER Project
    Specifies either the name of the Team Project or a previously initialized Microsoft.TeamFoundation.WorkItemTracking.Client.Project object to connect to. If omitted, it defaults to the connection opened by Connect-TfsTeamProject (if any).

For more details, see the Get-TfsTeamProject cmdlet.
 
.PARAMETER Collection
    Specifies either a URL/name of the Team Project Collection to connect to, or a previously initialized TfsTeamProjectCollection object.

When using a URL, it must be fully qualified. The format of this string is as follows:

http[s]://<ComputerName>:<Port>/[<TFS-vDir>/]<CollectionName>

Valid values for the Transport segment of the URI are HTTP and HTTPS. If you specify a connection URI with a Transport segment, but do not specify a port, the session is created with standards ports: 80 for HTTP and 443 for HTTPS.

To connect to a Team Project Collection by using its name, a TfsConfigurationServer object must be supplied either via -Server argument or via a previous call to the Connect-TfsConfigurationServer cmdlet.

For more details, see the Get-TfsTeamProjectCollection cmdlet.
 
.INPUTS
    Microsoft.TeamFoundation.WorkItemTracking.Client.Project
    System.String
#>

Function Get-TfsTeam
{
    [CmdletBinding(DefaultParameterSetName="Get by name")]
    [OutputType([Microsoft.TeamFoundation.Client.TeamFoundationTeam])]
    param
    (
        [Parameter(Position=0, ParameterSetName="Get by name")]
        [Alias("Name")]
        [ValidateScript({($_ -is [string]) -or ($_ -is [Microsoft.TeamFoundation.Client.TeamFoundationTeam])})] 
        [SupportsWildcards()]
        [object]
        $Team = '*',

        [Parameter(Position=0, ParameterSetName="Get default team")]
        [switch]
        $Default,

        [Parameter()]
        [switch]
        $IncludeMembers,

        [Parameter()]
        [Microsoft.TeamFoundation.Framework.Common.MembershipQuery]
        $QueryMembership = [Microsoft.TeamFoundation.Framework.Common.MembershipQuery]::Direct,

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

        [Parameter()]
        [object]
        $Collection
    )

    Process
    {
        if ($Team -is [Microsoft.TeamFoundation.Client.TeamFoundationTeam])
        {
            return $Team
        }

        $tp = Get-TfsTeamProject -Project $Project -Collection $Collection
        $tpc = $tp.Store.TeamProjectCollection
        $teamService = $tpc.GetService([type]'Microsoft.TeamFoundation.Client.TfsTeamService')

        if ($Default)
        {
            $teams = @($teamService.GetDefaultTeam($tp.Uri, $null))
        }
        else
        {
            $teams = $teamService.QueryTeams([string]$tp.Uri) | ? Name -like $Team
        }

        foreach($t in $teams)
        {
            if ($IncludeMembers)
            {
                $members = $t.GetMembers($tpc, $QueryMembership)
                $t | Add-Member -Name 'Members' -MemberType ([System.Management.Automation.PSMemberTypes]::NoteProperty) -Value $members -PassThru

            }
            else
            {
                $t | Add-Member -Name 'Members' -MemberType ([System.Management.Automation.PSMemberTypes]::NoteProperty) -Value ([Microsoft.TeamFoundation.Framework.Client.TeamFoundationIdentity[]] @()) -PassThru
            }
        }
    }
}
<#
 
.SYNOPSIS
    Creates a new team.
 
.PARAMETER Project
    Specifies either the name of the Team Project or a previously initialized Microsoft.TeamFoundation.WorkItemTracking.Client.Project object to connect to. If omitted, it defaults to the connection opened by Connect-TfsTeamProject (if any).

For more details, see the Get-TfsTeamProject cmdlet.
 
.PARAMETER Collection
    Specifies either a URL/name of the Team Project Collection to connect to, or a previously initialized TfsTeamProjectCollection object.

When using a URL, it must be fully qualified. The format of this string is as follows:

http[s]://<ComputerName>:<Port>/[<TFS-vDir>/]<CollectionName>

Valid values for the Transport segment of the URI are HTTP and HTTPS. If you specify a connection URI with a Transport segment, but do not specify a port, the session is created with standards ports: 80 for HTTP and 443 for HTTPS.

To connect to a Team Project Collection by using its name, a TfsConfigurationServer object must be supplied either via -Server argument or via a previous call to the Connect-TfsConfigurationServer cmdlet.

For more details, see the Get-TfsTeamProjectCollection cmdlet.
 
.PARAMETER Passthru
    Returns the results of the command. By default, this cmdlet does not generate any output.
 
.INPUTS
    System.String
#>

Function New-TfsTeam
{
    [CmdletBinding()]
    [OutputType([Microsoft.TeamFoundation.Client.TeamFoundationTeam])]
    param
    (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [Alias("Name")]
        [string] 
        $Team,
    
        [Parameter()]
        [string] 
        $Description,
    
        [Parameter()]
        [object]
        $Project,

        [Parameter()]
        [object]
        $Collection,

        [Parameter()]
        [switch]
        $Passthru
    )

    Process
    {
        $tp = Get-TfsTeamProject -Project $Project -Collection $Collection
        $tpc = $tp.Store.TeamProjectCollection
        $teamService = $tpc.GetService([type]"Microsoft.TeamFoundation.Client.TfsTeamService")

        $newTeam = $teamService.CreateTeam($tp.Uri, $Team, $Description, $null)

        if ($Passthru)
        {
            return $team
        }
    }
}
<#
 
.SYNOPSIS
    Deletes a team.
 
.PARAMETER Project
    Specifies either the name of the Team Project or a previously initialized Microsoft.TeamFoundation.WorkItemTracking.Client.Project object to connect to. If omitted, it defaults to the connection opened by Connect-TfsTeamProject (if any).

For more details, see the Get-TfsTeamProject cmdlet.
 
.PARAMETER Collection
    Specifies either a URL/name of the Team Project Collection to connect to, or a previously initialized TfsTeamProjectCollection object.

When using a URL, it must be fully qualified. The format of this string is as follows:

http[s]://<ComputerName>:<Port>/[<TFS-vDir>/]<CollectionName>

Valid values for the Transport segment of the URI are HTTP and HTTPS. If you specify a connection URI with a Transport segment, but do not specify a port, the session is created with standards ports: 80 for HTTP and 443 for HTTPS.

To connect to a Team Project Collection by using its name, a TfsConfigurationServer object must be supplied either via -Server argument or via a previous call to the Connect-TfsConfigurationServer cmdlet.

For more details, see the Get-TfsTeamProjectCollection cmdlet.
 
.INPUTS
    Microsoft.TeamFoundation.Client.TeamFoundationTeam
    System.String
#>

Function Remove-TfsTeam
{
    [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')]
    [OutputType([Microsoft.TeamFoundation.Client.TeamFoundationTeam])]
    param
    (
        [Parameter(Position=0, ValueFromPipeline=$true)]
        [Alias("Name")]
        [ValidateScript({($_ -is [string]) -or ($_ -is [Microsoft.TeamFoundation.Client.TeamFoundationTeam])})] 
        [SupportsWildcards()]
        [object]
        $Team = '*',

        [Parameter()]
        [object]
        $Project,

        [Parameter()]
        [object]
        $Collection
    )

    Process
    {
        $t = Get-TfsTeam -Team $Team -Project $Project -Collection $Collection

        if ($PSCmdlet.ShouldProcess($t.Name, 'Delete team'))
        {
            $tp = Get-TfsTeamProject -Project $Project -Collection $Collection
            $tpc = $tp.Store.TeamProjectCollection
            $identityService = $tpc.GetService([type]'Microsoft.TeamFoundation.Framework.Client.IIdentityManagementService')

            $identityService.DeleteApplicationGroup($t.Identity.Descriptor)
        }
    }
}
<#
 
.SYNOPSIS
    Renames a team.
 
.PARAMETER Project
    Specifies either the name of the Team Project or a previously initialized Microsoft.TeamFoundation.WorkItemTracking.Client.Project object to connect to. If omitted, it defaults to the connection opened by Connect-TfsTeamProject (if any).

For more details, see the Get-TfsTeamProject cmdlet.
 
.PARAMETER Collection
    Specifies either a URL/name of the Team Project Collection to connect to, or a previously initialized TfsTeamProjectCollection object.

When using a URL, it must be fully qualified. The format of this string is as follows:

http[s]://<ComputerName>:<Port>/[<TFS-vDir>/]<CollectionName>

Valid values for the Transport segment of the URI are HTTP and HTTPS. If you specify a connection URI with a Transport segment, but do not specify a port, the session is created with standards ports: 80 for HTTP and 443 for HTTPS.

To connect to a Team Project Collection by using its name, a TfsConfigurationServer object must be supplied either via -Server argument or via a previous call to the Connect-TfsConfigurationServer cmdlet.

For more details, see the Get-TfsTeamProjectCollection cmdlet.
 
.INPUTS
    Microsoft.TeamFoundation.Client.TeamFoundationTeam
    System.String
#>

Function Rename-TfsTeam
{
    [OutputType([Microsoft.TeamFoundation.Client.TeamFoundationTeam])]
    param
    (
        [Parameter(Position=0, ValueFromPipeline=$true)]
        [Alias("Name")]
        [ValidateScript({($_ -is [string]) -or ($_ -is [Microsoft.TeamFoundation.Client.TeamFoundationTeam])})] 
        [SupportsWildcards()]
        [object]
        $Team = '*',

        [Parameter()]
        [string]
        $NewName,

        [Parameter()]
        [object]
        $Project,

        [Parameter()]
        [object]
        $Collection
    )

    Process
    {
        Set-TfsTeam -Team $Team -NewName $NewName -Project $Project -Collection $Collection
    }
}
<#
 
.SYNOPSIS
    Changes the details of a team.
 
.PARAMETER Project
    Specifies either the name of the Team Project or a previously initialized Microsoft.TeamFoundation.WorkItemTracking.Client.Project object to connect to. If omitted, it defaults to the connection opened by Connect-TfsTeamProject (if any).

For more details, see the Get-TfsTeamProject cmdlet.
 
.PARAMETER Collection
    Specifies either a URL/name of the Team Project Collection to connect to, or a previously initialized TfsTeamProjectCollection object.

When using a URL, it must be fully qualified. The format of this string is as follows:

http[s]://<ComputerName>:<Port>/[<TFS-vDir>/]<CollectionName>

Valid values for the Transport segment of the URI are HTTP and HTTPS. If you specify a connection URI with a Transport segment, but do not specify a port, the session is created with standards ports: 80 for HTTP and 443 for HTTPS.

To connect to a Team Project Collection by using its name, a TfsConfigurationServer object must be supplied either via -Server argument or via a previous call to the Connect-TfsConfigurationServer cmdlet.

For more details, see the Get-TfsTeamProjectCollection cmdlet.
 
.INPUTS
    Microsoft.TeamFoundation.Client.TeamFoundationTeam
    System.String
#>

Function Set-TfsTeam
{
    [CmdletBinding(DefaultParameterSetName="Get by name")]
    [OutputType([Microsoft.TeamFoundation.Client.TeamFoundationTeam])]
    param
    (
        [Parameter(Position=0, ValueFromPipeline=$true)]
        [Alias("Name")]
        [ValidateScript({($_ -is [string]) -or ($_ -is [Microsoft.TeamFoundation.Client.TeamFoundationTeam])})] 
        [SupportsWildcards()]
        [object]
        $Team = '*',

        [Parameter()]
        [switch]
        $Default,

        [Parameter()]
        [string]
        $NewName,

        [Parameter()]
        [string]
        $Description,

        [Parameter()]
        [object]
        $Project,

        [Parameter()]
        [object]
        $Collection
    )

    Process
    {
        $t = Get-TfsTeam -Team $Team -Project $Project -Collection $Collection
        
        if ($Project)
        {
            $tp = Get-TfsTeamProject -Project $Project -Collection $Collection
            $tpc = $tp.Store.TeamProjectCollection
        }
        else
        {
            $tpc = Get-TfsTeamProjectCollection -Collection $Collection
        }

        $teamService = $tpc.GetService([type]'Microsoft.TeamFoundation.Client.TfsTeamService')

        if ($NewName)
        {
            $t.Name = $NewName
        }

        if ($PSBoundParameters.ContainsKey('Description'))
        {
            $t.Description = $Description
        }

        if ($Default)
        {
            $teamService.SetDefaultTeam($t)
        }

        $teamService.UpdateTeam($t)

        return $t
    }
}