Azure pipelines: Setting and passing the password in powershell script

Viewed 56

I am setting the password at the Library in the Azure pipelines under a Variable group.

Now I want to use the password in one of my powershell script by using its variable name from the Library but I am getting error.

Write-Host "Signing of Scripts."
Write-Host $PSScriptRoot
If (Test-Path -Path "C:\DigiCerts\*"){
signtool sign /f C:\DigiCerts\Certificate.pfx /t http://timestamp.sectigo.com /fd SHA256 /p $DigicertsPassword C:\dev\package-scripts\scripts\*.ps1
}
Else {
Write-Host "required certificate not found to sign" -ForegroundColor Red
exit 1
}

So here I am using $DigicertsPassword from the Library under Variable group which I have stored the password.

****Error: DigicertsPassword : The term 'DigicertsPassword' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.+ ... tp://timestamp.sectigo.com /fd SHA256 /p "$(DigicertsPassword)" C:\de ... + CategoryInfo : ObjectNotFound: (DigicertsPassword:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

SignTool Error: Missing filename.****

1 Answers

As you confirmed, you are using something like below and it can get the value successfully.

trigger:
- none

pool:
  vmImage: ubuntu-latest
variables:
  - group: xxx #variable group name
steps:
- task: PowerShell@2
  env:
    DigicertsPassword: $(DigicertsPassword)
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.
      
      Write-Host "$env:DigicertsPassword"

The reason of the issue is secret value need to map, but $key is not a correct way to map.

It has been clarified here:

https://docs.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=bash#usage-4

Secrets are not automatically mapped in

By the way, inline script should be able to get the value of runtime variables.

Related