functions/get-d365tfsworkspace.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

<#
    .SYNOPSIS
        Get the TFS / VSTS registered workspace path
         
    .DESCRIPTION
        Gets the workspace path from the configuration of the local tfs in visual studio
         
    .PARAMETER Path
        Path to the directory where the Team Foundation Client executable is located
         
    .PARAMETER TfsUri
        Uri to the TFS / VSTS that the workspace is connected to
         
    .EXAMPLE
        PS C:\> Get-D365TfsWorkspace -TfsUri https://PROJECT.visualstudio.com
         
        This will invoke the default tf.exe client located in the Visual Studio 2015 directory
        and fetch the configured URI.
         
    .NOTES
        Tags: TFS, VSTS, URL, URI, Servicing, Development
         
        Author: Mötz Jensen (@Splaxi)
#>

function Get-D365TfsWorkspace {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Parameter(Mandatory = $false, ParameterSetName = 'Default', Position = 1 )]
        [string]$Path = $Script:TfDir,

        [Parameter(Mandatory = $false, ParameterSetName = 'Default', ValueFromPipelineByPropertyName = $true, Position = 2 )]
        [string]$TfsUri = $Script:TfsUri
    )
    
    $executable = Join-Path $Path "tf.exe"
    if (!(Test-PathExists -Path $executable -Type Leaf)) {return}

    if([system.string]::IsNullOrEmpty($TfsUri)){
        Write-PSFMessage -Level Host -Message "The supplied uri <c='em'>was empty</c>. Please update the active d365 environment configuration or simply supply the -TfsUri to the cmdlet."
        Stop-PSFFunction -Message "Stopping because TFS URI is missing."
        return
    }

    Write-PSFMessage -Level Verbose -Message "Invoking tf.exe"
    #* Small hack to get the output from the execution into a variable.
    $res = & $executable "vc" "workspaces" "/collection:$TfsUri" "/format:detailed" 2>$null

    if (![string]::IsNullOrEmpty($res)) {
        [PSCustomObject]@{
            TfsWorkspacePath = ($res | select-string "meta").ToString().Trim().Split(" ")[1]
        }
    }
    else {
        Write-PSFMessage -Level Host -Message "No matching workspace configuration found for the specified URI. Either the URI is wrong or you haven't configured the server connection / workspace details correctly."
    }
}