Public/Remove-SlackFile.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
function Remove-SlackFile {
    <#
    .SYNOPSIS
        Remove Slack files
    .DESCRIPTION
        Remove Slack files
    .PARAMETER Token
        Token to use for the Slack API

        Default value is the value set by Set-PSSlackConfig
    .PARAMETER ID
        Remove file with this ID
    .PARAMETER Force
        If specified, override all prompts and delete file
    .PARAMETER Raw
        Return raw output
    .EXAMPLE
        Remove-SlackFile -id F18UVDLR3 -Force
        # Remove a specific file without prompts
    .EXAMPLE
        Get-SlackFileInfo -Before (Get-Date).AddYears(-1) | Remove-SlackFile
        # Remove files created over a year ago
    .FUNCTIONALITY
        Slack
    #>

    [cmdletbinding(SupportsShouldProcess=$true, ConfirmImpact="High")]
    param (
        [parameter( ValueFromPipelineByPropertyName = $True)]
        [string]$Id,
        [string]$Token = $Script:PSSlack.Token,
        [switch]$Force,
        [switch]$Raw
    )
    begin
    {
        Write-Verbose "$($PSBoundParameters | Remove-SensitiveData | Out-String)"
        $RejectAll = $false
        $ConfirmAll = $false
    }
    process
    {
        foreach($FileID in $Id)
        {
            $body = @{
                file = $FileID
            }
            $params = @{
                Token = $Token
                Method = 'files.delete'
                Body = $body
            }
            if( ($Force -and -not $WhatIfPreference) -or $PSCmdlet.ShouldProcess( "Removed the file [$FileID]",
                                        "Remove the file [$FileID]?",
                                        "Removing Files" )) {
                if( ($Force -and -not $WhatIfPreference) -or $PSCmdlet.ShouldContinue("Are you REALLY sure you want to remove [$FileID]?",
                                                       "Removing [$FileID]",
                                                       [ref]$ConfirmAll,
                                                       [ref]$RejectAll)) {
                    $Response = $null
                    $Response = Send-SlackApi @params
                    if($Raw)
                    {
                        return $Response
                    }
                    if($Response.ok)
                    {
                        [pscustomobject]@{
                            Id = $FileID
                            ok = $true
                            Raw = $Response
                        }
                    }
                    else
                    {
                        [pscustomobject]@{
                            Id = $FileID
                            ok = $false
                            Error = $Response.error
                            Raw = $Response
                        }
                    }
                }
            }
        }
    }
}