我正在寻找一种方法来隐藏 Read-Host cmdlet 中的用户输入。
我知道我可以使用 -assecurestring 来做到这一点,但我想将输入保存为变量中的纯文本。
有没有可能的方法来做到这一点?
您必须使用
-AsSecureString
开关,但您也可以检索明文值:
$securedValue = Read-Host -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securedValue)
$value = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
# then free up the unmanged memory afterwards (thank to dimizuno)
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
我不确定如何将
-AsSecureString
用作 macOS 与新的 pwsh
(我不知道是否支持 *nix 上的编组 BSTR,因为 PtrToStringAuto()
只返回密码的第一个字符)。
现在有一个更简单的选择
-MaskInput
:
$ Read-Host "Password" -MaskInput
Password: ****
abcd