Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write an inline multiline powershell script in an Azure Pipelines PowerShell task?

The yaml schema for a Powershell task allows you to select targetType: 'inline' and define a script in the script: input.

But what is the correct format for writing a script with more than one line?

The docs don't specify how, and using a pipe on line one (like is specified for the Command Line task) does not work.

like image 730
Ola Eldøy Avatar asked Oct 14 '19 08:10

Ola Eldøy


2 Answers

You can use the pipe character (the literal Block Scalar Indicator) to define a multi-line block of text with newline characters such as your inline script; for example like this:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.
      Write-Host "Hello world"
      Write-Host "Hullo clouds"
      Write-Host "Hullo sky"
like image 81
Vince Bowdren Avatar answered Oct 25 '22 05:10

Vince Bowdren


It is possible to just use the powershell task like this:

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

If you use powershell instead of task: PowerShell@2 the target type defaults to inline and you don't need to set it again.

like image 6
Martin Brown Avatar answered Oct 25 '22 03:10

Martin Brown