Admin/Admin.ps1

<#
.SYNOPSIS
Gets the installation path of a given Team Foundation Server component.
 
.DESCRIPTION
Many times a Team Foundation Server admin needs to retrieve the location where TFS is actually installed. That can be useful, for instance, to locate tools like TfsSecurity or TfsServiceControl. That information is recorded at setup time, in a well-known location in the Windows Registry of the server where TFS is installed.
 
.PARAMETER ComputerName
The machine name of the server where the TFS component is installed. It must be properly configured for PowerShell Remoting in case it's a remote machine. Optionally, a System.Management.Automation.Runspaces.PSSession object pointing to a previously opened PowerShell Remote session can be provided instead.
When omitted, defaults to the local machine where the script is being run
 
.PARAMETER Component
Indicates the TFS component whose installation path is being searched for. For the main TFS installation directory, use BaseInstallation.
When omitted, defaults to BaseInstallation.
 
.PARAMETER Version
The TFS version number, in the format '##.#'. For TFS 2015, use '14.0'
 
.PARAMETER Credential
The user credentials to be used to access a remote machine. Those credentials must have the required permission to execute a PowerShell Remote session on that computer and also the permission to access the Windows Registry.
 
.EXAMPLE
Get-TfsInstallationPath -Version 15.0
Gets the root folder (the BaseInstallationPath) of TFS in the local server where the cmdlet is being run
 
.EXAMPLE
Get-TfsInstallationPath -Computer SPTFSSRV -Version 14.0 -Component SharepointExtensions -Credentials (Get-Credentials)
Gets the location where the SharePoint Extensions have been installed in the remote server SPTFSSRV, prompting for admin credentials to be used for establishing a PS Remoting session to the server
#>

Function Get-TfsInstallationPath
{
    [CmdletBinding()]
    [OutputType('string')]
    Param
    (
        [Parameter()]
        [object]
        [Alias('Session')]
        $ComputerName,

        [Parameter()]
        [ValidateSet('BaseInstallation', 'ApplicationTier', 'SharePointExtensions', 'TeamBuild', 'Tools', 'VersionControlProxy')]
        [string]
        $Component = 'BaseInstallation',

        [Parameter(Mandatory=$true)]
        [ValidateSet('11.0','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 '_GetInstallationPath' -Dependency '_TestRegistryValue', '_GetRegistryValue'

        return _InvokeScriptBlock -ScriptBlock $scriptBlock -Computer $Computer -Credential $Credential -ArgumentList $Version, $Component
    }
}
<#
.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 Start-TfsIdentitySync
{
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', '')]
    [CmdletBinding()]
    Param
    (
        [Parameter(Position=0,ValueFromPipeline=$true)]
        [object] 
        $Server,

        [Parameter()]
        [switch]
        $Wait,

        [Parameter()]
        [object]
        $Credential
    )

    Process
    {
        $srv = Get-TfsConfigurationServer -Server $Server -Credential $Credential

        if($srv.Count -ne 1)
        {
            throw "Invalid or non-existent configuration server $Server"
        }

        $jobSvc = $srv.GetService([type]'Microsoft.TeamFoundation.Framework.Client.ITeamFoundationJobService')
        $syncJobId = [guid]'544dd581-f72a-45a9-8de0-8cd3a5f29dfe'
        $syncJobDef = $jobSvc.QueryJobs() | Where-Object { $_.JobId -eq $syncJobId }

        if ($syncJobDef)
        {
            _Log "Queuing job '$($syncJobDef.Name)' with high priority now"

            $success = ([bool] $jobSvc.QueueJobNow($syncJobDef, $true))

            if (-not $success)
            {
                throw "Failed to queue synchronization job"
            }

            if($Wait.IsPresent)
            {
                do
                {
                    _Log "Waiting for the job to complete"
                    Start-Sleep -Seconds 5

                    $status = $jobSvc.QueryLatestJobHistory($syncJobId)
                    _Log "Current job status: $($status.Result)"
                } while($status.Result -eq 'None')

                return $result
            }
        }
        else
        {
            throw "Could not find Periodic Identity Synchronization job definition (id $syncJobId). Unable to start synchronization process."
        }
    }
}
Function _GetInstallationPath($Version, $Component)
{
    $rootKeyPath = "HKLM:\Software\Microsoft\TeamFoundationServer\$Version"

    if ($Component -eq 'BaseInstallation')
    {
        $componentPath = $rootKeyPath
    }
    else
    {
        $componentPath = "$rootKeyPath\InstalledComponents\$Component"
    }

    if (-not (_TestRegistryValue -Path $rootKeyPath -Value 'InstallPath'))
    {
        throw "Team Foundation Server is not installed in computer $env:COMPUTERNAME"
    }

    if (-not (_TestRegistryValue -Path $componentPath -Value 'InstallPath'))
    {
        throw "Team Foundation Server component '$Component' is not installed in computer $env:COMPUTERNAME"
    }

    return _GetRegistryValue -Path $componentPath -Value 'InstallPath'
}