TfsCmdlets.psm1

# Private functions

Function _GetInstallationPath($Version, $Component = 'BaseInstallation')
{
    if(-not $Version)
    {
        foreach($v in 20..8)
        {
            if(Test-Path "HKLM:\Software\Microsoft\TeamFoundationServer\${v}.0")
            {
                $Version = "${v}.0"
                break
            }
        }
    
        if(-not $Version)
        {
            throw "No Team Foundation Server installation found in computer $([Environment]::MachineName)"
        }
    }

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

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

    if (-not (Test-Path $rootKeyPath))
    {
        throw "Team Foundation Server is not installed in computer $([Environment]::MachineName)"
    }

    if (-not (Test-Path $componentPath))
    {
        throw "Team Foundation Server component '$Component' is not installed in computer $([Environment]::MachineName)"
    }

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

Function _GetConnectionString($Version)
{
    $path = _GetInstallationPath -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 _GetRegistryValue($Path, $Value)
{
    return Get-ItemProperty -Path $Path -ErrorAction Continue | Select-Object -ExpandProperty $Value
}

Function _TestRegistryValue($Path, $Value)
{
    try
    {
        _GetRegistryValue -Path $Path -Value $Value | Out-Null
        return $true
    }
    finally {}

    return $false
}

Set-Alias -Name 'ctfs' -Value 'Connect-TfsTeamProjectCollection'
Set-Alias -Name 'itfs' -Value 'Invoke-TfsRestApi'
Function _EscapeArgumentValue {
    param
    (
        [Parameter(ValueFromPipeline = $true)]
        [string]
        $InputObject    
    )

    Process {
        if ($InputObject.Contains(' ') -or $InputObject.Contains("'") -or $InputObject.Contains('"')) {
            $InputObject = "'" + $InputObject.Replace("'", "''") + "'"
        }
        
        return $InputObject
    }
}
Register-ArgumentCompleter -ParameterName Node -Verbose -ScriptBlock {
    
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    if ($commandName -notlike '*-Tfs*') {
        return
    }

    if ($fakeBoundParameter['Collection']) {
        $tpc = Get-TfsTeamProjectCollection -Collection $fakeBoundParameter['Collection'] -Server $fakeBoundParameter['Server']
    }
    elseif ((Get-TfsTeamProjectCollection -Current)) {
        $tpc = (Get-TfsTeamProjectCollection -Current)
    }
    else {
        return
    }
 
    if ($fakeBoundParameter['Project']) {
        $tp = Get-TfsTeamProject -Collection $fakeBoundParameter['Project'] -Collection $tpc
    }
    elseif ((Get-TfsTeamProject -Current)) {
        $tp = (Get-TfsTeamProject -Current)
    }
    else {
        return
    }

    if ($commandName -like '*Area') {
        return Get-TfsArea -Node "\$wordToComplete*" -Project $tp -Collection $tpc | Select-Object -ExpandProperty RelativePath | Sort-Object | _EscapeArgumentValue
    }
    elseif ($commandName -like '*Iteration') {
        return Get-TfsIteration -Node "\$wordToComplete*" -Project $tp -Collection $tpc | Select-Object -ExpandProperty RelativePath | Sort-Object | _EscapeArgumentValue
    }
}
# Project

Register-ArgumentCompleter -ParameterName Project -Verbose -ScriptBlock {
    
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    if ($commandName -notlike '*-Tfs*') {
        return
    }

    if ($fakeBoundParameter['Collection']) {
        $tpc = Get-TfsTeamProjectCollection -Collection $fakeBoundParameter['Collection'] -Server $fakeBoundParameter['Server']
    }
    elseif ((Get-TfsTeamProjectCollection -Current)) {
        $tpc = (Get-TfsTeamProjectCollection -Current)
    }
    else {
        return
    }
 
    if ($tpc) {
        return Get-TfsTeamProject -Project "$wordToComplete*" -Collection $tpc -Deleted:($commandName -eq 'Undo-TfsTeamProjectRemoval') | Select-Object -ExpandProperty Name | Sort-Object | _EscapeArgumentValue
    }
}
Register-ArgumentCompleter -ParameterName Repository -Verbose -ScriptBlock {
    
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    if ($commandName -notlike '*-Tfs*') {
        return
    }

    if ($fakeBoundParameter['Collection']) {
        $tpc = Get-TfsTeamProjectCollection -Collection $fakeBoundParameter['Collection'] -Server $fakeBoundParameter['Server']
    }
    elseif ((Get-TfsTeamProjectCollection -Current)) {
        $tpc = (Get-TfsTeamProjectCollection -Current)
    }
    else {
        return
    }
 
    if ($fakeBoundParameter['Project']) {
        $tp = Get-TfsTeamProject -Collection $fakeBoundParameter['Project'] -Collection $tpc
    }
    elseif ((Get-TfsTeamProject -Current)) {
        $tp = (Get-TfsTeamProject -Current)
    }
    else {
        return
    }

    return Get-TfsGitRepository -Repository "$wordToComplete*" -Project $tp -Collection $tpc | Select-Object -ExpandProperty Name | Sort-Object | _EscapeArgumentValue
}
Register-ArgumentCompleter -ParameterName Team -Verbose -ScriptBlock {
    
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    if ($commandName -notlike '*-Tfs*') {
        return
    }

    if ($fakeBoundParameter['Collection']) {
        $tpc = Get-TfsTeamProjectCollection -Collection $fakeBoundParameter['Collection'] -Server $fakeBoundParameter['Server']
    }
    elseif ((Get-TfsTeamProjectCollection -Current)) {
        $tpc = (Get-TfsTeamProjectCollection -Current)
    }
    else {
        return
    }
 
    if ($fakeBoundParameter['Project']) {
        $tp = Get-TfsTeamProject -Collection $fakeBoundParameter['Project'] -Collection $tpc
    }
    elseif ((Get-TfsTeamProject -Current)) {
        $tp = (Get-TfsTeamProject -Current)
    }
    else {
        return
    }

    return Get-TfsTeam -Team "$wordToComplete*" -Project $tp -Collection $tpc | Select-Object -ExpandProperty Name | Sort-Object | _EscapeArgumentValue
}