最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

azure devops - Escape string for use in PowerShell task passed as batch script parameter - Stack Overflow

programmeradmin2浏览0评论

I need to escape special characters of a password in a YAML file of an Azure DevOps pipeline. The password is part of the connection string of a (Oracle) database connection. It is thus stored as a secret within the pipeline variables.

The password is used in a PowerShell@2 task which creates an argument string for a Windows Batch script.

I know how to escape the password for use in YAML but it doesn't seem to work in the combination with the arg-list for the Batch script. Obviously I cannot post the password, but here is an example string with all characters that I have figured out as problematic:

aa<a$\a#a$aa!a

For the use in YAML PowerShell@2 tasks I supply the password string as (note the backtick for the 2nd $ sign):

aa<a$\a#a`$aa!a

YAML file snippet of the pipeline:

variables:
  db_pw: $(database_password)

jobs:
- job: Job_1
  displayName: Agent job
  [...]
  - task: PowerShell@2
    displayName: Run Tests and Code Coverage
    inputs:
      targetType: 'inline'
      script: |
        $db_user = 'my_user'
        $db_conn = 'my_host'
        $argstr = @("run $db_user/$(db_pw)@$db_conn", [...])
        $cmdPath = (Get-Command my_script.bat).Source 
        Start-Process -FilePath $cmdPath -ArgumentList $argstr -Wait -NoNewWindow
  [...]

Results in this error:

The filename, directory name, or volume label syntax is incorrect.

Things I have tried:

  • Put the string in single quotes
  • Put the string in double quotes
  • Escape the problematic characters (<, #, !, \) with appropriate escape characters
  • Remove/add backticks for the $ sign(s)
  • Combinations of all mentioned points

I need to escape special characters of a password in a YAML file of an Azure DevOps pipeline. The password is part of the connection string of a (Oracle) database connection. It is thus stored as a secret within the pipeline variables.

The password is used in a PowerShell@2 task which creates an argument string for a Windows Batch script.

I know how to escape the password for use in YAML but it doesn't seem to work in the combination with the arg-list for the Batch script. Obviously I cannot post the password, but here is an example string with all characters that I have figured out as problematic:

aa<a$\a#a$aa!a

For the use in YAML PowerShell@2 tasks I supply the password string as (note the backtick for the 2nd $ sign):

aa<a$\a#a`$aa!a

YAML file snippet of the pipeline:

variables:
  db_pw: $(database_password)

jobs:
- job: Job_1
  displayName: Agent job
  [...]
  - task: PowerShell@2
    displayName: Run Tests and Code Coverage
    inputs:
      targetType: 'inline'
      script: |
        $db_user = 'my_user'
        $db_conn = 'my_host'
        $argstr = @("run $db_user/$(db_pw)@$db_conn", [...])
        $cmdPath = (Get-Command my_script.bat).Source 
        Start-Process -FilePath $cmdPath -ArgumentList $argstr -Wait -NoNewWindow
  [...]

Results in this error:

The filename, directory name, or volume label syntax is incorrect.

Things I have tried:

  • Put the string in single quotes
  • Put the string in double quotes
  • Escape the problematic characters (<, #, !, \) with appropriate escape characters
  • Remove/add backticks for the $ sign(s)
  • Combinations of all mentioned points
Share edited Mar 4 at 9:37 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Mar 4 at 9:12 Markus LMarkus L 1,0262 gold badges22 silver badges40 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Using inline scripts in Azure DevOps tasks means you are generating the body of the script to be executed, so everything in it must be properly escaped.

The workaround is not setting the password directly in the script.

Set an environment variables at the task level instead of directly using a pipeline variable:

Example:

jobs:
- job: Job_1
  displayName: Agent job
  [...]
  - task: PowerShell@2
    displayName: Run Tests and Code Coverage
    inputs:
      targetType: 'inline'
      script: |
        # ...
        $argstr = @("run $db_user/${Env:myDbPassword}@$db_conn", [...])
        # ...
    env:
      myDbPassword: $(database_password) # <------------------ Environment variable set here
发布评论

评论列表(0)

  1. 暂无评论