PowerShell Invoke-RestMethod: Uploading new Attachment to Jira ticket

Currently I’m working to upload a file to a Jira ticket using PowerShell Invoke-RestMethod. Problem is after several days of googling and playing around with some code I haven’t gotten very far.

My powershell code is below. I get a 500 error every time. But when I don’t add the no-check token I get a 403 error. Has anyone been able to get this to work in PowerShel?

All of the answers I have found are about least two years old.

$NewHeaders=$headers
$NewHeaders.Add(“X-Atlassian-Token”, “no-check”)
Invoke-RestMethod -Uri “$($URIPath)/issue/$($jiraTicket)/attachments” -Method POST -InFile $filepath -ContentType “multipart/form-data” -Headers $NewHeaders

What the variable “$headers” contains is below, which works when using it to create tickets, post comments, etc.:
Name Value


Authorization Basic exampe_token

I was thinking of building a curl command and getting it to work, then somehow translating it to PowerShell. But the problem is I’m not sure I’d run into the same issue I’m having now. :slight_smile:

From my research, I’ve tried the following, all of which didn’t work:
http://techqa.info/programming/question/31685034/how-do-i-upload-an-attachment-to-a-jira-issue-via-powershell

Read through the following, also was unable to code a solution based on them in PowerShell:

Resolved my own issue with a few edits using the code show at the below link. Thanks!

Figured post the code I edited to get it to work based on the link I posted above.

function Upload-JiraCSV($jiraTicket, $filepath)
{
    $wc = new-object System.Net.WebClient
    $wc.Headers.Add("Authorization", $headers.Authorization)
    $wc.Headers.Add("X-Atlassian-Token", "nocheck") 
    $wc.UploadFile("$URIPath/issue/$jiraTicket/attachments", $filepath)
}

I am struggling how to get my credentials into the authorization header in a way that Confluence or Jira can process. Can you give me an example of what the powershell code would be?