Unable to read the encrypted password.

  • Hi

    I used the below command to accept password. But unable to print the password entered.

    Please help me with the correct format to decrypt the password? I don't want to hard-code the password to my script.

    $SQLServiceAGTPassword = read-host "Enter a Password for SQL Agent AC:" -assecurestring | ConvertTo-SecureString -AsPlainText -Force

    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SQLServiceAGTPassword)

    $RetrievePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

    Write-Host "Password is: " $SQLServiceAGTPassword

    Thanks.

  • SQL-DBA-01 (3/23/2015)


    Hi

    I used the below command to accept password. But unable to print the password entered.

    Please help me with the correct format to decrypt the password? I don't want to hard-code the password to my script.

    $SQLServiceAGTPassword = read-host "Enter a Password for SQL Agent AC:" -assecurestring | ConvertTo-SecureString -AsPlainText -Force

    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SQLServiceAGTPassword)

    $RetrievePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

    Write-Host "Password is: " $SQLServiceAGTPassword

    Quick thought, don't convert to SecureString twice, using the -asSecureString does that for you.

    😎

    $InputStr = Read-Host "Enter a Password for SQL Agent AC:" -assecurestring

    $PWSTRPTR = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($InputStr)

    $PlainTextPW = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($PWSTRPTR)

    [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($PWSTRPTR)

    Write-Host "Password is: " $PlainTextPW

  • Eirikur is correct. The following is your own code adjusted:

    $SQLServiceAGTPassword = read-host "Enter a Password for SQL Agent AC:" -assecurestring

    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SQLServiceAGTPassword)

    $RetrievePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

    Write-Host "Password is: " $SQLServiceAGTPassword

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Thanks Gary and Eri for your help.

    The problem now is, if I run the small script, I get the o/p as:

    Password is: System.Security.SecureString

    Is there anyways, I can ask user to enter the password and it will encrypot it and if I want to read the password (for validation) I shall be able to print it? Any suggestion? Note: I don't want to hard-code the password.

    Note: If I run the below script, I can retrieve / see the password but it's hardcoded.

    $PlainPassword = "@Hi" | ConvertTo-SecureString -AsPlainText -Force

    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($PlainPassword)

    $PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

    Write-Host "Password is: " $PlainPassword

    Thanks.

  • Sorry but I had copied your typo. Try this:

    $SQLServiceAGTPassword = read-host "Enter a Password for SQL Agent AC:" -assecurestring

    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SQLServiceAGTPassword)

    $RetrievePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

    Write-Host "Password is: " $RetrievePassword

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • It works. 🙂

    Thanks.

  • You are most welcome. 🙂

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply