Functions/Get-SlackUser.ps1
<#
.SYNOPSIS This function returns the list of users in Slack. .DESCRIPTION This function returns the list of users in Slack. If the user name is specified, it only returns the user matching the name. The scope required to call this function is "users:read". #> function Get-SlackUser { [CmdletBinding(PositionalBinding=$false)] [OutputType([PSObject])] param( # The authentication token for Slack [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$token, # The name of the user to get [Parameter(Mandatory=$false)] [String]$userName ) # Prepare the API call parameters $invokeRestMethodParams = @{ Uri = "https://slack.com/api/users.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 users $users = $response.members if (!$users) { throw "Failed to retrieve users with the error message:`r`n$($response.error)." } # Only return the user that matches the name, if userName is specified if (![String]::IsNullOrWhiteSpace($userName)) { return ($users | Where-Object { $_.name -eq $userName -or $_.real_name -eq $userName }) } # Return all users return $users } |