Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use powershell's read-host function to accept a password for an external service?

I have a script I'm writing that makes a connection to a SOAP service. After the connection is made, I need to pass in a the username/pass with every command I send. The problem I have is that when I use read-host to do this, my password is shown in cleartext and remains in the shell:

PS C:\Users\Egr> Read-Host "Enter Pass" Enter Pass: MyPassword MyPassword 

If I hide it with -AsSecureString, the value can no longer be passed to the service because it is now a System.Security.SecureString object:

PS C:\Users\gross> Read-Host "Enter Pass" -AsSecureString Enter Pass: ********** System.Security.SecureString 

When I pass this, it does not work. I don't care about the passwords being passed to the service in cleartext, I just don't want them sticking around on a user's shell after they enter their password. Is it possible to hide the Read-Host input, but still have the password stored as cleartext? If not, is there a way I can pass the System.Security.SecureString object as cleartext?

Thanks

like image 543
EGr Avatar asked Feb 21 '13 16:02

EGr


People also ask

How do I pass a password in PowerShell?

The first way to create a credential object is to use the PowerShell cmdlet Get-Credential . When you run without parameters, it prompts you for a username and password. Or you can call the cmdlet with some optional parameters.

What does the cmdlet read-host do?

Description. The Read-Host cmdlet reads a line of input from the console (stdin). You can use it to prompt a user for input. Because you can save the input as a secure string, you can use this cmdlet to prompt users for secure data, such as passwords.

How do I pass credentials in PowerShell without prompt?

$cred = Get-Credential without asking for prompts in powershell - Microsoft Tech Community.


1 Answers

$Password is a Securestring, and this will return the plain text password.

[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)) 
like image 139
Musaab Al-Okaidi Avatar answered Sep 18 '22 14:09

Musaab Al-Okaidi