I am trying to take a variable from a variable group in Azure DevOps, enter it in to a PowerShell script that will change the value.
My variable group looks like:
The variable group name is = variableGroup, The only variable is = name: 'Alex'
dashboardTable.md (Trying to replace the string 'Alex')
My name is Alex.
azure-pipelines.yml
variables:
- group: variableGroup
- task: PowerShell@2
inputs:
targetType: 'inline'
script: '(Get-Content ./dashboardTable.md).replace("$(name)", 'Bob') | Set-Content ''./dashboardTable.md'''
Is this not actually possible or is my syntax wrong?
You need to consistently escape the '
characters embedded in the overall '...'
string as ''
:
variables:
- group: variableGroup
- task: PowerShell@2
inputs:
targetType: 'inline'
script: '(Get-Content ./dashboardTable.md).replace(''$(name)'', ''Bob'') | Set-Content ./dashboardTable.md'
You can simplify the string by using "..."
as the outer quoting, in which case no escaping is required for embedded '
:
variables:
- group: variableGroup
- task: PowerShell@2
inputs:
targetType: 'inline'
script: "(Get-Content ./dashboardTable.md).replace('$(name)', 'Bob') | Set-Content ./dashboardTable.md"
However, note that "..."
strings in YAML interpret \
as the escape character, so if a PowerShell command enclosed in "..."
contains instances of \
, they must be doubled (\\
).