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

前端 未结 2 1653
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 03:44

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 pr

相关标签:
2条回答
  • 2020-12-15 04:37

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

    [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
    
    0 讨论(0)
  • 2020-12-15 04:42

    You can save the password(input) as a variable and pass it to your service. If the code is run in a script or as a function, the variable containing the password will be deleted after it's done(they are stored in a temp. local scope). If you run the commands in the console(or dot-source the script like . .\myscript.ps1), the password variable will stay in the session scope, and they will be stored until you delete it or close the session. If you want to be sure the variable is removed after your script is run, you can delete it yourself. Like this:

    #Get password in cleartext and store in $password variable
    $password = Read-Host "Enter Pass"
    
    #run code that needs password stored in $password
    
    #Delete password
    Remove-Variable password
    

    To read more about how variables are stored in scopes, check out about_Scopes

    0 讨论(0)
提交回复
热议问题