Why ScriptBlock in PowerShell delays output until the ScriptBlock completes?

Multi tool use
Multi tool use


Why ScriptBlock in PowerShell delays output until the ScriptBlock completes?



I have this code:


Function-Step "Long operation" { MyLongRunningFunction }

function Function-Step
{
param([string]$message,[ScriptBlock]$block)
Write-Host $message
Read-Host
$block.Invoke()
}



MyLongRunningFunction writes information to the standard output as it runs, but it doesn't show in the console until the function finishes, suddenly.


MyLongRunningFunction



It seems that it's eating all the output of MyLongRunningFunction ends, and after that, it throws all the output at the same time.


MyLongRunningFunction



How can I avoid this behavior?





Use Jobs? That's the most straightforward way.
– TheIncorrigible1
Jul 2 at 1:01





$block.Invoke() -> & $block
– PetSerAl
Jul 2 at 3:14


$block.Invoke()


& $block





Use @PetSerAl Answer. That will keep the handle with the function itself.
– Rohin Sidharth
Jul 2 at 6:08









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

KDlYp6wp
pba0CR89JP,rB6ayKRyJQDYKi,DEVQ EtHb2x,TErt675JQ1no5,70SquWIameD

Popular posts from this blog

Boo (programming language)

Rothschild family