Get-TFSBuildDefinition.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
# Author: Miodrag Milic <miodrag.milic@gmail.com>
# Last Change: 27-Apr-2016.

<#
.SYNOPSIS
    Get the TFS build definition
 
.EXAMPLE
    Get-TFSBuildDefinition Build1
 
    Get the TFS build definition by name.
 
.EXAMPLE
    Get-TFSBuildDefinition Build1 -OutFile build1.json
 
    Exports the TFS build definition named Build1 to JSON file 'build1.json' in the current directory.
 
.EXAMPLE
    Get-TFSBuildDefinition 5 -OutFile .
 
    Exports the build definition to json file in the current directory. '.' is a special value for the file
    to be automatically named as <Project>-<BuildName>.json in the current directory.
.EXAMPLE
    defs | % { def $_.Id -OutFile . }
 
    Export all TFS build definitions on the project. The example is using aliases.
#>

function Get-TFSBuildDefinition{
    [CmdletBinding()]
    param(
        #Build defintion id [int] or name [string]
        $Id=0,      #without 0 API by default returns item with id=1...
        #Export the build to the specified JSON file
        [string]$OutFile,
        #Revision of the build to get
        [int]$Revision=0
    )
    check_credential

    if ( ![String]::IsNullOrEmpty($Id) -and ($Id.GetType() -eq [string])) { $Id = Get-TFSBuildDefinitions -Name $Id | % id }
    if ( [String]::IsNullOrEmpty($Id) ) { throw "Resource with that name doesn't exist" }
    Write-Verbose "Build definition id: '$Id'"

    if ($Revision) { $rev = "revision=$Revision&" }
    $uri = "$proj_uri/_apis/build/definitions/$($Id)?$($rev)api-version=" + $global:tfs.api_version
    Write-Verbose "URI: $uri"

    $params = @{ Uri = $uri; Method = 'Get'}
    $r = invoke_rest $params
    if (!$OutFile)  { return $r }

    if ($OutFile -eq '.') { $OutFile = "{0}-{1}.json" -f $global:tfs.project, $r.Name }
    $r | ConvertTo-Json -Depth 100 | Out-File $OutFile -Encoding UTF8
}

sal def Get-TFSBuildDefinition