Ola Eldøy
Ola Eldøy

Reputation: 5936

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.

Upvotes: 32

Views: 47478

Answers (3)

Martin Brown
Martin Brown

Reputation: 25310

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.

Upvotes: 10

Vince Bowdren
Vince Bowdren

Reputation: 9208

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"

Upvotes: 57

Ola Eldøy
Ola Eldøy

Reputation: 5936

It's possible to chain PowerShell command using semicolon. So in effect writing several commands on one line, separated by semicolon.

(Be aware of the 5000 characters line limit in Azure Pipelines.)

Upvotes: 3

Related Questions