Search code examples
azure-devopsencodingazure-pipelinesazure-powershellazure-pipelines-yaml

Detected characters in arguments that may not be executed correctly by the shell. Please escape special characters using backtick (`)


When running the below azure pipeline, I get the error:

Detected characters in arguments that may not be executed correctly by the shell. Please escape special characters using backtick (`).

This happens because of the Unicode character £ inside the password. For simplification, I put the password inside the script variable, but the real pipeline get it from it's parameters.

It seams to be an error at the azure task PowerShell@2 when generating the temporary script.

How to allow this special characters to be passed?

The pipeline script:

variables:
- name: password
  value: £f1l1

pool:
  vmImage: windows-latest
  
steps:                          
- task: PowerShell@2
  inputs:
    filePath: 'someScript.ps1'
    arguments: "-password $(password)"

Solution

  • Using environment variables set at the task level is probably the best way to avoid escaping/encoding issues. Also, it is the recommended approach when dealing with sensitive information such as passwords.

    Using a Powershell script with parameters

    Test script:

    # scripts/someScript1.ps1
    
    param (
       [string]$password
    )
    
    Write-Host "Password: $password"
    

    Test pipeline:

    variables:
      - name: password
        value: £f1l1
    
    steps:
      - checkout: self
    
      - task: PowerShell@2
        displayName: 'Run script'
        inputs:
          targetType: 'inline'
          script: |
            ./someScript1.ps1 -password $Env:MyPassword
          workingDirectory: '$(Build.SourcesDirectory)/scripts'
        env:
          MyPassword: $(password) # <-------------------- environment variable set here
    

    Running the pipeline:

    Pipeline logs

    Using a Powershell script with environment variables

    Test script:

    # scripts/someScript2.ps1
    
    Write-Host "Password: ${Env:MyPassword}"
    

    Test pipeline:

    variables:
      - name: password
        value: £f1l1
    
    steps:
      - checkout: self
    
      - task: PowerShell@2
        displayName: 'Run script with environment variable'
        inputs:
          filePath: '$(Build.SourcesDirectory)/scripts/someScript2.ps1'
        env:
          MyPassword: $(password) # <-------------------- environment variable set here
    

    Running the pipeline:

    Pipeline logs