Admin/Admin.psm1

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

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

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

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

    Process
    {

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

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


Function _GetInstallationPath($Version, $Component)
{
    return Get-InstallationPath @PSBoundParameters
}
Function Test-RegistryValue
{
    Param
    (
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        $Path,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        $Value
    )

    Process
    {
        try
        {
            Get-RegistryValue -Path $Path -Value $Value | Out-Null
            return $true
        }
        catch {}

        return $false

    }
}

Function Get-RegistryValue
{
    Param
    (
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        $Path,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        $Value
    )

    Process
    {
        return Get-ItemProperty -Path $Path | Select-Object -ExpandProperty $Value
    }
}

Function Get-InstallationPath
{
    Param
    (
        [string]
        $Version, 
        
        [string]
        $Component
    )

    $rootKeyPath = "HKLM:\Software\Microsoft\TeamFoundationServer\$Version"

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

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

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

    return Get-RegistryValue -Path $componentPath -Value 'InstallPath'
}
Function New-ScriptBlock($EntryPoint, [string[]]$Dependency)
{
    $entryPoint = (Get-Item "function:$EntryPoint").Definition.Trim()
    $paramSection = $entryPoint.Substring(0, $entryPoint.IndexOf("`n"))
    $bodySection = $entryPoint.Substring($paramSection.Length) + "`n`n"
    
    $body = $paramSection

    foreach($depFn in $Dependency)
    {
        $f = Get-Item "function:$depFn"

        $body += "Function $f `n{`n"
        $body += $f.Definition 
        $body += "`n}`n`n"
    }

    $body += $bodySection

    return [scriptblock]::Create($body)
}

Function Invoke-ScriptBlock($ScriptBlock, $Computer, $Credentials, $ArgumentList)
{
    if (-not $Computer)
    {
        return Invoke-Command -ScriptBlock $scriptBlock -ArgumentList $ArgumentList
    }
    elseif ($Computer -is [System.Management.Automation.Runspaces.PSSession])
    {
        return Invoke-Command -ScriptBlock $scriptBlock -Session $Computer -ArgumentList $ArgumentList
    }

    return Invoke-Command -ScriptBlock $scriptBlock -ComputerName $Computer -Credential $Credential -ArgumentList $ArgumentList
}