TeamProject/TeamProject.psm1

<#
.SYNOPSIS

    Gets a TFS Team Project.

.DESCRIPTION

    The Get-TfsTeamProject cmdlets gets one or more Team Project objects (an instance of Microsoft.TeamFoundation.WorkItemTracking.Client.Project) from the supplied Team Project Collection.

.PARAMETER Project

    Specifies the name of a Team Project. Wildcards are supported.

.PARAMETER Collection

    Specifies either a URL or the name of the Team Project Collection to connect to, or a previously initialized TfsTeamProjectCollection object.

    For more details, see the -Collection argument in the Get-TfsTeamProjectCollection cmdlet.

.PARAMETER Server

    Specifies either a URL or the name of the Team Foundation Server configuration server (the "root" of a TFS installation) to connect to, or a previously initialized Microsoft.TeamFoundation.Client.TfsConfigurationServer object.

    For more details, see the -Server argument in the Get-TfsTeamProjectCollection cmdlet.

.PARAMETER Credential

    Specifies a user account that has permission to perform this action. The default is the current user.

    Type a user name, such as "User01" or "Domain01\User01", or enter a PSCredential object, such as one generated by the Get-Credential cmdlet. If you type a user name, you will be prompted for a password.

    To connect to Visual Studio Online you must enable Alternate Credentials for your user profile and supply that credential in this argument.

    For more information on Alternate Credentials for your Visual Studio Online account, please refer to https://msdn.microsoft.com/library/dd286572#setup_basic_auth.

.INPUTS

    Microsoft.TeamFoundation.Client.TfsTeamProjectCollection

.NOTES

    As with most cmdlets in the TfsCmdlets module, this cmdlet requires a TfsTeamProjectCollection object to be provided via the -Collection argument. If absent, it will default to the connection opened by Connect-TfsTeamProjectCollection.

#>

Function Get-TfsTeamProject
{
    [CmdletBinding(DefaultParameterSetName='Get by project')]
    [OutputType([Microsoft.TeamFoundation.WorkItemTracking.Client.Project])]
    Param
    (
        [Parameter(Position=0, ParameterSetName='Get by project')]
        [object] 
        $Project = '*',

        [Parameter(ValueFromPipeline=$true, Position=1, ParameterSetName='Get by project')]
        [object]
        $Collection,

        [Parameter(Position=0, ParameterSetName="Get current")]
        [switch]
        $Current,

        [Parameter()]
        [System.Management.Automation.Credential()]
        [System.Management.Automation.PSCredential]
        $Credential
    )

    Process
    {
        if ($Current)
        {
            return $global:TfsProjectConnection
        }

        if ($Project -is [Microsoft.TeamFoundation.WorkItemTracking.Client.Project])
        {
            return $Project
        }

        if (($Project -is [uri]) -or ([System.Uri]::IsWellFormedUriString($Project, [System.UriKind]::Absolute)))
        {
            $tpc = Get-TfsTeamProjectCollection $Collection -Credential $Credential
            $css = $tpc.GetService([type]'Microsoft.TeamFoundation.Server.ICommonStructureService')

            $projInfo = $css.GetProject([string] $Project)
            $Project = $projInfo.Name
        }

        if ($Project -is [string])
        {
            $tpc = Get-TfsTeamProjectCollection $Collection -Credential $Credential
            $wiStore = $tpc.GetService([type]'Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore')

            return _GetAllProjects $tpc | ? Name -Like $Project | % { $wiStore.Projects[$_.Name] }
        }

        if ($Project -eq $null)
        {
            if ($global:TfsProjectConnection)
            {
                return $global:TfsProjectConnection
            }
        }

        throw "No TFS team project information available. Either supply a valid -Project argument or use Connect-TfsTeamProject prior to invoking this cmdlet."
    }
}

Function _GetAllProjects
{
    param ($tpc)

    $css = $tpc.GetService([type]'Microsoft.TeamFoundation.Server.ICommonStructureService')

    return $css.ListAllProjects() | ? Status -eq WellFormed
}