Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Deactivate/Disable Jira User using a Powershell script

Kevo
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 24, 2024

Hi Community,

we are using a Powershell script to offboard users from our enviroment when they leave the company (deactivate user and mailbox, revoke permissions, etc.) - The script runs locally on our Domain Controller.

We would like to include Jira in this process and automatically deactivate the relevant user in Jira. 

I've tried the following code, but get a (400) Bad Request as result:

# Define variables
$User = "admin"
$Pass = "password"
$Username = "usernameToBeDisabled"

# Convert credentials to Base64
$Base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User,$Pass)))

# Create headers
$headers = @{
    Authorization=("Basic {0}" -f $Base64AuthInfo)
    "Content-Type"="application/json"
}

# Set the body
$body = '{ "active": false }'

# Define the Jira API endpoint
$uri = "https://jira.companydomain.com/rest/api/2/user?username=$Username"

# Invoke the REST method
Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -Body $body

Error message:

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At C:\path\to\script\script.ps1:27 char:1
+ Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -Body $body
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 

I found this question regarding deactivate user with powershell but this doesn't really help (or I don't get it right, I don't have used Jira Server applications before).

Can someone give me a push in the right direction? 

 

Thank you very much and best regards

0 answers

Suggest an answer

Log in or Sign up to answer