Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,298,554
Community Members
 
Community Events
165
Community Groups

API Powershell examples

Hi there,

I am starting to delve into the API for JSM but looking on the developer website there doesn't seem to be any examples set out in Powershell, this hurts due to me pretty much only knowing scripting in Powershell.

Does anyone know of any sites that give some examples for how to best leverage API calls (of all types) using Powershell?

Thanks all.

2 answers

1 vote
Phill Fox Community Leader Jun 01, 2022

You might like to have a look at https://atlassianps.org/docs/JiraPS/ which is a community site that explores options around Jira and PowerShell. 

Hi Steven,

I'm working on creating requests via API through Powershell as well. I'm slowly getting it to work, while I' m convinced that it can be done more easily.

It would be nice if there was already a ready made Powershell module, but that is not the case. The proposed JiraPS is basically suitable for creating ISSUES, but not REQUESTS. It is made for normal Jira projects, but not for JSM projects.

There is a separate API for JSM projects, which is designed for this.
You can find the documentation here: https://developer.atlassian.com/cloud/jira/service-desk/rest/intro/

Now that you also asked for some examples, I'll post my code here.

Use case: Creation of a request, during the automatic user creation, from the HR database system.

The Request Type is "Neues Konto anlegen" (Translated it should be something like "Create New Account")

# Create encoded CREDS
$user = 'YOUR MAIL ADDRESS'
$pass = 'YOUR API TOKEN'

$pair = "$($user):$($pass)"

$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))


#Create Auth Headers for Invoke-RestMethod
#in my case encoded Creds is loaded from Keepass with Module PoShKeePass and already Base64encoded
# Please dont use plain text credentials in scripts, due to security reasons. if you have a Read-Host method to get the pass thats ok.
$Headers = @{
Authorization = "Basic $encodedCred"
Accept = "application/json"
}
$Body = @"
{
"requestParticipants": [],
"serviceDeskId": "4",
"requestTypeId": "51",
"requestFieldValues": {
"summary": "Request JSD help via REST",
"description": "I need a new *mouse* for my Mac",
"components" : [{"name": "Active Directory"}]
}
}
"@

My Problem is, the above Body. I would like to work with PS Objects and use ConvertTo-JSON once, but this is not working properly.

So i have something like a mix with creating the damn Body.

Current problems:

  • components: I would like to use an array and convert it to an an Object wih key/value pair. That works fine. If i add multiple components, the ConvertTo-Json also works fine, but if there is only one entry i do not get the right JSON format.
  • requestParticipants: Same like components
  • descriptions: Would like to use a multi line description, but thats not working as well.
Invoke-RestMEthod -Method Post -Uri "https://YOUR-BASE-DOMAIN/rest/servicedeskapi/request" -Headers $Headers -Body $Body -ContentType 'application/json'

I hope I could help you a little with this.

if you want, we can also exchange ideas via private channels like MS teams

Hi Steven,

i am currently taking a deeper look into the JiraPS Module to find some generic code for the different Attribute Types.

I think there is the root cause, that simply build the Objects wrong, so the resulting JSON is wrong, too.

Additonally have a look here: https://community.atlassian.com/t5/Jira-Service-Management/Is-anybody-working-on-a-Powershell-Module-for-Jira-Service/qaq-p/2052980#U2053806

Another Post of me, which is looking for a JSM Powershell Module.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Site Admin
TAGS
Community showcase
Published in Jira Service Management

Coming Soon: Insight Changing to Assets

The 2020 acquisition of Mindville added powerful asset and configuration management capabilities to Jira Service Management in the form of Insight. Following the completion of that integration, custo...

294 views 2 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you