Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - read-host -assecurestring problem

Reply
 
Old 06-05-2008   #1 (permalink)
GodOfLions


 
 

read-host -assecurestring problem

I want to be able to type in a password and have it masked with ****, but I
need it to be in plain-text in the script I'm using. So I tried the following:

$SecurePassword = read-host -AsSecureString "Type in your new password"
$PlainPassword = ConvertFrom-SecureString $SecurePassword
write-host "Plain Text Password = " $PlainPassword

When I run this, instead of getting a plain-text version of the password, I
get a long string of numerals. Granted I'll not simply just post the password
to the user, but for the sake of example the above snippet shows my point and
problem.

The problem I have with using read-host without the -assecurestring is
two-fold:
1. Anyone can see what you type in
2. What you type into read-host is stored in the command history, so you can
just "up-arrow" and see what was last typed into read-host.

I want to prevent people from looking over one's shoulder and not have the
value stored in the command history.

So what do I need to do in order for powershell to use read-host
-assecurestring and yet be able to use the plain-text value of the password
later in the script.

My System SpecsSystem Spec
Old 06-05-2008   #2 (permalink)
Oisin (x0n) Grehan [MVP]


 
 

Re: read-host -assecurestring problem

On Jun 5, 11:54*am, GodOfLions <GodOfLi...@xxxxxx>
wrote:
Quote:

> I want to be able to type in a password and have it masked with ****, but I
> need it to be in plain-text in the script I'm using. So I tried the following:
>
> $SecurePassword = read-host -AsSecureString "Type in your new password"
> $PlainPassword = ConvertFrom-SecureString $SecurePassword
> write-host "Plain Text Password = " $PlainPassword
>
> When I run this, instead of getting a plain-text version of the password, I
> get a long string of numerals. Granted I'll not simply just post the password
> to the user, but for the sake of example the above snippet shows my point and
> problem.
>
> The problem I have with using read-host without the -assecurestring is
> two-fold:
> 1. Anyone can see what you type in
> 2. What you type into read-host is stored in the command history, so you can
> just "up-arrow" and see what was last typed into read-host.
>
> I want to prevent people from looking over one's shoulder and not have the
> value stored in the command history.
>
> So what do I need to do in order for powershell to use read-host
> -assecurestring and yet be able to use the plain-text value of the password
> later in the script.
Hi,

SecureString objects are part of the deeper operating system rather
than something PowerShell invented, so there are a few hoops to jump
through and apologies if I don't explain this but it's a bit hairy.

Function ConvertTo-PlainText( [security.securestring]$secure ) {
$marshal = [Runtime.InteropServices.Marshal]
$marshal::PtrToStringAuto( $marshal::SecureStringToBSTR($secure) )
}

usage:

ps > $s = read-host -assecurestring "password?"
password?: ****
ps > convertto-plaintext $s
test

Hope this helps,

- Oisin

PowerShell MVP
http://www.nivot.org/

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
VbSystemModal type functionality for Read-Host input PowerShell
Can't read physical CD without rebooting host Virtual Server
Read-Host values into an Array PowerShell
Read-Host issue, won't store to variable when using multiple read-host lines PowerShell
Default value for Read-Host PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

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 36 37 38 39 40 41 42 43 44 45 46