Forum Discussion
Passing a here-string as adhoc Output Variable
- Sep 17, 2023
In Azure DevOps pipelines, when you set a variable using the `Write-Host` command as you've shown in your example, the variable is set to the value until the first newline character. To set a multi-line variable, you can use a different approach. You can use the `Write-Host` command with multiple `##vso[task.setvariable]` commands to set the variable line by line.
Here's an example of how you can set a multi-line variable in PowerShell:
$lines = @( "This is some text that I want to send", "to another task in my pipeline.", "This is another line of text." ) foreach ($line in $lines) { Write-Host "##vso[task.setvariable variable=scriptOutputMsg;isOutput=true]$line" }
This PowerShell script creates an array of lines and then iterates through the lines, setting the `scriptOutputMsg` variable one line at a time. The `isOutput=true` parameter is used to indicate that this is an output variable.
In your downstream tasks, you can access this multi-line variable as `$(scriptOutputMsg)` and it will retain all the lines you've set in the loop.
In Azure DevOps pipelines, when you set a variable using the `Write-Host` command as you've shown in your example, the variable is set to the value until the first newline character. To set a multi-line variable, you can use a different approach. You can use the `Write-Host` command with multiple `##vso[task.setvariable]` commands to set the variable line by line.
Here's an example of how you can set a multi-line variable in PowerShell:
$lines = @(
"This is some text that I want to send",
"to another task in my pipeline.",
"This is another line of text."
)
foreach ($line in $lines) {
Write-Host "##vso[task.setvariable variable=scriptOutputMsg;isOutput=true]$line"
}
This PowerShell script creates an array of lines and then iterates through the lines, setting the `scriptOutputMsg` variable one line at a time. The `isOutput=true` parameter is used to indicate that this is an output variable.
In your downstream tasks, you can access this multi-line variable as `$(scriptOutputMsg)` and it will retain all the lines you've set in the loop.