Skip to content

Instantly share code, notes, and snippets.

@jpbruckler
Last active October 4, 2018 22:04
Show Gist options
  • Save jpbruckler/8ea2040de49fbf71de81e9d8709db57b to your computer and use it in GitHub Desktop.
Save jpbruckler/8ea2040de49fbf71de81e9d8709db57b to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Creates an HTTP header using BASIC authentication for use with Invoke-WebRequest or Invoke-RestMethod.
.DESCRIPTION
PowerShell 5 and below does not support BASIC authentication with Invoke-WebRequest and Invoke-RestMethod.
This function will create a header in the format necessary to send BASIC authorization strings for HTTP
requests. The returned header defaults to a hashtable, but with the -AsJson switch, it will return
properly formatted JSON.
Optionally, an Accept and Content-Type can be provided.
.PARAMETER Credential
A PSCredential object consisting of the username and password used for BASIC authentication.
.PARAMETER Accept
A string representing the HTTP Accept necessary for interacting with the remote server.
.PARAMETER ContentType
A string representing the HTTP Content-Type being sent to the remote server.
.PARAMETER AsJson
When provided, returns a JSON object, instead of a hashtable.
.EXAMPLE
PS C:\> $Credential = Get-Credential
PS C:\> New-BasicAuthHeader -Credential $Credential
Name Value
---- -----
Authorization Basic c3lzdGHjasoKjlkIZXN0cmF0aW9uOlY0c0JRTzBkS3E3ZnJKLioUghNjGo==
The example above a hashtable containing a Key called Authorization with a Value of the
Authorization string created from the credential passed in.
.EXAMPLE
PS C:\> $Credential = Get-Credential
PS C:\> New-BasicAuthHeader -Credential $cred -Accept 'application/json' -ContentType 'application/json'
Name Value
---- -----
Authorization Basic c3lzdGHjasoKjlkIZXN0cmF0aW9uOlY0c0JRTzBkS3E3ZnJKLioUghNjGo==
Content-Type application/json
Accept application/json
.EXAMPLE
PS C:\> $Credential = Get-Credential
PS C:\> New-BasicAuthHeader -Credential $cred -Accept 'application/json' -ContentType 'application/json' -AsJson
{
"Authorization": "Basic c3lzdGHjasoKjlkIZXN0cmF0aW9uOlY0c0JRTzBkS3E3ZnJKLioUghNjGo==",
"Content-Type": "application/json",
"Accept": "application/json"
}
#>
function New-BasicAuthHeader {
param(
[Parameter( Mandatory )]
[pscredential] $Credential,
[string] $Accept,
[string] $ContentType,
[switch] $AsJson
)
process {
$Header = @{}
# Process and convert the credential into a BasicAuth string
$Bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credential.Password)
$BasicPlain = '{0}:{1}' -f $Credential.UserName, ([System.Runtime.InteropServices.Marshal]::PtrToStringAuto($Bstr))
$BasicB64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($BasicPlain))
$Header.Add('Authorization',('Basic {0}' -f $BasicB64))
if ($Accept) {
$Header.Add('Accept', $Accept)
}
if ($ContentType) {
$Header.Add('Content-Type', $ContentType)
}
if ($AsJson) {
Write-Output ($Header | ConvertTo-Json)
}
else {
Write-Output $Header
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment