gpt4 book ai didi

azure - 如何将 AzureKeyVault 中的 secret 用于 PowerShell -Credential?

转载 作者:行者123 更新时间:2023-12-03 06:52:19 25 4
gpt4 key购买 nike

      - task: AzureKeyVault@2
inputs:
azureSubscription: 'mySub'
KeyVaultName: 'myVault'
SecretsFilter: 'myPass'
RunAsPreJob: false

我可以这样做来确保密码正确,确实如此。

echo $(myPass) > secret.txt

这是我在单独的任务中使用它的方式:

      - task: PowerShell@2
inputs:
targetType: inline
script: |
$password = ConvertTo-SecureString -String $(myPass) -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "administrator", $password
Invoke-Command -VMName "myVM" -ScriptBlock {
Write-Host "Hello!"
systeminfo
} -Credential $cred
displayName: 'Remoting into a computer.'

我收到此错误:

The credential is invalid.

如何使用从 keyvault 获取的密码访问虚拟机?

最佳答案

从您的 YAML 示例中,您正在 Pipeline 中使用 Key Vault key 。当您下载 Key Vault secret 时,它将作为 secret 变量保存在 Pipeline 中。

要在 Azure Pipeline 中使用 secret 变量,您需要在代理作业中显式映射 secret 变量。

在您的情况下,您需要在 PowerShell 任务中添加环境变量来映射 secret 变量。

这是一个例子:

- powershell: |
echo $(myPass)

displayName: 'PowerShell Script'
env:
myPass: $(myPass)

更详细的信息,您可以引用这个文档:Secret Variable

关于azure - 如何将 AzureKeyVault 中的 secret 用于 PowerShell -Credential?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73478147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com