Post Issue to Jira Using PowerShell 2.0

Some time ago I was looking for a skeleton of a PowerShell 2.0 script that would allow me to post an issue to Jira. Didn’t find one, so had to come up with my own.

# Change those strings to whatever is specific to environment
[string] $SERVER = “localhost:8080”
[string] $USER = “powershelluser”
[string] $PWD = “password”
# Get Project ID from Administration -> Projects, e.g. 
[string] $PROJECTID = “10123”
# Specify fields that need to be posted
# NOTE: You must provide all required fields
# Example below submits 4 fields: 
#    assignee – automatically assigned to default user of the project
#    issuetype – selected type 3 = task
#    summary and description, which are read from elsewhere
[string] $FIELDS = “issuetype=3&priority=3&assignee=-1&summary={0}&description={1}”

# Those constants normally do not change
[string] $LOGIN_URL = http://{0}/login.jsp?os_username={1}&os_password={2}&os_destination=/secure/”
[string] $CREATE_URL = http://{0}/secure/CreateIssueDetails.jspa?pid={1}&{2}&Create=Create&atl_token={3}”
[string] $TOKEN_PREFIX = ‘<meta id=”atlassian-token” name=”atlassian-token” content=”‘
[string] $TOKEN_SUFFIX = ‘”>’

function loginToJira($server, $user, $pwd) {

[string] $postUrl = $url -f  $user, $pwd

[System.Net.WebRequest] $webRequest = [System.Net.WebRequest]::Create($postUrl)
$webRequest.CookieContainer = New-object System.Net.CookieContainer
[System.Net.WebResponse] $webResponse = $webRequest.getResponse()
# TO DO: Response can be verified here
# One of the ways to check if request was successful is to read $webResponse.StatusDescription, e.g.
Write-Host “status = “ $webResponse.StatusDescription;

$resStream = $webResponse.GetResponseStream()
[System.IO.StreamReader] $streamReader = New-Object System.IO.StreamReader -argumentList $resStream
$res = $streamReader.ReadToEnd()
$start = $res.IndexOf($TOKEN_PREFIX, 0+ $TOKEN_PREFIX.Length
$end = $res.IndexOf($TOKEN_SUFFIX, $start)
$token = $res.substring($start, $end  $start)

return $webRequest.CookieContainer, $token

function createJiraIssue($server, $session, $token, $project_id, $summary, $description) {

[string] $formattedFields = $FIELDS -f [System.Web.HttpUtility]::UrlEncode($summary), [System.Web.HttpUtility]::UrlEncode($description)
[string] $postUrl = $CREATE_URL -f $server, $project_id, $formattedFields, $token

[System.Net.WebRequest]$webRequest = [System.Net.WebRequest]::Create($postUrl);
$webRequest.CookieContainer = $session
[System.Net.WebResponse]$webResponse = $webRequest.getResponse()
# TO DO: Response can be verified here (similar to login)

$resStream = $webResponse.GetResponseStream()
[System.IO.StreamReader]$streamReader = New-Object System.IO.StreamReader -argumentList $resStream
$res = $streamReader.ReadToEnd()
# TO DO: Here it can be verified if the issue was posted successully, by looking into $res
# If issue is not posted, Jira will return HTML similar to the page seeing by the user who tried to post issue with missing / problematic fields

if($args.count -ne 3) {
Add-Type -AssemblyName System.Web
$session, $token = loginToJira $SERVER $USER $PWD
$testsummary = “This is a test – issue summary”
$testdscription = “This is a test.`n`nIssue description usually contains several lines of text”
createJiraIssue $session $token $PROJECTID $testsummary $testdscription

Post Issue to Jira Using PowerShell 2.0

3 thoughts on “Post Issue to Jira Using PowerShell 2.0

    1. Of course it doesn’t! this was posted 3 years ago for Jira 3.x. Thankfully products change, and nowadays not only Jira has the excellent set of Jira REST APIs, which is what should be used. But also PowerShell is long gone from version 2. Its v.4 has for instance Invoke-RestMethod.
      So if I had to create the same script today, I’d use those two.

  1. Artem says:

    This is working script, if anybody needs it:

    [String] $target = “http://jirahost:2990”;
    [String] $username = “username”
    [String] $password = “password”
    [String] $projectKey = “TP”;
    [String] $issueType = “Task”;
    [String] $summary = “summary”;
    [String] $description = “description”
    [String] $priority = “2”

    [String] $body = ‘{“fields”:{“project”:{“key”:”‘+$projectKey+'”},”issuetype”:{“name”: “‘+$issueType+'”},”summary”:”‘+$summary+'”,”description”:”‘+$description+'”, “priority”:{“id”:”‘+$priority+'”}}}’;

    function ConvertTo-Base64($string) {
    $bytes = [System.Text.Encoding]::UTF8.GetBytes($string);
    $encoded = [System.Convert]::ToBase64String($bytes);
    return $encoded;

    try {

    $b64 = ConvertTo-Base64($username + “:” + $password);
    $auth = “Basic ” + $b64;

    $webRequest = [System.Net.WebRequest]::Create($target+”/jira/rest/api/2/issue/”)
    $webRequest.ContentType = “application/json”
    $BodyStr = [System.Text.Encoding]::UTF8.GetBytes($body)
    $webrequest.ContentLength = $BodyStr.Length
    $webRequest.ServicePoint.Expect100Continue = $false
    $webRequest.Headers.Add(“Authorization”, $auth);
    $webRequest.PreAuthenticate = $true
    $webRequest.Method = “POST”
    $requestStream = $webRequest.GetRequestStream()
    $requestStream.Write($BodyStr, 0, $BodyStr.length)
    [System.Net.WebResponse] $resp = $webRequest.GetResponse()

    $rs = $resp.GetResponseStream()
    [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs
    [string] $results = $sr.ReadToEnd()
    Write-Output $results


    catch [System.Net.WebException]{
    if ($_.Exception -ne $null -and $_.Exception.Response -ne $null) {
    $errorResult = $_.Exception.Response.GetResponseStream()
    $errorText = (New-Object System.IO.StreamReader($errorResult)).ReadToEnd()
    Write-Warning “The remote server response: $errorText”
    Write-Output $_.Exception.Response.StatusCode
    } else {
    throw $_

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s