Use PowerShell to post to a Slack channel with extra args

on

You need to create an application in Slack with incoming webhooks.

Enable the application and confirm your identity.

Copy that webhook.

Invoke the script with system variables to post extra content for alerts, automation, escalations, etc.

$message = "`n" + $args

Set-StrictMode -Version Latest

$payload = @{
  "channel" = "#alerts";
  "icon_emoji" = ":inbox_tray:";
  "text" = $(Write-output "$message");
  "username" = "User";
}

Invoke-WebRequest `
  -Uri " https://hooks.slack.com/services/(YOURHOOK)" `
  -Method "POST" `
-Body (ConvertTo-Json -Compress -InputObject $payload)

 

 

Was this helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *