Public/New-SlackReminder.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
function New-SlackReminder
{
    <#
    .SYNOPSIS
        Creates a Slack reminder

    .DESCRIPTION
        Creates a Slack reminder

    .PARAMETER Token
        Token to use for the Slack API

        Default value is the value set by Set-PSSlackConfig

        This takes precedence over Uri

    .PARAMETER Proxy
        Proxy server to use

        Default value is the value set by Set-PSSlackConfig

    .PARAMETER Text
        The content of the reminder

    .PARAMETER Time
        When this reminder should happen. Must be passed in a valid datetime format.

    .PARAMETER User
        The user who will receive the reminder. If no user is specified,
        the reminder will go to user who created it.

        Note: This is not the user's name in Slack but rather the unique user identifier.

    .EXAMPLE
        # This is a simple example on how to create a reminder

        New-SlackReminder -Text "Do some things" -Time (Get-Date).AddMinutes(1)

        # Creates a reminder with the name "Do some things" set for 1 minute from now

    .LINK
        https://api.slack.com/methods/reminders.add

    .FUNCTIONALITY
        Slack
    #>


    param (

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$Token = $Script:PSSlack.Token,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$Proxy = $Script:PSSlack.Proxy,

        [Parameter(Mandatory = $true,
            Position = 0)]
        [String]$Text,

        [Parameter(Mandatory = $true,
            Position = 1)]
        [validatescript( {$_ -gt (Get-Date) -and $_ -le (Get-Date).AddYears(5)})]
        [datetime]$Time,

        [string]$User

    )
    begin
    {
        $ProxyParam = @{}
        if ($Proxy)
        {
            $ProxyParam.Proxy = $Proxy
        }
    }
    process
    {
        $body = @{ }

        switch ($psboundparameters.keys)
        {
            'text' {$body.text = $text}
            'time' {$body.time = [Math]::Floor([decimal](Get-Date($time).ToUniversalTime()-uformat "%s"))}
            'user' {$body.user = $User}
        }
    }
    end
    {
        Write-Verbose "Send-SlackApi -Body $body"
        $response = Send-SlackApi @ProxyParam -Method reminders.add -Body $body -Token $Token -ForceVerbose:$ForceVerbose

        Add-ObjectDetail -InputObject $response.reminder -TypeName 'PSSlack.Reminder'
    }
}