Powershell masking password
Hello Everyone,
I have the script (API POST) below which is working fine.
$UserPass = “username:password”
[string]$stringToEncode=$UserPass
$encodedString=[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($stringToEncode))
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$headers = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”
$headers.Add(“content-type”, “application/json”)
$headers.Add(“accept”, “application/json”)
$headers.Add(“Authorization”, “Basic ” + $encodedString)
Invoke-RestMethod -Uri “https://api_base_url/session” -Headers $headers -Method POST
I want to mask the password instead of plain text. So modified it to below which will ask to enter the masked password.
$UserPass = “username:$(Read-Host -Prompt “Please type the password” -AsSecureString)“
But the whole script is not working anymore. When I troubleshoot, there is a difference in encoding/decoding. I ran an online base64 decode and encode, the result is different.
Plain text password – username:password
dXNlcm5hbWU6cGFzc3dvcmQ= —> username:password
Masked password – username:$(Read-Host -Prompt “Please type the password” -AsSecureString)
dXNlcm5hbWU6U3lzdGVtLlNlY3VyaXR5LlNlY3VyZVN0cmluZw== —> username:System.Security.SecureString
How can I mask the password but able to read the System.Security.SecureString as the actual password?
Thank you in advanced.
Hello Everyone, I have the script (API POST) below which is working fine. $UserPass = “username:password” [string]$stringToEncode=$UserPass$encodedString=[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($stringToEncode)) [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12$headers = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”$headers.Add(“content-type”, “application/json”)$headers.Add(“accept”, “application/json”)$headers.Add(“Authorization”, “Basic ” + $encodedString) Invoke-RestMethod -Uri “https://api_base_url/session” -Headers $headers -Method POST I want to mask the password instead of plain text. So modified it to below which will ask to enter the masked password. $UserPass = “username:$(Read-Host -Prompt “Please type the password” -AsSecureString)” But the whole script is not working anymore. When I troubleshoot, there is a difference in encoding/decoding. I ran an online base64 decode and encode, the result is different. Plain text password – username:passworddXNlcm5hbWU6cGFzc3dvcmQ= —> username:password Masked password – username:$(Read-Host -Prompt “Please type the password” -AsSecureString)dXNlcm5hbWU6U3lzdGVtLlNlY3VyaXR5LlNlY3VyZVN0cmluZw== —> username:System.Security.SecureString How can I mask the password but able to read the System.Security.SecureString as the actual password? Thank you in advanced. Read More