Guest User

Untitled

a guest
Feb 26th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. function Get-AuthorizationHeader
  2. {
  3. [cmdletbinding()]
  4. Param(
  5. [Parameter(
  6. ValueFromPipeline)]
  7. [PSCredential]
  8. $Credential
  9. )
  10.  
  11. Begin
  12. {
  13. $f = $MyInvocation.InvocationName
  14. Write-Verbose -Message "$f - START"
  15. }
  16.  
  17. Process
  18. {
  19. if (-not $Credential)
  20. {
  21. Write-Warning -Message "Credential parameter is null"
  22. return
  23. }
  24.  
  25. $userName = $Credential.GetNetworkCredential().UserName
  26. $pass = $Credential.GetNetworkCredential().Password
  27. $domain = $Credential.GetNetworkCredential().Domain
  28.  
  29. [string]$authString = "$($userName):$pass"
  30.  
  31. if (-not [string]::IsNullOrEmpty($domain))
  32. {
  33. [string]$authString = "$($userName)@$($domain):$pass"
  34. }
  35.  
  36. $auth = $authString | ConvertTo-Base64
  37. @{
  38. "Authorization" = "Basic $auth"
  39. }
  40. }
  41.  
  42. End
  43. {
  44. Write-Verbose -Message "$f - END"
  45. }
  46. }
  47.  
  48. function ConvertTo-Base64
  49. {
  50. [cmdletbinding()]
  51. Param(
  52. [Parameter(
  53. Mandatory,
  54. ValueFromPipeline)]
  55. [string[]]
  56. $InputObject
  57. )
  58. Begin
  59. {
  60. $f = $MyInvocation.InvocationName
  61. Write-Verbose -Message "$f - START"
  62. }
  63.  
  64. Process
  65. {
  66. foreach ($string in $InputObject)
  67. {
  68. $bytes = [System.Text.Encoding]::UTF8.GetBytes($string)
  69. [System.Convert]::ToBase64String($bytes)
  70. }
  71. }
  72.  
  73. End
  74. {
  75. Write-Verbose -Message "$f - EMD"
  76. }
  77. }
Add Comment
Please, Sign In to add comment