I am trying to bulk create users in Jira Service Desk using the Rest API. But for the life of my I cannot get it to work. I've been able to get the Service Desk Instance, Organization, and a list of current users, but I do not know what I'm missing for creating the user.
I either receive a "415 Unsupported Media Type, or a 500 Internal Server Error.
The code below returns a 415 - Unsupported Media Type. When I add a ContentType of Json to my Invoke-RestMethod, I receive the 500 Internal Server Error. I know I have to be close, can anyone suggest how to create users?
You should be able to take the code, and change the $target, $username, $password, $projectKeys, $OrgID and it should run in your environment. The last line is where I am trying to create the user.
Any help would be appreciated.
Below is my code:
[string] $target = "https://mysite.atlassian.net"
[string] $username = "user@domain.com"
[string] $password = "secret-password"
[string] $projectKey = "HD";
[string] $OrgName = "MyOrg"
function set-Headers
{
$basicAuth = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($Username):$Password"))
$headers = @{
Authorization = $basicAuth
}
return $headers
}
function get-RestData
{
param([Parameter(Mandatory=$True)] $headers,
[Parameter(Mandatory=$True)]$requestURI)
return Invoke-RestMethod -uri $requestURI -Method Get -Headers $headers
}
function get-Jira-ServiceDesks
{
$requestURI = "$target/rest/servicedeskapi/servicedesk/"
$headers = set-Headers
$response=get-RestData -headers $headers -requestURI $requestURI
return $response.values
}
function get-Jira-organizations
{
$headers = set-Headers
$requestURI = "$target/rest/servicedeskapi/servicedesk/$ServiceDeskID/organization"
$response=get-RestData -headers $headers -requestURI $requestURI
return $response.values
}
Function Add-Jira-User
{
param([Parameter(Mandatory=$True)] $emailAddress,
[Parameter(Mandatory=$True)]$displayName)
$headers = set-Headers
$requestURI = "$target/rest/servicedeskapi/organization/$OrgID/user"
$Data= @{
"X-ExperimentalApi" = "true"
emailAddress = '"' + $emailAddress + '"'
username = '"' + $emailAddress + '"'
name='"' + $displayname + '"'
}
#$json = ConvertTo-Json -InputObject $Data
$response=Invoke-RestMethod -uri $requestURI -Method POST -Headers $headers -Body $json #-ContentType "application/json"
#$response.status
}
Function Get-Jira-Users
{
$headers = set-Headers
$requestURI = "$target/rest/servicedeskapi/organization/$OrgID/user"
$response=get-RestData -headers $headers -requestURI $requestURI
return $response.values
}
#Get the ServiceDesk ID
$ServiceDeskList=get-Jira-ServiceDesks
$ServiceDeskID = ($ServiceDeskList | where {$_.projectKey -eq $projectKey}).Id
#Get the OrgID
$OrgList = get-Jira-organizations
$OrgID = ($OrgList | where {$_.name -eq $OrgName}).Id
$OrgID
$users=Get-Jira-Users
#$users
Add-Jira-User -emailAddress "userA@domain.com" -displayName "User A"
Thanks Matt!
Hello Matt,
I am not sure if you were able to solve your issue. If not, below is a discussion regarding this, where Natalie wrote a code to create an Atlassian user and add access to Service Desk: https://community.atlassian.com/t5/Jira-questions/Powershell-Script-to-create-user-and-add-to-servicedesk-suddenly/qaq-p/847713.
Hope this helps.
Thanks!
Boris
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.