PoshCode Archive  Artifact [95232b8807]

Artifact 95232b88072b5513a62e1523ef8609177e5812a69efc484ebd23aa6db85ce306:

  • File Access-Jira-REST.ps1 — part of check-in [4bfd364cf7] at 2018-06-10 13:26:04 on branch trunk — PowerShell 3 Script to call a Jira REST Service. Does HTTP BASIC Pre-Authentication (a plain Invoke-RestMethod does not work) (user: Markus Essl size: 1872)

# encoding: ascii
# api: powershell
# title: Access Jira (REST)
# description: PowerShell 3 Script to call a Jira REST Service. Does HTTP BASIC Pre-Authentication (a plain Invoke-RestMethod does not work)
# version: 0.1
# type: function
# author: Markus Essl
# license: CC0
# function: ConvertTo-UnsecureString
# x-poshcode-id: 3462
# x-archived: 2017-04-30T09:59:32
# x-published: 2013-06-20T00:10:00
#
#
param($Issue, $Credentials = $(Get-Credential), $BaseURI = "https://your.jira.server/jira")

function ConvertTo-UnsecureString(
    [System.Security.SecureString][parameter(mandatory=$true)]$SecurePassword)
{
    $unmanagedString = [System.IntPtr]::Zero;
    try
    {
        $unmanagedString = [Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($SecurePassword)
        return [Runtime.InteropServices.Marshal]::PtrToStringUni($unmanagedString)
    }
    finally
    {
        [Runtime.InteropServices.Marshal]::ZeroFreeGlobalAllocUnicode($unmanagedString)
    }
}

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

   return $encoded;
}

function ConvertFrom-Base64($string) {
   $bytes  = [System.Convert]::FromBase64String($string);
   $decoded = [System.Text.Encoding]::UTF8.GetString($bytes);

   return $decoded;
}

function Get-HttpBasicHeader($Credentials, $Headers = @{})
{
	$b64 = ConvertTo-Base64 "$($Credentials.UserName):$(ConvertTo-UnsecureString $Credentials.Password)"
	$Headers["Authorization"] = "Basic $b64"
	return $Headers
}

if($Issue) {
	$uri = "$BaseURI/rest/api/2/issue/$Issue"
} else {
	$uri = "$BaseURI/rest/api/2/mypermissions" 
}

$headers = Get-HttpBasicHeader $Credentials
Invoke-RestMethod -uri $uri -Headers $headers -ContentType "application/json"