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 = [System.Management.Automation.PSCredential]::Empty ) Process { if (($Current) -or (($Server -eq $null) -and ($Global:TfsServerConnection -ne $null))) { 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 } $Server = $null } 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 -ne [System.Management.Automation.PSCredential]::Empty) { return New-Object Microsoft.TeamFoundation.Client.TfsConfigurationServer -ArgumentList ([Uri] $Url), (_GetCredential $cred) } return [Microsoft.TeamFoundation.Client.TfsConfigurationServerFactory]::GetConfigurationServer([Uri] $Url) } Function _GetConfigServerFromName { Param ($Name, $Cred) $Servers = Get-TfsRegisteredConfigurationServer $Name foreach($Server in $Servers) { if ($Cred -ne [System.Management.Automation.PSCredential]::Empty) { $configServer = New-Object Microsoft.TeamFoundation.Client.TfsConfigurationServer -ArgumentList $Server.Uri, (_GetCredential $cred) } else { $configServer = [Microsoft.TeamFoundation.Client.TfsConfigurationServerFactory]::GetConfigurationServer($Server) } $configServer.EnsureAuthenticated() $configServer } } Function _GetCredential { Param ($Cred) if ($Cred) { return [System.Net.NetworkCredential] $Cred } return [System.Net.CredentialCache]::DefaultNetworkCredentials } 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 = [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 } 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 } } |