Get-TFSBuildLogs.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
58
59
# Author: Miodrag Milic <miodrag.milic@gmail.com>
# Last Change: 05-Aug-2016.

<#
.SYNOPSIS
    Get the unified build logs for the TFS build
 
.EXAMPLE
    PS> Get-TFSBuildLogs
 
    Returns logs of the latest build
 
.EXAMPLE
    PS> Get-TFSBuildLogs 250
 
    Returns logs of the build by id
 
.EXAMPLE
    PS> Get-TFSBuilds -Definitions MyDefinition | select -First 1 | % BuildNumber | % { Get-TFSBuildLogs $_ }
 
    Return logs of the latest build for given build definition
#>

function Get-TFSBuildLogs{
    [CmdletBinding()]
    param(
        #Id of the build, by default the latest build is used.
        [string]$Id
    )
    check_credential

    if ($Id -eq '') { $Id = Get-TFSBuilds -Top 1 | %{ $_.Id } }
    if ($Id -eq $null) { throw "Can't find latest build or there are no builds" }
    Write-Verbose "Build id: $Id"
    
    $uri = "$proj_uri/_apis/build/builds/$Id/logs?api-version=" + $global:tfs.api_version
    Write-Verbose "Logs URI: $uri"

    $params = @{ Uri = $uri; Method = 'Get'}
    $r = invoke_rest $params

    $lines = @()
    $root_server_name = $global:tfs.root_url -split '/' | select -Index 2
    foreach ( $url in $r.value.url ) {
        #TFS might return non FQDM so its best to replace its server name with the one user specified
        $new_url = $url -split '/'
        $new_url[2] = $root_server_name
        $new_url = $new_url -join '/'

        Write-Verbose "Log URI: $new_url"
        $params = @{ Uri = $new_url; Method = 'Get'}
        $l = invoke_rest $params
        $lines += $l.value -replace '\..+?Z'
        $lines += "="*150
    }
    $lines
}

sal blogs Get-TFSBuildLogs