Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $usernamesFile = '.\usernames.csv'
- $base = 'https://YOURDOMAIN.jira.com/rest/api/2'
- function ConvertTo-Base64($string)
- {
- $bytes=[System.Text.Encoding]::UTF8.GetBytes($string);
- $encoded=[System.Convert]::ToBase64String($bytes);
- return $encoded;
- }
- function Get-HttpBasicHeader
- {
- Param([Parameter(Mandatory=$false,
- ValueFromPipeline=$true,
- ValueFromPipelineByPropertyName=$true)]
- $credentials=@{}
- )
- $headers=@{}
- if(!$credentials.username)
- {
- $credentials.username=Read-Host 'Input Username'
- }
- if(!$credentials.password)
- {
- $credentials.password=Read-Host 'Input Password'
- }
- $b64=ConvertTo-Base64 ($credentials.username+':'+$credentials.password)
- $headers["Authorization"]='Basic ' + $b64
- return $headers
- }
- function Get-User
- {
- Param( [Parameter(Mandatory=$true)]$header, [Parameter(Mandatory=$true)]$username)
- Invoke-RestMethod -URI ($base+"/user?username="+$username+"&expand=groups") -Headers $header -ContentType 'application/json' -Method Get -Verbose
- }
- function Remove-UserFromGroup
- {
- [CmdletBinding()]
- Param
- (
- [Parameter(Mandatory=$true)]
- $Header,
- $User,
- $Group
- )
- Invoke-RestMethod -URI ($base+"/group/user?groupname="+$Group+"&username="+$User) -Headers $Header -ContentType 'application/json' -Method Delete
- }
- $usernames = (Get-Content $usernamesFile).Split(",")
- $header = Get-HttpBasicHeader
- Foreach($user in $usernames)
- {
- $userinfo = Get-User $header $user
- Foreach($group in $userinfo.groups.items)
- {
- $group=$group.name.trim()
- $user=$user.trim()
- Remove-UserFromGroup $header $user $group -Verbose
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement