Skip to content

Post message with results to Slack #57

@MrPeanut12

Description

@MrPeanut12

I am starting to move all of my notifications to Slack and was wondering if this script could post the results to Slack, similar to how it sends an email. Another example is how Tautulli sends notifications to Slack.

I have an Incoming Webhook set up (via Sending messages using Incoming Webhooks). I am able to post from PowerShell with something like the code below (modified from Sending messages to Slack using PowerShell). However, I'm not sure how to modify this script in order to send success/failure messages with the appropriate data.

Example PowerShell script:

$uriSlack = "https://hooks.slack.com/services/<my-unique-url>"
$body = ConvertTo-Json @{
    text = "PlexBackup.ps1 complete."
}

try {
    Invoke-RestMethod -uri $uriSlack -Method Post -body $body -ContentType 'application/json' | Out-Null
} catch {
    Write-Error "Unable to post to Slack."
}

Slack has a convenient Block Kit Builder to show some of the components available.

Example JSON to post to Slack:

{
    "blocks": [
        {
            "type": "header",
            "text": {
                "type": "plain_text",
                "text": "\u2714\uFE0F Success",
                "emoji": false
            }
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "Plex backup script *C:\\Users\\User\\Documents\\PlexBackup.ps1* completed the `BACKUP -7ZIP (TEST)` operation on *PlexServer*. The backup folder *\\\\nas\\PlexBackup\\20220220112909* contains *1* objects and *0* GB of data. The script started at *02/20/2022 11:29:09* and ended at *02/20/2022 11:29:11* running for *00:00:02.593* (hr:min:sec.msec)."
            }
        }
    ]
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions