Skip to content

Instantly share code, notes, and snippets.

@zakes-it
Created August 4, 2016 22:43
Show Gist options
  • Save zakes-it/1d9e2dbf1f47a3153c110d690da74f38 to your computer and use it in GitHub Desktop.
Save zakes-it/1d9e2dbf1f47a3153c110d690da74f38 to your computer and use it in GitHub Desktop.
Deactivate a Trello account matching an Active Directory user
# Get-oAuth2AccessToken and Get-TrelloToken functions copied from
# https://github.com/danroot/PowerTrello
Function Remove-TrelloUser {
[CmdletBinding()]
Param(
# Active Directory user hash
[Parameter(Mandatory = $true)] $user,
# Get your key by signing in Trello and visit https://trello.com/app-key
[Parameter(Mandatory = $true)] [string] $appKey,
# Trello organization ID
[Parameter(Mandatory = $true)] [string] $OrgId
)
Function Get-oAuth2AccessToken {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)] [string] $AuthUrl,
[int] $SleepInterval = 2
)
# Create the Internet Explorer object and navigate to the constructed authorization URL
$IE = New-Object -ComObject InternetExplorer.Application
$ie.width = 500
$ie.height = 700
$IE.Navigate($AuthUrl)
$IE.Visible = $true
# Sleep the script for $X seconds until callback URL has been reached
# NOTE: If user cancels authorization, this condition will not be satisfied
while ($IE.LocationUrl -notmatch 'token=') {
if ($IE.LocationUrl -match 'error=') {
[Void]($IE.LocationUrl -match '=([\w\.]+)');
throw $Matches[1];
}
Write-Debug -Message ('Sleeping {0} seconds for access URL' -f $SleepInterval);
Start-Sleep -Seconds $SleepInterval;
}
# Parse the access token from the callback URL and exit Internet Explorer
Write-Debug -Message ('Callback URL is: {0}' -f $IE.LocationUrl);
[Void]($IE.LocationUrl -match '=([\w\.]+)');
$AccessToken = $Matches[1];
$IE.Quit();
# Write the access token to the pipeline inside of a HashTable (in case we want to return other properties later)
Write-Debug -Message ('Access token is: {0}' -f $AccessToken);
return $AccessToken
}
Function Get-TrelloToken {
param(
$appKey,
$AppName,
$Expiration="30days",
$Scope="read"
)
$uri = "https://trello.com/1/authorize?key=" + $appKey + "&name=" + $AppName + "&expiration=" + $Expiration + "&scope=" + $Scope + "&response_type=token&callback_method=fragment&return_url=https://trello.com?"
$token = Get-oAuth2AccessToken -AuthUrl $uri
return @{Token=$token;AccessKey=$appKey}
}
Function Get-MatchingTrelloUsers {
Param(
$Token,
$orgID,
$user
)
$RestParam = @{
'token' = $Token.Token[2];
'key' = $Token.AccessKey
}
$uri = "https://api.trello.com/1/organizations/$orgID/members"
try {
$result = Invoke-RestMethod -Uri $uri -Body $RestParam -Method Get
} catch {
Write-Host "failed to get user list from Trello"
$host.enternestedprompt()
}
$searchTerms = {
($_.fullName -like "*$($user.GivenName)*") `
-or ($_.fullName -like "*$($user.Surname)*") `
-or ($_.fullName -like "*$($user.SamAccountName)*") `
-or ($_.username -like "*$($user.GivenName)*") `
-or ($_.fullName -like "*$($user.Surname)*") `
-or ($_.fullName -like "*$($user.SamAccountName)*")
}
$matchingUsers = $result | Where-Object $searchTerms
Return $matchingUsers
}
Function Remove-UserFromTrelloOrg {
param(
$trelloUser,
$orgId,
$Token
)
$caption = "Confirm Removal"
$message = "Do you really want to deactivate `"" + $trelloUser.fullName + "`" from the trello org?"
$choices = [System.Management.Automation.Host.ChoiceDescription[]] `
@("&Don't remove", "&Remove")
[int]$defaultChoice = 0
$choiceRTN = $host.ui.PromptForChoice($caption,$message, $choices,$defaultChoice)
if ( $choiceRTN -eq 1 ) {
$uri = "https://api.trello.com/1/organizations/" + $orgID + "/members/" + $trelloUser.id + "/deactivated?key=" `
+ $Token.AccessKey + "&token=" + $Token.Token[2] + "&value=true&idMember=" + $trelloUser.id
try {
$result = Invoke-RestMethod -Uri $uri -Body $RestParam -Method Put
if ( $result._value ) {
Write-Host -f red ("Error deactivating account. Trello returned: " + $result._value)
} else {
Write-Host -f green "Deactivated the Trello account successfully."
}
} catch {
Write-Host -f red "Failed to deactivate the Trello account."
}
}
}
$Token = Get-TrelloToken -appKey $appKey -AppName 'IT - User Termination Script' `
-Expiration '1hour' -Scope 'read,write'
if ( $Token.Token[2] ) {
$matchingUsers = Get-MatchingTrelloUsers -Token $Token -user $user -orgID $OrgID
if ( $matchingUsers ) {
$selectedUser = $matchingUsers | Out-GridView -Title "Select account to deactivate." -PassThru
} else {
Write-Host -f green -noNewLine ( "No Trello account found for " + $user.SamAccountName + "." )
Write-Host " Trello user search capabilities are somewhat limited so if you believe the user has an account, log in to the trello webpage and disable or deactivate the account from there."
}
if ( ($selectedUser | measure).count -gt 1 ) {
Write-Error "Only one user account can be selected for removal."
} elseif ( ($selectedUser | measure).count -eq 1 ) {
Remove-UserFromTrelloOrg -trelloUser $selectedUser -orgId $OrgID -Token $Token
} else {
Write-Host "No matching Trello accounts found. Trello user search capabilities are somewhat limited so if you believe the user has an account, log in to the trello webpage and disable or deactivate the account from there."
}
} else {
throw "Could not retrieve Trello token."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment