Public/Slack/Send-SlackMessage.ps1
function Send-SlackMessage { [CmdletBinding(PositionalBinding = $true)] param ( [Parameter(Mandatory = $false)][string]$Url = 'https://slack.com/api/chat.postMessage', [Parameter(Mandatory = $true)][string]$ChannelId, [Parameter(Mandatory = $false)][string]$ThreadTs, [Parameter(Mandatory = $true)][string]$MessageText, [Parameter(Mandatory = $true)][string]$Token ) try { $body = @{ type = 'mrkdwn' channel = $ChannelId response_type = "ephemeral" text = $MessageText } if ($PSBoundParameters.Keys -contains 'ThreadTs') { $body += @{ thread_ts = $ThreadTs } } $jsonBody = $body | ConvertTo-Json -Compress $headers = @{ Authorization = "Bearer $Token" } Write-Log "Sedning Slack Message: POST: $Url -- $jsonBody" $msgResp = Invoke-RestMethod -Uri $Url -Method Post -ContentType 'application/json' -Body $jsonBody -Headers $headers Write-Log "Slack message Response: $($msgResp | ConvertTo-Json -Depth 10 -Compress)" return $msgResp } catch { Write-Log "Error sending Slack message" -LogType error -ErrorObject $_ } } |