Functions/Get-SlackChannel.ps1
<#
.SYNOPSIS This function returns the list of channels in Slack. .DESCRIPTION This function returns the list of channels in Slack. If the channel name is specified, it only returns the channel matching the name. The scope required to call this function is "channels:read". #> function Get-SlackChannel { [CmdletBinding(PositionalBinding=$false)] [OutputType([PSObject])] param( # The authentication token for Slack [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$token, # The name of the channel to get [Parameter(Mandatory=$false)] [ValidateNotNullOrEmpty()] [String]$channelName ) # Prepare the API call parameters $invokeRestMethodParams = @{ Uri = "https://slack.com/api/channels.list" Method = "GET" Headers = @{ "Content-Type" = "application/x-www-form-urlencoded" Authorization = "Bearer $($token)" } } # Invoke the call $response = Invoke-RestMethod @invokeRestMethodParams # Get the list of all channels $channels = $response.channels if (!$channels) { throw "Failed to retrieve channels with the error message:`r`n$($response.error)." } # Return the channel that matches the name, if the channel name is specified if (![String]::IsNullOrWhiteSpace($channelName)) { return ($channels | Where-Object { $_.name -eq $channelName }) } # Return all channels return $channels } |