Created
February 14, 2018 19:56
-
-
Save torgro/b32e45d1eec81f64bb1f5ce5e67e61b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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