Functions/New-SlackChannel.ps1
<#
.SYNOPSIS This function creates a channel in Slack. .DESCRIPTION This function creates a channel in Slack. The scope required to call this function is "channels:write". #> function New-SlackChannel { [CmdletBinding(PositionalBinding=$false)] [OutputType([Bool])] param( # The authentication token for Slack [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$token, # The name of the new channel [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$channelName ) # Prepare the API call parameters $invokeRestMethodParams = @{ Uri = "https://slack.com/api/channels.create" Method = "POST" Headers = @{ "Content-Type" = "application/json" Authorization = "Bearer $($token)" } Body = @{ name = "$($channelName)" validate = $true } | ConvertTo-Json } # Invoke the call $response = Invoke-RestMethod @invokeRestMethodParams # Verify that the creation is successful if ($response.ok) { return $true } else { throw "Failed to create channel '$($channelName)' with the error message:`r`n$($response.error)." } } |