Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
90 lines (82 sloc) 4.27 KB
Function Set-SlackNotification {
.DESCRIPTION Enable or Disable @channel and @here notifications for Slack Channel
.NOTES Author: William Lam
.NOTES Site:
.NOTES Reference:
.PARAMETER SlackAccessToken
This is your OAuth Access Token that you will need to either provide or generate as part of the OAuth login workflow
.PARAMETER SlackBrowserAccessToken
This is the OAuth Access Token which will be retrieved from the browser and is scoped to access the private Slack API
.PARAMETER SlackBrowserSessionCookie
This is the internal Slack cookie that is required to be able to access the private Slack API
.PARAMETER Operation
Enable or Disable
.PARAMETER ExcludeChannels
List of Slack Channels to ignore
$SlackAccessToken = "xxx"
$SlackBrowserAccessToken = "xxx"
$SlackBrowserSessionCookie = "xxx"
$ExcludeChannels = @(
Set-SlackNotification -SlackAccessToken $SlackAccessToken -SlackBrowserAccessToken $SlackBrowserAccessToken -SlackBrowserSessionCookie $SlackBrowserSessionCookie -Operation Disable -ExcludeChannels $ExcludeChannels
$headers = @{
"Accept" = "application/x-www-form-urlencoded";
# Retrieve all Slack channels user is part of
$conversation_results = Invoke-WebRequest -Uri "" -Method GET -Headers $headers -Body @{"token"=$SlackAccessToken;"limit"=1000;"exclude_archived"="true";"types"="public_channel,private_channel";}
$slack_channels = ($conversation_results.Content|ConvertFrom-Json).channels | Select id,name
# Determine the notification operation
if($Operation -eq "Enable") {
Write-Host "Enabling @channel and @here notification for the following channels"
$notification_value = "false"
} else {
Write-Host "Disabling @channel and @here notification for the following channels"
$notification_value = "true"
# Construct the required cookie to call Private Slack API
$websession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$cookie = New-Object System.Net.Cookie
$cookie.Name = "d"
$cookie.Value = $SlackBrowserSessionCookie
$cookie.Domain = ""
foreach ($slack_channel in $slack_channels) {
if( ($ExcludeChannels.toLower()) -NotContains ($ ) {
$body = @{
"token" = $SlackBrowserAccessToken;
"name" = "suppress_at_channel";
"value" = $notification_value;
"channel_id" = $($;
"global" = "false";
"sync" = "false";
Write-Host "`tUpdating $($ channel ..."
try {
$requests = Invoke-WebRequest -Uri "" -Method Post -Headers $headers -Body $body -WebSession $websession
if(-not ($requests.Content|ConvertFrom-Json).ok) {
Write-Error "Failed to update $($ channel ($($"
Write-Error "`n($requests.Content)`n"
} catch {
Write-Error "Error in updating $($ channel ($($"
Write-Error "`n($_.Exception.Message)`n"
Write-Host "Successfully $($Operation)d @channel and @here notification preferences for $($slack_channels.count) Slack Channels!"
You can’t perform that action at this time.