Created
February 17, 2023 08:25
-
-
Save drlsdee/d361beb2183201d704314905b2c0ac00 to your computer and use it in GitHub Desktop.
The function appends the username and password, both URL-encoded, to the specified URI. It can be useful for cases where you need to specify a username and password in an HTTP request, and either the username or the password or both contain special characters that need to be escaped.
This file contains hidden or 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
| #Requires -Version 5.1 | |
| #Requires -Assembly "System.UriBuilder, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" | |
| #Requires -Assembly "System.Web.HttpUtility, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" | |
| <# | |
| .SYNOPSIS | |
| The function appends the username and password, both URL-encoded, to the specified URI. | |
| .DESCRIPTION | |
| The function appends the username and password, both URL-encoded, to the specified URI. | |
| It can be useful for cases where you need to specify a username and password in an HTTP request, and either the username or the password or both contain special characters that need to be escaped. | |
| .PARAMETER Uri | |
| Specifies the input URI. If the source URI already contains a username and password, these credentials will be replaced. | |
| .PARAMETER Credential | |
| Specifies the credentials in a secure form to append to the source URI. | |
| .PARAMETER UserName | |
| Specifies the username in plain text to append to the source URI. | |
| .PARAMETER PassPlain | |
| Specifies the password in plain text to append to the source URI. | |
| .EXAMPLE | |
| PS C:\> Set-UriUserInfo -Uri 'https://site.contoso.com/site/path?and=query' -UserName 'sampleLogin' -PassPlain 'P@s$w0:rD!' | |
| The function appends the username and password, both URL-encoded, and returns this URI: | |
| https://sampleLogin:P%40s%24w0%3arD!@site.contoso.com:443/site/path?and=query | |
| .EXAMPLE | |
| PS C:\> 'https://site.contoso.com/site/path?and=query' | Set-UriUserInfo -UserName 'sampleLogin' -PassPlain 'P@s$w0:rD!' | |
| The function appends the username and password, both URL-encoded, and returns this URI: | |
| https://sampleLogin:P%40s%24w0%3arD!@site.contoso.com:443/site/path?and=query | |
| .EXAMPLE | |
| PS C:\> Set-UriUserInfo -Uri 'https://site.contoso.com/site/path?and=query' -Credential 'sampleLogin' | |
| The function asks for the credential, then decodes them, appends the extracted username and password, both URL-encoded, and returns this URI: | |
| https://sampleLogin:P%40s%24w0%3arD!@site.contoso.com:443/site/path?and=query | |
| .EXAMPLE | |
| PS C:\> 'https://site.contoso.com/site/path?and=query' | Set-UriUserInfo -Credential 'sampleLogin' | |
| The function asks for the credential, then decodes them, appends the extracted username and password, both URL-encoded, and returns this URI: | |
| https://sampleLogin:P%40s%24w0%3arD!@site.contoso.com:443/site/path?and=query | |
| .INPUTS | |
| [System.Uri] | |
| [System.Management.Automation.PSCredential] | |
| [System.String] | |
| .OUTPUTS | |
| [System.Uri] | |
| .NOTES | |
| It can be useful for cases where you need to specify a username and password in an HTTP request, and either the username or the password or both contain special characters that need to be escaped. | |
| Specifying credentials in plain text is a bad idea, yep. | |
| .LINK | |
| . | |
| #> | |
| function Set-UriUserInfo { | |
| [CmdletBinding()] | |
| [OutputType([uri])] | |
| param ( | |
| # Specifies the input URI. If the source URI already contains a username and password, these credentials will be replaced. | |
| [Parameter( | |
| Mandatory = $true, | |
| ValueFromPipeline = $true, | |
| ValueFromPipelineByPropertyName = $true, | |
| Position = 0 | |
| )] | |
| [uri] | |
| $Uri, | |
| # Specifies the credentials in a secure form to append to the source URI. | |
| [Parameter( | |
| Mandatory = $true, | |
| ValueFromPipelineByPropertyName = $true, | |
| ParameterSetName = 'Credential', | |
| Position = 1 | |
| )] | |
| [pscredential] | |
| $Credential, | |
| # Specifies the username in plain text to append to the source URI. | |
| [Parameter( | |
| Mandatory = $true, | |
| ValueFromPipelineByPropertyName = $true, | |
| ParameterSetName = 'UserPass', | |
| Position = 1 | |
| )] | |
| [Alias('user')] | |
| [Alias('u')] | |
| [Alias('Login')] | |
| [Alias('l')] | |
| [string] | |
| $UserName, | |
| # Specifies the password in plain text to append to the source URI. | |
| [Parameter( | |
| Mandatory = $true, | |
| ValueFromPipelineByPropertyName = $true, | |
| ParameterSetName = 'UserPass', | |
| Position = 2 | |
| )] | |
| [Alias('Password')] # The word "Password" is specified as the parameter alias instead of the parameter name due to warnings displayed by PSScriptAnalyzer | |
| [Alias('pass')] | |
| [Alias('p')] | |
| [string] | |
| $PassPlain | |
| ) | |
| begin {} | |
| process { | |
| [System.UriBuilder]$uriBuilder = [System.UriBuilder]::new($Uri) | |
| if ($PSCmdlet.ParameterSetName -eq 'Credential') { | |
| [string]$UserName = $Credential.UserName | |
| [string]$PassPlain = $Credential.GetNetworkCredential().Password | |
| } | |
| $uriBuilder.UserName = [System.Web.HttpUtility]::UrlEncode($UserName) | |
| $uriBuilder.Password = [System.Web.HttpUtility]::UrlEncode($PassPlain) | |
| return $uriBuilder.Uri | |
| } | |
| end {} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment