Public/New-SlackField.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
function New-SlackField
{
    <#
    .SYNOPSIS
        Creates an array of Slack message attachment fields from an arbitrary PowerShell object

    .DESCRIPTION
        Creates an array of Slack message attachment fields from an arbitrary PowerShell object

        By default, retrieves all properties. You can use parameters to restrict this.

    .PARAMETER InputObject
        A plain-text summary of the attachment. This text will be used in clients that don't show formatted text (eg. IRC, mobile notifications) and should not contain any markup.

    .PARAMETER Short
        Whether to try to fit the field into a table, rather than a list

    .PARAMETER IncludeProperty
        If specified, include only these properties from the InputObject's properties
    
    .PARAMETER ExcludeProperty
        If specified, exclude these properties from the InputObject's properties

    .PARAMETER MemberType
        If specified, restrict the properties we discover from InputObject to these member types.

        Defaults to NoteProperty, Property, ScriptProperty

    .EXAMPLE
        [pscustomobject]@{
            One = 1
            Two = 2
            Five = 5
        } | New-SlackField

        # Simple illustration: pipe anything that produces an object into New-SlackField.

    .EXAMPLE
        $Fields = [pscustomobject]@{
            AlertName = 'The System Is Down'
            Severity = 11
            ImpactedDepartment = 'All'
            URL = 'https://www.youtube.com/watch?v=TmpRs7xN06Q'
        } | New-SlackField -Short

        New-SlackMessageAttachment -Color $_PSSlackColorMap.orange `
                                   -Fields $Fields `
                                   -Fallback 'Your client is bad' |
            New-SlackMessage -Channel '@wframe' `
                             -IconEmoji :bomb: `
                             -AsUser `
                             -Username 'SCOM Bot' |
            Send-SlackMessage -Token $Token

        # Build an imaginary SCOM alert, send it through New-SlackField, short mode
        # Send a Slack message with that field in an attachment

    .LINK
        https://github.com/RamblingCookieMonster/PSSlack

    .LINK
        https://api.slack.com/docs/attachments

    .LINK
        https://api.slack.com/methods/chat.postMessage
    #>

    [CmdletBinding(DefaultParameterSetName='InputObject')]
    Param
    (
        [Parameter(Mandatory = $true,
                   ParameterSetName = 'InputObject',
                   ValueFromPipeline = $True)]
        [Object[]]
        $InputObject,

        [switch]$Short,

        [string[]]$IncludeProperty,

        [string[]]$ExcludeProperty,

        [string[]]$MemberType
    )

    Process
    {
        foreach($Object in $InputObject)
        {
            $Params = @{}
            if($ExcludeProperty)
            {
                $Params.add('ExcludeProperty', $ExcludeProperty)
            }
            if($MemberType)
            {
                $Params.add('MemberType', $MemberType)
            }
            $Properties = Get-PropertyOrder @params -InputObject $Object

            if($IncludeProperty)
            {
                $Properties = $Properties | Where {$IncludeProperty -contains $_}
            }

            foreach($Property in $Properties)
            {
                $Field = @{
                    title = $Property
                    value = $Object.$Property
                }
                if($Short)
                {
                    $Field.add('short',$true)
                }
                Add-ObjectDetail -InputObject $Field -TypeName 'PSSlack.Field'
            }
        }
    }
}