Team/Team.psm1

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
    }
}
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
            }
        }
    }
}
Function New-TfsTeam
{
    [CmdletBinding()]
    [OutputType([Microsoft.TeamFoundation.Client.TeamFoundationTeam])]
    param
    (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [Alias("Name")]
        [string] 
        $Team,
    
        [Parameter()]
        [string] 
        $Description,
    
        [Parameter(ValueFromPipeline=$true)]
        [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
        }
    }
}
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)
        }
    }
}
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
    }
}
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)
}