Functional PowerShell with Slack

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.

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
}

Read More for PowerShell v2 compatibility.

My new favorite PowerShell Tool: Slack

I have been using Slack for several years now. It is an excellent collaboration tool for teams. At its base, it is great for communicating with teammates. At another level it becomes an IT monitoring hub for alerts, metrics and tools. I have done customization with many different products, utilizing their “bots” to do basic tasks like project management and scheduling. I have configured Solarwinds, a complete infrastructure monitoring suite, to push alerts to multiple channels within Slack. This is a great benefit in an age where, as an IT professional, we get enough spam from co-workers, company announcements and vendors. Alternative notification is a must to stay agile.