admin管理员组文章数量:1406942
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
1 Answer
Reset to default 2Using 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
本文标签: azure devopsEscape string for use in PowerShell task passed as batch script parameterStack Overflow
版权声明:本文标题:azure devops - Escape string for use in PowerShell task passed as batch script parameter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745052947a2639762.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论