【问题标题】:How do I write an inline multiline powershell script in an Azure Pipelines PowerShell task?如何在 Azure Pipelines PowerShell 任务中编写内联多行 powershell 脚本?
【发布时间】:2019-10-14 08:21:18
【问题描述】:

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

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

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

【问题讨论】:

    标签: powershell azure-pipelines


    【解决方案1】:

    您可以使用竖线字符(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"
    

    【讨论】:

      【解决方案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,您无需再次设置。

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

        • 感谢分享解决方案,您可以接受它作为答案,这对阅读此主题的其他社区成员可能会有所帮助。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-01-25
        • 2017-10-14
        • 2020-10-05
        • 2017-06-14
        • 2020-12-09
        • 2021-06-30
        • 1970-01-01
        相关资源
        最近更新 更多