Skip to content

Instantly share code, notes, and snippets.

@torgro
Created February 14, 2018 19:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torgro/b32e45d1eec81f64bb1f5ce5e67e61b0 to your computer and use it in GitHub Desktop.
Save torgro/b32e45d1eec81f64bb1f5ce5e67e61b0 to your computer and use it in GitHub Desktop.
function Get-AuthorizationHeader
{
[cmdletbinding()]
Param(
[Parameter(
ValueFromPipeline)]
[PSCredential]
$Credential
)
Begin
{
$f = $MyInvocation.InvocationName
Write-Verbose -Message "$f - START"
}
Process
{
if (-not $Credential)
{
Write-Warning -Message "Credential parameter is null"
return
}
$userName = $Credential.GetNetworkCredential().UserName
$pass = $Credential.GetNetworkCredential().Password
$domain = $Credential.GetNetworkCredential().Domain
[string]$authString = "$($userName):$pass"
if (-not [string]::IsNullOrEmpty($domain))
{
[string]$authString = "$($userName)@$($domain):$pass"
}
$auth = $authString | ConvertTo-Base64
@{
"Authorization" = "Basic $auth"
}
}
End
{
Write-Verbose -Message "$f - END"
}
}
function ConvertTo-Base64
{
[cmdletbinding()]
Param(
[Parameter(
Mandatory,
ValueFromPipeline)]
[string[]]
$InputObject
)
Begin
{
$f = $MyInvocation.InvocationName
Write-Verbose -Message "$f - START"
}
Process
{
foreach ($string in $InputObject)
{
$bytes = [System.Text.Encoding]::UTF8.GetBytes($string)
[System.Convert]::ToBase64String($bytes)
}
}
End
{
Write-Verbose -Message "$f - EMD"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment