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

Ola*_*døy 14 powershell azure-pipelines

Powershell 任务的 yaml 架构允许您选择 targetType: 'inline' 并在 script: input 中定义一个脚本。

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

文档没有指定如何,并且在第一行使用管道(就像为命令行任务指定的那样)不起作用。

Vin*_*ren 22

您可以使用管道字符(文字块标量指示器)来定义带有换行符的多行文本块,例如您的内联脚本;例如像这样:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.
      Write-Host "Hello world"
      Write-Host "Hullo clouds"
      Write-Host "Hullo sky"
Run Code Online (Sandbox Code Playgroud)


Mar*_*own 9

可以像这样使用 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.
Run Code Online (Sandbox Code Playgroud)

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