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. #> 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, # Select the stream where the messages will be directed. [Parameter(Mandatory=$false)] [ValidateSet("Information", "Warning", "Error", "None")] [String]$outputStream = "Error" ) # 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)" } } # Try to get channels Write-Information "Retrieving channels in Slack." try { $response = Invoke-RestMethod @invokeRestMethodParams } catch { Write-OutputMessage "Exception occurred while retrieving channels in Slack.`r`n$($_.Exception.Message)" -OutputStream $outputStream -ReturnMessage:$false return $null } # Get the list of all channels and return false if null $channels = $response.channels if (!$channels) { Write-OutputMessage "Failed to retrieve channels with the error message:`r`n$($response.error)." -OutputStream $outputStream -ReturnMessage:$false return $null } # Return the channel that matches the name, if channelName is specified if (![String]::IsNullOrWhiteSpace($channelName)) { return ($channels | Where-Object { $_.name -eq $channelName }) } # Return all channels return $channels } |