Azure CLI更新自动化账户Runbook内容时遇换行截断问题
问题根源与解决方案
问题根源
这个内容截断问题主要源于Azure DevOps的AzureCLI@2任务默认使用的PowerShell Shell对文件内容传递的处理逻辑:当你用--content "@temp.txt"`写法时,PowerShell会把文件里的换行符识别为命令分隔符,导致仅传递第一行内容就终止读取,并非Azure CLI本身的问题。
解决办法
有两种可行的修复方案,可根据流水线需求选择:
方案1:切换为Bash Shell执行命令
在AzureCLI@2任务中指定使用Bash,它能正确解析@文件路径的写法,完整读取包含换行的文件内容:
- task: AzureCLI@2 inputs: azureSubscription: '<你的Azure订阅>' scriptType: 'bash' scriptLocation: 'inlineScript' inlineScript: | az automation runbook replace-content --debug --automation-account-name "${{ parameters.automationAccountName }}" --resource-group "${{ parameters.resourceGroup }}" --name "Schedule Summary" --content "@temp.txt"
方案2:用PowerShell完整读取内容后传递
如果必须保留PowerShell作为执行Shell,先将文件内容完整读入变量,再传递给Azure CLI命令,确保换行符被完整保留:
- task: AzureCLI@2 inputs: azureSubscription: '<你的Azure订阅>' scriptType: 'pscore' scriptLocation: 'inlineScript' inlineScript: | $runbookContent = Get-Content -Path temp.txt -Raw az automation runbook replace-content --debug --automation-account-name "${{ parameters.automationAccountName }}" --resource-group "${{ parameters.resourceGroup }}" --name "Schedule Summary" --content "$runbookContent"
这里的-Raw参数会原样读取文件的所有内容(包括CRLF或LF格式的换行),避免PowerShell拆分内容。
内容的提问来源于stack exchange,提问作者Optional
相关产品推荐
相关产品推荐

