Forums

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

API Powershell examples

Steven Lees-Smith
Contributor
June 1, 2022

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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 1, 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. 

0 votes
Frederic Wolf
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.
June 12, 2022

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

Frederic Wolf
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.
June 13, 2022

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
Product Admin
TAGS
AUG Leaders

Atlassian Community Events