PoshCode Archive  Artifact [10abf0c1a5]

Artifact 10abf0c1a541e125917b13f74e9f611ace0e64062e999d98798bc75c87c85c92:

  • File Access-Jira-REST.ps1 — part of check-in [672065399b] at 2018-06-10 14:14:11 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: 6368
# x-archived: 2016-08-06T17:51:50
# x-published: 2016-06-03T07:33: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"