是否可以在Powershell中隐藏来自read-host的用户输入?

问题描述 投票:0回答:2

我正在寻找一种方法来隐藏 Read-Host cmdlet 中的用户输入。

我知道我可以使用 -assecurestring 来做到这一点,但我想将输入保存为变量中的纯文本。

有没有可能的方法来做到这一点?

powershell input hide powershell-cmdlet
2个回答
29
投票

您必须使用

-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)

0
投票

我不确定如何将

-AsSecureString
用作 macOS 与新的
pwsh
(我不知道是否支持 *nix 上的编组 BSTR,因为
PtrToStringAuto()
只返回密码的第一个字符)。

现在有一个更简单的选择

-MaskInput

$ Read-Host "Password" -MaskInput
Password: ****
abcd
© www.soinside.com 2019 - 2024. All rights reserved.