Public/Get-SlackUser.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
120
121
122
123
124
125
126
127
128
129
130
131
function Get-SlackUser {
    <#
    .SYNOPSIS
        Get info on a Slack user

    .DESCRIPTION
        Get info on a Slack user

    .PARAMETER Token
        Token to use for the Slack API

        Default value is the value set by Set-PSSlackConfig

    .PARAMETER Presence
        Whether to include presence information

    .PARAMETER Billing
        Whether to include billing info

    .Parameter Name
        Optional. One or more names to search for. Accepts wildcards.

    .Parameter Raw
        Return raw output. If specified, Name parameter is ignored

    .EXAMPLE
        Get-SlackUser -Token $Token `
                      -Name ps*

        # Get users with name starting 'ps'

    .EXAMPLE
        Get-SlackUser -Token $Token -Presence -Billing

        # Get all users in the team, including bots, as well as presence and billing info

    .FUNCTIONALITY
        Slack
    #>


    [cmdletbinding(DefaultParameterSetName = 'Content')]
    param (
        [string]$Token = $Script:PSSlack.Token,
        [string[]]$Name,
        [switch]$Presence,
        [switch]$Billing,
        [switch]$ExcludeBots,
        [switch]$Raw
    )
    begin
    {
        $body = @{}
        if($Presence)
        {
            $body.add('presence', 1)
        }

        $params = @{
            Token = $Token
            Method = 'users.list'
        }
        if($body.keys.count -gt 0)
        {
            $params.add('body', $Body)
        }
        $RawUsers = Send-SlackApi @params

        $HasWildCard = $False
        foreach($Item in $Name)
        {
            if($Item -match '\*')
            {
                $HasWildCard = $true
                break
            }
        }

        if($Billing)
        {
            $BillingInfo = Send-SlackApi -Token $Token -Method team.billableInfo
            $UserIDs = $BillingInfo.billable_info.psobject.properties.name
            foreach($User in $RawUsers.members)
            {
                $UserId = $User.Id
                if($UserIDs -contains $UserId)
                {
                    Add-Member -InputObject $User -MemberType NoteProperty -Name BillingActive -Value $BillingInfo.billable_info.$UserId.billing_active -Force
                }
            }
        }

        if($Name -and -not $HasWildCard)
        {
            # torn between independent queries, or filtering users.list
            # submit a PR if this isn't performant enough or doesn't make sense.
            $Users = $RawUsers.members |
                Where-Object {$Name -Contains $_.name}
        }
        elseif ($Name -and $HasWildCard)
        {
            $AllUsers = $RawUsers.members

            # allow like operator on each channel requested in the param, avoid dupes
            $UserHash = [ordered]@{}
            foreach($SlackUser in $AllUsers)
            {
                foreach($Username in $Name)
                {
                    if($SlackUser.Name -like $Username -and -not $UserHash.Contains($SlackUser.id))
                    {
                        $UserHash.Add($SlackUser.Id, $SlackUser)
                    }
                }
            }
            $Users = $UserHash.Values
        }
        else # nothing specified
        {
            $Users = $RawUsers.members
        }

        if($Raw)
        {
            $RawUsers
        }
        else
        {
            Parse-SlackUser -InputObject $Users
        }
    }
}