如何在Azure Pipelines PowerShell任务中编写内联多行Powershell脚本?

时间:2019-10-14 08:21:18

标签: powershell azure-pipelines

Powershell task的yaml架构允许您选择targetType:'inline'并在脚本:input中定义脚本。

但是编写多行脚本的正确格式是什么?

文档未指定操作方式,并且在第一行中使用管道(如为命令行任务指定的管道)无效。

3 个答案:

答案 0 :(得分:3)

您可以使用竖线字符(literal Block Scalar Indicator)来定义带有换行符的多行文本块,例如内联脚本;例如这样的

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.
      Write-Host "Hello world"
      Write-Host "Hullo clouds"
      Write-Host "Hullo sky"

答案 1 :(得分:2)

可以像这样使用powershell任务:

# Job definition etc
steps:
  - powershell: |
      Write-Host A
      Write-Host B
      Write-Host C
  - task: AzureRmWebAppDeployment@4
      # The rest of this task is omitted.

如果您使用powershell而不是task: PowerShell@2,则目标类型默认为inline,并且无需再次设置。

答案 2 :(得分:0)

可以chain PowerShell command using semicolon。因此,实际上是在一行上用分号隔开了几个命令。

(请注意Azure Pipelines中的5000 characters line limit。)