ConfigServer/ConfigServer.psm1

Function Get-TfsConfigurationServer
{
    [CmdletBinding(DefaultParameterSetName='Get by server')]
    [OutputType([Microsoft.TeamFoundation.Client.TfsConfigurationServer])]
    Param
    (
        [Parameter(Position=0, ParameterSetName='Get by server', Mandatory=$true)]
        [AllowNull()]
        [object] 
        $Server,
    
        [Parameter(Position=0, ParameterSetName="Get current")]
        [switch]
        $Current,

        [Parameter(Position=1, ParameterSetName='Get by server')]
        [System.Management.Automation.Credential()]
        [System.Management.Automation.PSCredential]
        $Credential
    )

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

        if ($Server -is [Uri])
        {
            return _GetConfigServerFromUrl $Server $Credential
        }

        if ($Server -is [string])
        {
            if ([Uri]::IsWellFormedUriString($Server, [UriKind]::Absolute))
            {
                return _GetConfigServerFromUrl ([Uri] $Server) $Credential
            }

            if (-not [string]::IsNullOrWhiteSpace($Server))
            {
                return _GetConfigServerFromName $Server $Credential
            }

            $Server = $null
        }

        if ($Server -eq $null)
        {
            if ($Global:TfsServerConnection)
            {
                return $Global:TfsServerConnection
            }
        }

        throw "No TFS connection information available. Either supply a valid -Server argument or use Connect-TfsConfigurationServer prior to invoking this cmdlet."
    }
}

# =================
# Helper Functions
# =================

Function _GetConfigServerFromUrl
{
    Param ($Url, $Cred)
    
    if ($Cred)
    {
        $configServer = New-Object Microsoft.TeamFoundation.Client.TfsConfigurationServer -ArgumentList ([Uri] $Url), ([System.Net.NetworkCredential] $cred)
    }
    else
    {
        $configServer = [Microsoft.TeamFoundation.Client.TfsConfigurationServerFactory]::GetConfigurationServer([Uri] $Url)
    }


    $configServer.EnsureAuthenticated()
    return $configServer
}

Function _GetConfigServerFromName
{
    Param ($Name, $Cred)

    $Servers = Get-TfsRegisteredConfigurationServer $Name
    
    foreach($Server in $Servers)
    {
        if ($Cred)
        {
            $configServer = New-Object Microsoft.TeamFoundation.Client.TfsConfigurationServer -ArgumentList ($Server.Uri), ([System.Net.NetworkCredential] $cred)
        }
        else
        {
            $configServer = [Microsoft.TeamFoundation.Client.TfsConfigurationServerFactory]::GetConfigurationServer($Server)
        }

        $configServer.EnsureAuthenticated()
        $configServer
    }
}
Function Get-TfsConfigurationServerConnectionString
{
    [CmdletBinding()]
    [OutputType([string])]
    Param
    (
        [Parameter()]
        [string]
        [Alias('Session')]
        $Computer,

        [Parameter()]
        [string]
        $Version = '12.0',

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

    Process
    {

        $scriptBlock = New-ScriptBlock -EntryPoint '_GetConnectionString' -Dependency 'Get-InstallationPath', 'Test-RegistryValue', 'Get-RegistryValue'

        return Invoke-ScriptBlock -ScriptBlock $scriptBlock -Computer $Computer -Credential $Credential -ArgumentList $Version
    }
}

Function _GetConnectionString($Version)
{
    $path = Get-InstallationPath -Version $Version -Component ApplicationTier
    $webConfigPath = Join-Path $path 'Web Services\Web.config'
    $webConfig = [xml] (Get-Content $webConfigPath)

    return (Select-Xml -Xml $webConfig -XPath '/configuration/appSettings/add[@key="applicationDatabase"]/@value').Node.Value
}
Function Get-TfsRegisteredConfigurationServer
{
    [CmdletBinding()]
    [OutputType([Microsoft.TeamFoundation.Client.RegisteredConfigurationServer[]])]
    Param
    (
        [Parameter(Position=0, ValueFromPipeline=$true)]
        [string]
        $Name = "*"
    )

    Process
    {
        if(($Name -eq "localhost") -or ($Name -eq "."))
        {
            $Name = $env:COMPUTERNAME
        }

        return [Microsoft.TeamFoundation.Client.RegisteredTfsConnections]::GetConfigurationServers() | ? Name -Like $Name
    }
}