I have a simple workflow for running some commands within a parallel foreach loop, but cannot figure out how to print to the console from within the loop.
$names = @('foo', 'bar', 'bat')
Workflow Test-Print
{
foreach -Parallel ($name in $names)
{
# execute some commands on #name
(InlineScript {Write-Host "Hello $name";}) # doesn't print
Write-Output "Hello $name" # doesn't print
Write-Host "Hello $name" # error
Write-Information "Hello $name" # doesn't print
Write-Verbose "Hello $name" # doesn't print, even with -Verbose flag
}
}
Test-Print -Verbose
This works as designed. Note just the minor change I made.
If I add back in all you had. You can see that of the 6 options, only 4 will be successful.
In general practice, Write-Host should just be avoided, except when absolutely needed. Think color, other formatting particulars that won't work without it. Also, as you'll note, if you run this, the "`n" are also not being processed in the WorkFlow, wherein normal cases, it would be.
Now, this way, we get a bit better rendering and commenting out the problem children.
Now, back to your original post and this small change: