Git/Branch/Git_Branch.ps1

<#
.SYNOPSIS
    Gets information from one or more branches in a Git repository.
 
.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-TfsGitBranch
{
    [CmdletBinding()]
    [OutputType('Microsoft.TeamFoundation.SourceControl.WebApi.GitBranchStats')]
    Param
    (
        [Parameter()]
        [Alias('RefName')]
        [SupportsWildcards()]
        [object] 
        $Branch = '*',

        [Parameter(ValueFromPipeline=$true)]
        [SupportsWildcards()]
        [object] 
        $Repository,

        [Parameter()]
        [object]
        $Project,

        [Parameter()]
        [object]
        $Collection
    )

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

    Process
    {
        if((-not $Repository) -and $Project)
        {
            $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
            $Repository = $tp.Name
        }

        $repos = Get-TfsGitRepository -Repository $Repository -Project $Project -Collection $Collection

        $tpc = Get-TfsTeamProjectCollection -Collection $Collection; if (-not $tpc -or ($tpc.Count -ne 1)) {throw "Invalid or non-existent team project collection $Collection."}

        $client = _GetRestClient 'Microsoft.TeamFoundation.SourceControl.WebApi.GitHttpClient' -Collection $tpc
        
        foreach($repo in $repos)
        {
            if($repo.Size -eq 0)
            {
                Write-Verbose "Repository $($repo.Name) is empty. Skipping."
                continue
            }

            $task = $client.GetBranchesAsync($tp.Name,$repo.Id); $result = $task.Result; if($task.IsFaulted) { throw "Error retrieving branches from repository '$($repo.Name)'" + ": $($task.Exception.InnerExceptions | ForEach-Object {$_.ToString()})" }

            Write-Output $result | Where-Object name -Like $Branch | `
                Add-Member -Name  'Project' -MemberType NoteProperty -Value $repo.ProjectReference.Name -PassThru | `
                Add-Member -Name  'Repository' -MemberType NoteProperty -Value $repo.Name -PassThru | `
                Sort-Object Project, Repository
        }
    }
}