我有一个带有安全字符串的配置文件。这些字符串是我用于自动化的密码。
通常,我可以使用
加密我的密码Read-Host -AsSecureString | ConvertFrom-SecureString
系统会提示我输入密码,并输出安全字符串。我将这些安全字符串存储在配置文件中。
我可以将这些安全字符串查询到 powershell 脚本中,如下所示:
SecurePassword = ConvertTo-SecureString $Config.Configuration.password
这适用于我的 WinSCP 应用程序,因此我的配置文件中没有以纯文本形式存储凭据。
我的问题:
我正在尝试使用 WinRAR 的 Unrar 实用程序执行类似的操作。以下纯文本示例有效:
$PW = "mypassword"
& $unrar_path x $Source_Path.exe -p"$PW" $Destination_Path\
如果我尝试从配置文件中查询安全字符串:
$PW = ConvertTo-SecureString $Config.Configuration.ExtractPass
& $unrar_path x $Source_Path.exe -p"$PW" $Destination_Path\
它不接受密码。
我尝试了很多不同的语法版本,试图让安全字符串发挥作用。还有另一种我没有想到的方法吗?
非常欢迎任何帮助或建议。谢谢你。
WinRAR 不会将安全字符串作为参数,它没有这个概念,如果您从安全字符串开始并希望将纯文本密码作为参数传递,您将需要使用
ConvertFrom-SecureString -AsPlainText
来解密它(powershell 7+
) 或 NetworkCredential
(powershell 5.1
)。
$secureString = ConvertTo-SecureString $Config.Configuration.ExtractPass
if ($IsCoreCLR) {
$plainTextPassw = $secureString | ConvertFrom-SecureString -AsPlainText
}
else {
$plainTextPassw = [System.Net.NetworkCredential]::new('', $secureString).Password
}
& $unrar_path x $Source_Path.exe -p $plainTextPassw $Destination_Path\