Forums

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

How do you create a user with Powershell and the Rest API?

Matt Talaga
Contributor
July 5, 2018

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!

1 answer

0 votes
Boris MBOUMEHANG
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 17, 2019

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 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events