public/Send-BuildStopTaskMessage.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
function Send-BuildStopTaskMessage {
    [CmdletBinding()]
    param(
        [object]$Task
    )

    if ($Task.Error)
    {
        $script:failedTask = $true

        $message = Get-BuildMessage -Failed

        $primaryAttachment = @{
            Color = '#FF0000'
            Title = ":x: Failed Task: $($Task.Name)"
            Fallback = "Failed Task: $($Task.Name)"
            Text = $Task.Error
        }
    }
    else
    {
        $message = Get-BuildMessage -Success

        $primaryAttachment = @{
            Color = '#228B22' 
            Title = ":white_check_mark: Exiting Task: $($Task.Name)"
            Text = "Time: {0:g}" -f $Task.Elapsed
            Fallback = "Exiting Task: $($Task.Name)"
        }
    }

    $null = New-SlackMessageAttachment @primaryAttachment |
        New-SlackMessage @message |
        Send-SlackMessage
}