Azure Powershell 安全机密值

Azure Powershell secure secret value

我遇到了一个奇怪的问题,我不知道为什么它不起作用。我查看了所有文档并尝试了不同的解决方案,但一无所获。

我正在尝试在 powershelgl 中编写一个带有 2 个值的简单命令:

我想在 powershell 执行期间确保 value 安全,并在脚本完成后查看存储在 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,我找到的 none 解决方案或文档帮助我解决了这个问题。

拜托,如果有人能指导我如何实现这一点,我将不胜感激。

如果您需要更多信息,请随时问我

您不需要通过 InteropServices 调用执行这些转换。只需使用这三行。 password 变量已经是正确的类型,因为您正在阅读它 -AsSecureString.

$SecretName = Read-Host "Enter Secret Name"
$password = Read-Host "Enter password" -AsSecureString
Set-AzKeyVaultSecret -VaultName 'keyvaultname' -Name $SecretName -SecretValue $password