Advertisement
bfikse

Bulk Remove JIRA OnDemand Users

Sep 24th, 2015
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $usernamesFile = '.\usernames.csv'
  2. $base = 'https://YOURDOMAIN.jira.com/rest/api/2'
  3.  
  4. function ConvertTo-Base64($string)
  5. {
  6.     $bytes=[System.Text.Encoding]::UTF8.GetBytes($string);
  7.     $encoded=[System.Convert]::ToBase64String($bytes);
  8.     return $encoded;
  9. }
  10.  
  11. function Get-HttpBasicHeader
  12. {
  13.     Param([Parameter(Mandatory=$false,
  14.                     ValueFromPipeline=$true,
  15.                     ValueFromPipelineByPropertyName=$true)]
  16.            $credentials=@{}
  17.  
  18.     )
  19.     $headers=@{}
  20.     if(!$credentials.username)
  21.     {
  22.         $credentials.username=Read-Host 'Input Username'
  23.     }
  24.  
  25.     if(!$credentials.password)
  26.     {
  27.         $credentials.password=Read-Host 'Input Password'
  28.     }
  29.  
  30.     $b64=ConvertTo-Base64 ($credentials.username+':'+$credentials.password)
  31.     $headers["Authorization"]='Basic ' + $b64
  32.     return $headers
  33. }
  34.  
  35. function Get-User
  36. {
  37.     Param( [Parameter(Mandatory=$true)]$header, [Parameter(Mandatory=$true)]$username)
  38.     Invoke-RestMethod -URI ($base+"/user?username="+$username+"&expand=groups") -Headers $header -ContentType 'application/json' -Method Get -Verbose
  39. }
  40.  
  41. function Remove-UserFromGroup
  42. {
  43.     [CmdletBinding()]
  44.     Param
  45.     (
  46.         [Parameter(Mandatory=$true)]
  47.         $Header,
  48.         $User,
  49.         $Group
  50.     )
  51.     Invoke-RestMethod -URI ($base+"/group/user?groupname="+$Group+"&username="+$User) -Headers $Header -ContentType 'application/json' -Method Delete
  52. }
  53.  
  54. $usernames = (Get-Content $usernamesFile).Split(",")
  55. $header = Get-HttpBasicHeader
  56.  
  57. Foreach($user in $usernames)
  58. {
  59.     $userinfo = Get-User $header $user
  60.     Foreach($group in $userinfo.groups.items)
  61.     {
  62.         $group=$group.name.trim()
  63.         $user=$user.trim()
  64.         Remove-UserFromGroup $header $user $group -Verbose
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement