ConfigServer/ConfigServer.ps1

<#
.SYNOPSIS
Gets information about a configuration server.
 
.PARAMETER Server
Specifies either a URL/name of the Team Foundation Server to connect to, or a previously initialized TfsConfigurationServer object.
 
When using a URL, it must be fully qualified. The format of this string is as follows:
 
http[s]://<ComputerName>:<Port>/[<TFS-vDir>/]
 
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.nnTo connect to a Team Foundation Server instance by using its name, it must have been previously registered.
 
.PARAMETER Current
Returns the configuration server specified in the last call to Connect-TfsConfigurationServer (i.e. the "current" configuration server)
 
.PARAMETER Credential
Specifies a user account that has permission to perform this action. The default is the cached credential of the user under which the PowerShell process is being run - in most cases that corresponds to the user currently logged in. To provide a user name and password, and/or to open a input dialog to enter your credentials, call Get-TfsCredential with the appropriate arguments and pass its return to this argument. For more information, refer to https://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.client.tfsclientcredentials.aspx
 
.INPUTS
Microsoft.TeamFoundation.Client.TfsConfigurationServer
System.String
System.Uri
#>

Function Get-TfsConfigurationServer
{
    [CmdletBinding(DefaultParameterSetName='Get by server')]
    [OutputType('Microsoft.TeamFoundation.Client.TfsConfigurationServer')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', '')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUsePSCredentialType', '')]
    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')]
        [object]
        $Credential
    )

    Process
    {
        if ($Current.IsPresent -or (-not $Server))
        {
            return $script:TfsServerConnection
        }

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

        $cred = Get-TfsCredential -Credential $Credential

        if (($Server -is [Uri]) -or ([Uri]::IsWellFormedUriString($Server, [UriKind]::Absolute)))
        {
            return New-Object Microsoft.TeamFoundation.Client.TfsConfigurationServer -ArgumentList ([Uri] $Server), $cred
        }

        if ($Server -is [string] -and (-not [string]::IsNullOrWhiteSpace($Server)))
        {
            $serverNames = Get-TfsRegisteredConfigurationServer -Server $Server
            
            foreach($s in $serverNames)
            {
                Write-Output (New-Object Microsoft.TeamFoundation.Client.TfsConfigurationServer -ArgumentList $s.Uri,  $cred)
            }

            return
        }

        throw 'No TFS connection information available. Either supply a valid -Server argument or use Connect-TfsConfigurationServer prior to invoking this cmdlet.'
    }
}
<#
.SYNOPSIS
    Gets the configuration server database connection string.
 
.PARAMETER Computer
    Specifies the name of a Team Foundation Server application tier from which to retrieve the connection string
 
.PARAMETER Version
    Specifies the version of the Team Foundation Server being queried. Valid values are '12.0' (TFS 2013), '14.0' (TFS 2015), '15.0' (TFS 2017)
 
.PARAMETER Credential
    Specifies a user account that has permission to perform this action. The default is the credential of the user under which the PowerShell process is being run - in most cases that corresponds to the user currently logged in.nnType 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.nnTo connect to Visual Studio Team Services you must either: enable Alternate Credentials for your user profile and supply that credential in this argument or omit this argument to have a logon being dialog displayed automatically.nnFor more information on Alternate Credentials for your Visual Studio Team Services account, please refer to https://msdn.microsoft.com/library/dd286572#setup_basic_auth.
#>

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

        [Parameter()]
        [ValidateSet('12.0', '14.0', '15.0')]
        [string]
        $Version,

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

    Process
    {

        $scriptBlock = _NewScriptBlock -EntryPoint '_GetConnectionString' -Dependency 'Get-InstallationPath', '_TestRegistryValue', '_GetRegistryValue'

        return _InvokeScriptBlock -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
}
<#
.SYNOPSIS
    Gets one or more Team Foundation Server addresses registered in the current computer.
 
.PARAMETER Name
    Specifies the name of a registered server. When omitted, all registered servers are returned. Wildcards are permitted.
 
.INPUTS
    System.String
#>

Function Get-TfsRegisteredConfigurationServer
{
    [CmdletBinding()]
    [OutputType('Microsoft.TeamFoundation.Client.RegisteredConfigurationServer' )]
    Param
    (
        [Parameter(Position=0, ValueFromPipeline=$true)]
        [Alias('Name')]
        [string]
        $Server = "*"
    )

    Begin
    {
        #_ImportRequiredAssembly -AssemblyName 'Microsoft.TeamFoundation.Client'
    }

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

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