Skip to content

Instantly share code, notes, and snippets.

@zakes-it
Created August 4, 2016 22:52
Show Gist options
  • Save zakes-it/d4b6b062febcc323087c805c20f01bdd to your computer and use it in GitHub Desktop.
Save zakes-it/d4b6b062febcc323087c805c20f01bdd to your computer and use it in GitHub Desktop.
Disable Slack account matching a username
Function Disable-SlackAccount {
[CmdletBinding()]
Param(
[String]$user,
[String]$org,
[String]$token
)
$SlackUsers = Invoke-RestMethod -Uri $( "https://" +$org + ".slack.com/api/users.list?token=" + $token )
if ( ! $SlackUsers.ok ) {
Write-Error ( "Error getting user list from Slack: " + $SlackUsers.error )
} else {
$UserSlackID = ($SlackUsers.members | select id, profile | ?{ $_.profile.email -Match "^$user@" }).id
if ( ! $UserSlackID ) {
Write-Host -f red ( "Failed to find $user on Slack." )
} else {
$RequestBody = @{
user = $UserSlackID;
token = $token;
set_active = "true";
_attempts = "1"
}
$DisableRequest = Invoke-RestMethod -Uri $("https://" + $org + ".slack.com/api/users.admin.setInactive") -Body $RequestBody
if ( ! $DisableRequest.ok ) {
Write-Error ( "Failed to disable Slack account! Received the following error from Slack: " + $DisableRequest.error )
} else {
Write-Host -f green "Slack account has been disabled."
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment