forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertTo-BasicAuthentication.ps1
More file actions
35 lines (25 loc) · 1.25 KB
/
Copy pathConvertTo-BasicAuthentication.ps1
File metadata and controls
35 lines (25 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<#
.SYNOPSIS
Produces a basic authentication header string from a credential.
.INPUTS
System.Management.Automation.PSCredential to convert to the Authorization HTTP header value.
.OUTPUTS
System.String to use as the Authorization HTTP header value.
.LINK
https://tools.ietf.org/html/rfc1945#section-11.1
.LINK
http://stackoverflow.com/q/24672760/54323
.LINK
https://weblog.west-wind.com/posts/2010/Feb/18/NET-WebRequestPreAuthenticate-not-quite-what-it-sounds-like
.LINK
https://powershell.org/forums/topic/pscredential-parameter-help/
.EXAMPLE
Invoke-RestMethod https://example.com/api/items -Method Get -Headers @{Authorization=ConvertTo-BasicAuthentication.ps1 (Get-Credential -Message 'Log in')}
Calls a REST method that requires Basic authentication on the first request (with no challenge-response support).
#>
[CmdletBinding()][OutputType([string])] Param(
# Specifies a user account to authenticate an HTTP request that only accepts Basic authentication.
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)]
[PSCredential][Management.Automation.Credential()]$Credential
)
'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($Credential.UserName+':'+$Credential.GetNetworkCredential().Password))