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.

PowerShell >= Screwdriver

“Always use the right tool!” I learned this early on in boy scouts, along with, “Work Smarter, not Harder!”.

Scripting and Automation are becoming the norm in IT. Every year it seems like I am writing 150% more scripts than I did the year before. As an engineer and not a programmer, it was somewhat frustrating for a long time. I was using phrases like, “No, we will need to contract someone”, “Does the dev team have time for that?”, “I am not a programmer” and “I don’t code!”. I hate saying ‘no’, when it comes to technology. I am a creative person, I like finding easier ways of doing things, making things more automated, or efficient. As long as I didn’t have to code to do it. I prided myself on finding these creative niches.