Public/Get-SlackWebChannel.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
function Get-SlackWebChannel {
    Param (
        [string]$Name,
        [string]$SearchString,
        [switch]$ExcludeMine
    )
    <# Param (
        [switch]$IncludePublic,
        [switch]$IncludePrivate,
        [switch]$IncludeArchived,
        [switch]$IncludeConnections,
        [switch]$ExcludeMine
    ) #>


    # exclude_archived = IncludePublic + IncludePrivate + IncludeConnections (All)
    # private_exclude = IncludePublic + $IncludeArchived (Public) + IncludeConnections (Public)
    # private = IncludePrivate + $IncludeArchived (Private) + IncludeConnections (Private)
    # archived = IncludeArchived (All)
    # external_shared = IncludeConnections (All)

    $Modes = @(
        'exclude_archived'
        'private_exclude'
        'private'
        'archived'
        'external_shared'
    )

    $FormData = @{
        module              = 'channels'
        exclude_my_channels = 0
    }
    if ($ExcludeMine) {
        $FormData.exclude_my_channels = 1
    }

    if ($SearchString ) {
        $FormData.query = $SearchString
    }
    elseif ($Name -and -not $SearchString) {
        $FormData.query = $Name
    }

    $ReturnedItems = New-Object -TypeName 'System.Collections.Generic.List[String]'
    foreach ($Mode in $Modes) {
        $FormData.channel_type = $Mode

        $Response = Invoke-SearchRequest -FormData $FormData
        $ProcessedItems = $Response | Select-Object -Property 'id', 'name', 'member_count', 'is_member', 'purpose'
        foreach ($Item in $ProcessedItems) {
            if ($ReturnedItems -notcontains $Item.id) {
                if ($Name -and $Item.name -like $Name -or -not $Name) {
                    $Item
                    $ReturnedItems.Add($Item.id)
                }
            }
        }
    }
}