Forums

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

Powershell Script to create user and add to servicedesk suddenly not working

Natalie_P_
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!
July 18, 2018

Hello,

last year I created a PS script, that took care of automatically creating users and adding them to our servicedesk, we use a special user creation account for this, the credentials are locally saved in a text file. It all worked fine, however the script doesn't seem to work anymore, did the JIRA API change?

I get following error message: Invoke-Rest-Method: The remote server returned an error (401) Unauthorized at response = Invoke-Rest-Method -Uri...etc

I checked and our user creation account still has all the permissions to create users, I can manually create them and the log shows that the user also logs in normally through the script.

Hopefully somebody can help with my problem!

Here's the code:

$jiraCredentials = Get-Content -Raw -Path "C:\PowerShellScripts\New-AdUser\credentials.json" |ConvertFrom-Json

$bytes = [System.Text.Encoding]::ASCII.GetBytes("${$jiraCredentials.username}:${$jiraCredentials.password}")
$base64 = [System.Convert]::ToBase64String($bytes)

$basicAuthValue = "Basic $base64"

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.add("Authorization", $basicAuthValue)
$headers.add("X-Experimentalapi", "opt-in")
$headers.add("Content-Type", "application/json")
#$headers.add("X-Atlassian-Token", "nocheck")

$JSON = @"
{
"fullName": "$emailAddressClean",
"email": "$emailAddressClean"
}
"@

$response = Invoke-RestMethod -Uri https://jira.dilax.com/rest/servicedeskapi/customer -Method POST -Body $JSON -ContentType "application/json" -Headers $headers

#add customer to servicedesk
$JSON2 = @"
{
"usernames":[
"$emailAddressClean"
]
}
"@

$response2 = Invoke-RestMethod -Uri https://jira.dilax.com/rest/servicedeskapi/servicedesk/9/customer -Method POST -Body $JSON2 -ContentType "application/json" -Headers $headers

 

3 answers

1 accepted

0 votes
Answer accepted
Natalie_P_
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!
July 19, 2018

managed to fix it because the log in credentials didn't get transmitted correctly:

 

$jiraCredentials = Get-Content -Raw -Path "C:\PowerShellScripts\New-AdUser\credentials.json" |ConvertFrom-Json
$user = $jiraCredentials.username
$pass = $jiraCredentials.password
$pair = "${user}:${pass}"

$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
$basicAuthValue = "Basic $base64"

0 votes
Srecko Anzic
Contributor
August 3, 2020

Is this a code for JIRA server or JIRA cloud instance?

0 votes
Danyal Iqbal
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

Did you upgrade your jira instance in the meanwhile?

Check if there is a captcha on the user account and disable it.

Make sure that the username and pasword variables are correctly filled.

Print the request (header+body) to shell and check that it is not malformed.

Suggest an answer

Log in or Sign up to answer