ConfigServer/ConfigServer.psm1
<#
.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. To 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])] Param ( [Parameter(Position=0, ParameterSetName='Get by server', Mandatory=$true, ValueFromPipeline=$true)] [AllowNull()] [object] $Server, [Parameter(Position=0, ParameterSetName="Get current")] [switch] $Current, [Parameter(Position=1, ParameterSetName='Get by server')] [object] $Credential ) Process { if ($Current) { return $Global:TfsServerConnection } 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 } } 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([uri] $Url, $Credential) { $cred = Get-TfsCredential -Credential $Credential return New-Object Microsoft.TeamFoundation.Client.TfsConfigurationServer -ArgumentList ([Uri] $Url), $cred } Function _GetConfigServerFromName($Name, $Credential) { $Servers = Get-TfsRegisteredConfigurationServer $Name foreach($Server in $Servers) { $Url = $Server.Uri $cred = Get-TfsCredential -Credential $Credential $configServer = New-Object Microsoft.TeamFoundation.Client.TfsConfigurationServer -ArgumentList $Url, $cred $configServer.EnsureAuthenticated() Write-Output $configServer } } <# .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. 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 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. For 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 = 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 } <# .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)] [string] $Name = "*" ) Process { if(($Name -eq "localhost") -or ($Name -eq ".")) { $Name = $env:COMPUTERNAME } return [Microsoft.TeamFoundation.Client.RegisteredTfsConnections]::GetConfigurationServers() | ? Name -Like $Name } } |