I would like to write to console [host] as well as send the resolved value to the output channels under all circumstances
Desired Result:
==== FOOBAR ====
Input: <input>
<output>
==== END OF FOOBAR ====
The closest I've gotten to a solution is the following:
function Write-OutAndReturn {
param(
[Parameter(ValueFromPipeline = $true)]
$Value
)
process {
$ConsoleDevice = if ($IsWindows) { '\\.\CON' } else { '/dev/tty' }
Write-Host "====== SECTION ======"
Write-Host "Input: " -NoNewline
Write-Host $MyInvocation.BoundParameters.Value
$(if ($Value -is [scriptblock]) {
$Value.Invoke()
} else { $Value }) | Tee-Object -FilePath $ConsoleDevice
Write-Host "====== END SECTION ======"
}
}
Where the output of $Result = Write-OutAndReturn { 1 + 1 }
equals:
====== SECTION ======
Input: 1 + 1
2
====== END SECTION ======
Which is desired; and $Result
now contains 2
However when the function is only called (Write-OutAndReturn { 1 + 1 }
) it generates the following:
====== SECTION ======
Input: 1 + 1
2
2
====== END SECTION ======
Where the second 2
is undesired
Is there a more semantic way to handle this behavior or to ensure brevity is honored?