Public/Get-SlackFileInfo.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
function Get-SlackFileInfo {
    <#
    .SYNOPSIS
        Get Slack file info
    .DESCRIPTION
        Get Slack file info
        
        We query the first 100 files unless you specify -Paging
    .PARAMETER Token
        Token to use for the Slack API
        Default value is the value set by Set-PSSlackConfig
    .PARAMETER Channel
        If specified, search for files in this channel (ID)
    .PARAMETER Before
        If specified, search for files created before this date
    .PARAMETER After
        If specified, search for files created after this date
    .PARAMETER Types
        If specified, search for files of this type:
            all - All files
            spaces - Posts
            snippets - Snippets
            images - Image files
            videos - Video files
            gdocs - Google docs
            zips - Zip files
            pdfs - PDF files
    .PARAMETER User
        If specified, search for files by this user
    .Parameter Raw
        Return raw output. If specified, Name parameter is ignored
    .PARAMETER Paging
        If specified, and more data is available, continue querying Slack until we have retrieved all the data available.
    .PARAMETER Count
        Number of messages to return per query. Defaults to 100
    .PARAMETER MaxQueries
        Limit the count of API queries to this number. Only used if you enable -Paging
    .EXAMPLE
        Get-SlackFileInfo
        # Lists up to 100 files
    .EXAMPLE
        Get-SlackFileInfo -Paging
        # Lists all files, querying 100 at a time
    .EXAMPLE
        Get-SlackFileInfo -User wframe -Type csv -Paging
        # Lists all CSV files uploaded by wframe
    .EXAMPLE
        Get-SlackFileInfo -User wframe -Channel C58AHBEPJ
        # Lists up to 100 files from channel C58AHBEPJ
    .EXAMPLE
        Get-SlackFileInfo -Before (Get-Date).AddDays(-7) -After (Get-Date).AddDays(-14) -Paging
        # Get all files from a week ago
    .FUNCTIONALITY
        Slack
    #>

    [cmdletbinding()]
    param (
        [string]$Token = $Script:PSSlack.Token,
        [string]$Channel,
        [datetime]$Before,
        [datetime]$After,
        [validateset('all','spaces','snippets','images','videos','gdocs','zips','pdfs')]
        [string[]]$Types,
        [string]$User,
        [switch]$Paging,
        [ValidateRange(1,1000)]
        [int]$Count = 100,
        [switch]$Raw,
        [int]$MaxQueries
    )
    begin
    {
        Write-Verbose "$($PSBoundParameters | Out-String)"
        $body = @{
            count = $count
        }
        if($User)
        {
            $u = $null
            if($Script:_PSSlackUserMap.ContainsKey($User)){
                $u = $Script:_PSSlackUserMap[$User]
            }
            else {
                $map = Get-SlackUserMap -Update -Token $Token
                if($map.ContainsKey($User))
                {
                    $u = $map[$User]
                }
                else
                {
                    Write-Warning "Could not find user [$User]. Check Get-SlackUserMap for valid names"
                }
            }
            if($u)
            {
                $body.add('user', $u)
            }
        }
        if($PSBoundParameters.ContainsKey('Channel'))
        {
            $body.add('channel', $Channel)
        }
        if($PSBoundParameters.ContainsKey('Types'))
        {
            $body.add('types', $($Types -join ','))
        }
        $BeforeTS = $null
        $AfterTS = $null
        if($PSBoundParameters.ContainsKey('Before'))
        {
            $BeforeTS = Get-UnixTime -Date $Before
            $body.add('ts_to', $BeforeTS)
        }
        if($PSBoundParameters.ContainsKey('After'))
        {
            $AfterTS = Get-UnixTime -Date $After
            $body.add('ts_from', $AfterTS)
        }
        $params = @{
            Token = $Token
            Method = 'files.list'
            Body = $body
        }
        $Queries = 1
        $has_more = $false
        do
        {
            $response = Send-SlackApi @params
            Write-Debug "$($Response | Format-List -Property * | Out-String)"
            if ($response.ok)
            {
                if($response.psobject.properties.name -contains 'paging' -and $response.paging.page -lt $response.paging.pages)
                {
                    Write-Debug 'Paging engaged!'
                    $has_more = $true
                    $Params.body.page = 1 + $response.paging.page
                }
                elseif ($response.psobject.properties.name -contains 'paging' -and $response.paging.page -like $response.paging.pages)
                {
                    $has_more = $false
                }
                else {
                    # Might need this case later - is paging always included? Is this an error?
                    $has_more = $false
                }
                if($Raw)
                {
                    $response
                }
                else
                {
                    Parse-SlackFile -InputObject $Response 
                }
            }
            else 
            {
                $response
            }
            $Queries++
        }
        until (
            -not $Paging -or
            -not $has_more -or
            ($MaxQueries -and $Queries -gt $MaxQueries)
        )
    }
}