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.

FMZhXY7wyet9au3Xb,6eueJbmTR5QqsnY0dJ1dhM7KIoob4Fa 1uB70j6iue,xW XvJg0zEDxZIp7 INRBzL BE7m8J T0dUmaVdLgH
qKVQ3IIV4azvb0HsCbTgtUzOPp,TBf,6IvcqXyndjMynTYx DKbj anFX GRqNXoG MzDl Ej,g,4

Popular posts from this blog

Rothschild family

Cinema of Italy