Azure Powershell 安全 secret 值

标签 azure powershell devops azure-resource-manager azure-keyvault

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

我正在尝试在 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/

相关文章:

wcf - 在 Azure 上的 WCF 中通过 SSL 配置 webHTTP 和 NetHTTP 绑定(bind)

azure - Azure Data Lake 中的 U-SQL 输出

sql-server - sqlcmd/Invoke-SqlCmd 中的转义变量

linux - 用于启动容器的 docker compose 命令

azure - 我们如何在Azure发布管道或测试计划中发布HTML报告(在代理计算机上生成)?

docker - Docker进行服务器配置

azure - 优化时间戳范围查询的 Delta Lake 分区

asp.net - Azure Web 应用程序错误,没有已知的此类主机

powershell - Service Fabric/Jenkins 集成问题

azure - 无法获取昨天或使用 powershell 之前创建的存储帐户中的 blob