Azure DevOps中AWSShellScript传递带前置/参数异常求助
问题:阻止带前置斜杠的参数被识别为相对路径
在Azure DevOps中使用AWSShellScript@1任务执行PowerShell脚本,任务配置如下:
- task: AWSShellScript@1 inputs: scriptType: inline inlineScript: 'pwsh -File test-script.ps1 -pathValue $(pathValue)'
当流水线变量pathValue的值为带前置斜杠的/apigw/sub时(因遗留系统限制无法移除斜杠),脚本接收到的不是预期的/apigw/sub,而是c:/program files/git/pathvalue。PowerShell脚本参数定义如下:
param ( [Parameter(Mandatory=$true)][string]$pathValue ) Write-Host " > Path: $pathValue"
如何阻止带前置/的参数被识别为相对路径?
解决方案
1. 给参数值添加单引号包裹
修改AWSShellScript@1的inlineScript,把$(pathValue)用单引号包裹,避免Git Bash(AWSShellScript任务默认用Git Bash执行)解析前置斜杠为相对路径:
- task: AWSShellScript@1 inputs: scriptType: inline inlineScript: 'pwsh -File test-script.ps1 -pathValue ''$(pathValue)'''
注:外层已用单引号包裹脚本内容,内部单引号需用双单引号转义。
2. 使用PowerShell停止解析符号--%
在pwsh命令后添加--%参数,让PowerShell不再解析后续的路径类参数,直接传递原始值:
- task: AWSShellScript@1 inputs: scriptType: inline inlineScript: 'pwsh --% -File test-script.ps1 -pathValue $(pathValue)'
--%是PowerShell的停止解析符号,后续参数会原封不动传递给脚本,不会被Git Bash或PowerShell解析转换。
3. 切换任务执行环境为PowerShell
若无需AWSShellScript的特定功能,可直接改用PowerShell@2任务执行脚本,绕过Git Bash的路径解析逻辑:
- task: PowerShell@2 inputs: targetType: 'filePath' filePath: 'test-script.ps1' arguments: '-pathValue $(pathValue)'
内容的提问来源于stack exchange,提问作者user1474992
相关产品推荐
相关产品推荐

