I have written this function out a few times. I have made it extremely simple and then went back and added some complexity. I have also run into a situation where I needed to write a tool that was widely used among users with Windows 7-10. So, I needed the tool to be compatible with PowerShell v2. This first function will not work with PowerShell v2 for a few different reasons. I will, however, post both functions.
[powershell]
function Send-ToSlack($channel, $message, $user, $heading=$null, $webhook)
{
#Slack doesnt accept Arrays, so if it is an array, this converts it to a string
if(($message -is [array]) -eq "True")
{
[System.Collections.ArrayList]$ArrayList = $message
#Dont use the heading unless you are using an array as an input
if($heading -ne $null)
{
$ArrayList.Insert(0,$heading)
$ArrayList[0] = "*" + $ArrayList[0] + "*"
$ArrayList[0] += "`n>>>"
}
$message = $ArrayList | Out-String
}
#Bundle of information to send to slack
$payload = @{
channel = $channel
text = $message
username = $user
}
#Send Message via web request to slack
Invoke-WebRequest -Body (ConvertTo-Json -Compress -InputObject $payload) -Method Post -Uri $webhook | Out-Null
}
[/powershell]
Read More for PowerShell v2 compatibility.