我遇到了一个奇怪的问题,我不知道为什么它不起作用。我查看了所有文档并尝试了不同的解决方案,但一无所获。
我正在尝试在 powershelgl 中编写一个需要 2 个值的简单命令:
- secret 名称
- secret 值
我希望在 powershell 执行期间保护值
,并在脚本完成后查看存储在 azure KeyVault -> Secrets 中的这些参数。
我设置了这个代码:
$SecretName = Read-Host "Enter Secret Name"
$password = Read-Host "Enter password" -AsSecureString
$password = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$password = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($password)
Set-AzKeyVaultSecret -VaultName "keyvaultname" -Name $SecretName -SecretValue $password
但这给了我以下错误:
Cannot bind parameter 'SecretValue'. Cannot convert the "asdlkjiou" value of type "System.String" to type "System.Security.SecureString".
注意:我正在转换回密码,因为如果不这样做,代码就会运行,但 KeyVault 中的值显示如下 System.Security.SecureString
我在这里几乎迷失了方向,很长一段时间没有使用 Windows PowerShell,并且我找到的解决方案或文档都没有帮助我解决这个问题。
拜托,如果有人能指导我如何实现这一目标,我将不胜感激。
如果您需要更多信息,请随时询问我
最佳答案
您不需要使用 InteropServices
调用来执行这些转换。只需使用这三行即可。 password
变量的类型已经正确,因为您正在读取它 -AsSecureString
。
$SecretName = Read-Host "Enter Secret Name"
$password = Read-Host "Enter password" -AsSecureString
Set-AzKeyVaultSecret -VaultName 'keyvaultname' -Name $SecretName -SecretValue $password
关于Azure Powershell 安全 secret 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70004682/