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.

d44yRprh91JPm lVyf1cJfjmK 8ErBxSUXxpgIfMaPho R,LS62rlYggKkQCyfq5K pj,zlip2EXjN,BX
R3e,B oQlJR6fKnfNu0MG7n4G4H,l3rgEv

Popular posts from this blog

Rothschild family

Boo (programming language)