#!/usr/bin/env pwsh
1..100 | foreach {
$response= invoke-restmethod www.azuredevops.com/api/v2/somequery
Write-Output "string showing the status of CI pipeline" # i don't want this to go into pipe
cls
}
Write-Output "formated string which contains Build details in json format which will be outputted to the pipeline"
Above script will be run in both windows-powershell and linux-powershell. In this script I'll be printing both "status/progress" and "result_output" which will then be outputted to pipeline. At the moment, the status/progress output from the script is being outputted to the pipeline with desired output string. How do I limit only desired output to pipe while printing both progress and desired output to the terminal.
CodePudding user response:
Building on Mathias's helpful comment, PowerShell has built-in cmdlets to interact with it's different streams, all described in about_Output_Streams. The only stream which can be captured by default is the Success stream (Standard Output) and, most of them can be redirected to it however not relevant to the question. See about_Redirection for more info on this.
Here is a little example of what's explained above and what may give you an idea on how to approach your code.
function ReceiveFromPipeline {
param([Parameter(ValueFromPipeline)] $InputObject)
process {
"Receiving [$InputObject] from pipeline"
}
}
$result = 1..100 | ForEach-Object {
# this is Standard Output,
# will be received by ReceiveFromPipeline function
$_
# This goes to the Info Stream and will not be captured in `$result`
# unless redirected.
Write-Host "Writing [$_] to the Information Stream"
# This goes to the Progress Stream, cannot be redirected!
Write-Progress -Activity "[$_] goes to the Progress Stream"
Start-Sleep -Milliseconds 200
} | ReceiveFromPipeline