Azure DevOps Pipeline执行PowerShell脚本报错:字符串缺少终止符
问题:Azure DevOps Pipeline中执行PowerShell注册Runbook计划时报字符串终止符缺失错误
本地执行以下PowerShell命令可成功为Azure自动化Runbook注册计划:
Register-AzureRmAutomationScheduledRunbook -AutomationAccountName $automationAccount -ResourceGroupName $automationRg -RunbookName $runBookName -ScheduleName $scheduler.Name -Parameters @{"RESOURCEGROUP"="test-rg";"APPNAME"="test-app-del";"SUBSCRIPTIONID"="test-sub";"BRAND"="MS";"ENV"="dev"} -ErrorAction Stop
但在Azure DevOps Pipeline的AzureCLI@2任务中运行时,出现错误:The string is missing the terminator: "。任务中的相关代码如下:
$automationAccount = $rbEnv+"-aa-ng-infra" $automationRg=$rbEnv+"-rg-ng-infra-shared" $webSiteName = "pre${{parameters.brand}}-website-${{parameters.brand}}" $webSiteRG = $rbEnv+"-rg-ng-pre${{parameters.brand}}-website-eau" $runBookName = "Retire-Container-App" # check automation runbook exists $runbook = Get-AzureRmAutomationRunbook -AutomationAccountName $automationAccount -Name $runBookName" -ResourceGroupName $automationRg $TimeZone = "Australia/Sydney" $startTime = ([System.DateTimeOffset]::Now.AddHours("${{parameters.deleteContainerAppIn}}")) $scheduler = New-AzureRmAutomationSchedule -AutomationAccountName $automationAccount -Name "${{parameters.brand}}-aa-sch-eau" -OneTime -ResourceGroupName $automationRg -StartTime $startTime -TimeZone $TimeZone Register-AzureRmAutomationScheduledRunbook -AutomationAccountName $automationAccount -ResourceGroupName $automationRg -RunbookName $runBookName -ScheduleName $scheduler.Name -Parameters @{'RESOURCEGROUP'='test-rg';'APPNAME'='test-app-del';'SUBSCRIPTIONID'='test-sub';'BRAND'='MS';'ENV'='dev'} -ErrorAction Stop
解决方案
1. 修复语法错误
首先检查到Get-AzureRmAutomationRunbook行存在多余的双引号,这是直接引发解析异常的原因:
# 错误代码 $runbook = Get-AzureRmAutomationRunbook -AutomationAccountName $automationAccount -Name $runBookName" -ResourceGroupName $automationRg # 修正后 $runbook = Get-AzureRmAutomationRunbook -AutomationAccountName $automationAccount -Name $runBookName -ResourceGroupName $automationRg
该行的$runBookName"多了一个闭合双引号,本地PowerShell可能因容错性未触发错误,但在Pipeline环境中会直接暴露问题。
2. 处理AzureCLI@2任务的引号解析冲突
AzureCLI@2默认使用bash/cmd解析脚本内容,会提前解析PowerShell中的双引号,导致字符串终止符缺失。可通过两种方式解决:
方式一:指定脚本类型为PowerShell
在AzureCLI@2任务中设置scriptType: 'pscore'(或powershell),让任务直接用PowerShell解析脚本,避免外层shell干扰:
- task: AzureCLI@2 inputs: azureSubscription: '<你的Azure订阅>' scriptType: 'pscore' scriptLocation: 'inlineScript' inlineScript: | # 此处放置你的完整PowerShell代码
方式二:转义哈希表中的引号
如果必须使用默认脚本类型,可改用单引号定义参数哈希表(当前参数为固定值,单引号不影响功能):
Register-AzureRmAutomationScheduledRunbook -AutomationAccountName $automationAccount -ResourceGroupName $automationRg -RunbookName $runBookName -ScheduleName $scheduler.Name -Parameters @{'RESOURCEGROUP'='test-rg';'APPNAME'='test-app-del';'SUBSCRIPTIONID'='test-sub';'BRAND'='MS';'ENV'='dev'} -ErrorAction Stop
或者针对bash解析逻辑,用反斜杠转义双引号:
Register-AzureRmAutomationScheduledRunbook -AutomationAccountName $automationAccount -ResourceGroupName $automationRg -RunbookName $runBookName -ScheduleName $scheduler.Name -Parameters @{\"RESOURCEGROUP\"=\"test-rg\";\"APPNAME\"=\"test-app-del\";\"SUBSCRIPTIONID\"=\"test-sub\";\"BRAND\"=\"MS\";\"ENV\"=\"dev\"} -ErrorAction Stop
3. 验证Pipeline参数传递
检查${{parameters.brand}}和${{parameters.deleteContainerAppIn}}等参数值是否包含特殊字符,若存在特殊字符可能导致字符串截断。建议在参数定义时限制字符范围,或用PowerShell内置方法对参数值进行编码处理。
内容的提问来源于stack exchange,提问作者Madhu
相关产品推荐
相关产品推荐

