Get password from user crypted and pass along

Have you ever received hazzel from the security guy when you store passwords in installation and/or configiguration files. Been there, done that 🙂

But Powershell provide a feature to avoid this problem. The follwing code lines will retrieve a password from the user as crypted string (first line). The following two lines will convert the crypted password to a readable string than can be passed to setup programs. The last line (number 4), will print the password.

$password = Read-Host -Prompt "Enter default password" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$psw = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

Write-Host $psw

I will most likely use this in my semi-automatic scripts when I shall install SQL Server, and pass the $psw to the setup.exe program as a command-line parameter as follow:

D:\setup.exe /configurationfile="C:\SQLInstall\ConfigurationFile.ini" 

Happy scripting…


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s