Azure DevOps CI/CD中使用Azure CLI创建路由表路由遇Unexpected token错误
Azure DevOps AzureCLI任务PowerShell解析错误解决
问题场景
在Azure DevOps CI/CD流水线中使用AzureCLI@2任务创建路由表路由时,触发PowerShell解析错误,错误提示:
At D:\a\_temp\azureclitaskscript1712679912359_inlinescript.ps1:3 char:3 + --route-table-name test ` + ~ Missing expression after unary operator '--'. At D:\a\_temp\azureclitaskscript1712679912359_inlinescript.ps1:3 char:3 + --route-table-name test ` + ~~~~~~~~~~~~~~~~ Unexpected token 'route-table-name' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingExpressionAfterOperator
原任务脚本:
- task: AzureCLI@2 displayName: 'Route' inputs: azureSubscription: subscription scriptType: ps scriptLocation: inlineScript inlineScript: | az network route-table route create ` --resource-group myrg ` --route-table-name test ` --name vnetroute ` --next-hop-type VirtualAppliance ` --address-prefix 0.0.0.0/0 ` --next-hop-ip-address 0.0.0.0
错误原因
当前任务设置scriptType: ps,脚本以PowerShell模式执行。PowerShell的续行符(反引号)**后面不能包含任何字符(包括空格、缩进)**,否则续行会失效,后续的--xxx`参数会被当成独立的PowerShell语句,触发语法解析错误。
解决方法
方案一:修正PowerShell续行格式
确保每个续行符`都位于行的最后一位,且后面没有空格或缩进,同时参数行与主命令保持同一缩进级别:
- task: AzureCLI@2 displayName: 'Route' inputs: azureSubscription: subscription scriptType: ps scriptLocation: inlineScript inlineScript: | az network route-table route create ` --resource-group myrg ` --route-table-name test ` --name vnetroute ` --next-hop-type VirtualAppliance ` --address-prefix 0.0.0.0/0 ` --next-hop-ip-address 0.0.0.0
(注:需确保每个`后无任何空格,直接换行;参数行的缩进需与主命令一致,避免PowerShell将其识别为新语句)
方案二:切换为Bash脚本类型
如果构建代理支持Bash环境,可将scriptType改为bash,使用Bash的续行符\,无需调整缩进:
- task: AzureCLI@2 displayName: 'Route' inputs: azureSubscription: subscription scriptType: bash scriptLocation: inlineScript inlineScript: | az network route-table route create \ --resource-group myrg \ --route-table-name test \ --name vnetroute \ --next-hop-type VirtualAppliance \ --address-prefix 0.0.0.0/0 \ --next-hop-ip-address 0.0.0.0
内容的提问来源于stack exchange,提问作者user
相关产品推荐
相关产品推荐

