powershell - 如何在 DevOps 管道上运行的 Powershell 上使用 key 保管库 secret

标签 powershell azure-devops azure-pipelines azure-keyvault

大家

我的 DevOps 管道中有 2 个任务,第一个任务是从 Azure Key Vault 获取 secret 值,

trigger: none
jobs: 
- job: PBICDSolution
  pool:
    vmImage: windows-latest

  steps:
  - checkout: self
  - task: AzureKeyVault@2
    inputs:
      azureSubscription: '<my subscription>'
      KeyVaultName: 'PA01'
      SecretsFilter: '<my secret name>'
      RunAsPreJob: false

接下来我想在我的 powershell 脚本中使用这个 secret 值来登录服务主体帐户。这是我的 powershell 代码,

$azureAplicationId = "<my service principal client id>"
$azureTenantId= "<my tanant id>"

Write-Output "Generate Credential"
$azurePassword = ConvertTo-SecureString <here should be the variable of AKV secret value> -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)

Write-Output "Login SP"
Connect-PowerBIServiceAccount -Tenant $azureTenantId -ServicePrincipal -Credential $psCred

这是上述任务的管道 yml 代码,

  - task: AzurePowerShell@5
    inputs:
      azureSubscription: '<my subscripton name>'
      ScriptType: 'FilePath'
      ScriptPath: '$(Build.SourcesDirectory)\<my ps file name>.ps1'
      ScriptArguments: '<how can i set the variable here?>'
      azurePowerShellVersion: 'LatestVersion'

所以问题来了,如何从task1获取输出值,然后将该值传递给task2(PowerShell脚本)? 我引用过this docs但这没有帮助,因为我不需要将 secret 下载到 txt 文件。

任何解决方案将不胜感激!

最佳答案

how can I get the output value from task1, then pass this value into task2 (PowerShell script)?

您在 Azure Key Vault 任务中下载的 Key Vault 可以用作管道变量。

请参阅以下步骤来使用 Key Vault。

在 Azure Powershell 任务中,您可以定义参数:-azurePassword $(azurePassword)

例如:

- task: AzurePowerShell@5
  displayName: 'Azure PowerShell script: FilePath'
  inputs:
    azureSubscription: xx
    ScriptPath: test.ps1
    ScriptArguments: '-azurePassword  $(azurePassword)'
    preferredAzurePowerShellVersion: 3.1.0

在Powershell文件中,您可以定义参数。

例如:

param (
  
  [string]$azurePassword
)

关于powershell - 如何在 DevOps 管道上运行的 Powershell 上使用 key 保管库 secret ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71850843/

相关文章:

visual-studio - 具有多个 Web 项目的解决方案的 VSTS 构建定义

powershell - 创建自定义文化并将其注册到本地机器 powershell

powershell - 如何使用 ComObject 从计划任务的操作中获取路径

powershell - 如何将用户变量从 TFS 2015 Build 定义传递给 shell 脚本?

azure-devops - 使用多个框架时 .Net 5 的 Azure Devops 构建失败

带有审核应用程序的 Azure Pipeline - 合并后清理

list - 如何使用Powershell在Sharepoint 2013中的默认列列表中选中复选框?

templates - 从 YAML 管道到模板文件的 Azure Pipeline 动态参数

asp.net-core - NETSDK1045 : The current . NET SDK不支持 'newer version'作为目标

azure - 使用 Visual Studio Online 进行 SlowCheetah 转换失败