3

I have a function which prints the result of an Exchange cmdlet. But the output is not printed immediately. It is almost like it kind of runs asynchronous.

In the following example the output is printed AFTER the first Read-Host break. I expected it would print immediately, that is before the first Read-Host break.

I also tried to experience with Start-Sleep -S 1 but it didn't change anything.

Apparently there is something basic understanding I don't have. Can someone point me in the right direction?

Function GetMailboxFolders {
    $MBID = 15
    Get-MailboxFolderStatistics -Identity ((Get-Mailbox)[$MBID].alias) |
        Select-Object Identity, ItemsInFolder, FolderSize
    Read-Host -Prompt 'Push key to continue(1)'
    # The result of the cmdlet is printed here!?
    Read-Host -Prompt 'Push key to continue(2)'
}

If I don't add the | Select-Object ... it prints immediately/run synchronously...why?

MrCalvin
  • 1,675
  • 1
  • 19
  • 27
  • Probably something to do with the output buffer. What happens if you add `[Console]::Out.Flush()` before the first `Read-Host`? – arco444 Dec 04 '17 at 10:27
  • 4
    https://stackoverflow.com/questions/19754069/powershell-difference-between-write-host-and-write-output - your mailbox output goes to the write-output pipeline. That won't appear on screen until the relevant part of the script has finished, if ever. Write-Host and Read-Host deal with immediate, interactive writing/reading. Write-output is for everyday use, for automation, for remote scripts. Write-host is specifically if you have one human at a console reading the prompts. – TessellatingHeckler Dec 04 '17 at 10:32
  • @TessellatingHeckler Can you please add this to the answer or if you feel its a duplicate can you mark it as duplicate – Ram Dec 04 '17 at 12:35
  • Ignore my first comment....I'll try to delete it later. arco444. It didn't help to insert the ...out.flush... TessellatingHeckler I would say the solution is to force the script NOT to continue until the cmdlet is finished. Now it goes to the first Read-host before the cmdlet is finished. I tried to add both "|Write-output" and |Write-host" it didn't give the result I'm looking for. – MrCalvin Dec 04 '17 at 14:06
  • Adding `| Out-Default` or `| Out-Host` solved the problem. – MrCalvin Dec 04 '17 at 19:34
  • If you want MS to fix this problem please up-vote at [connect.microsoft.com...](https://connect.microsoft.com/PowerShell/Feedback/Details/1635172) and [windowsserver.uservoice.com...](https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/32512852-code-execution-not-synchronously-when-using-sel) – MrCalvin Dec 04 '17 at 20:18
  • There isn't anything to fix - this is part of the design of PowerShell. It isn't the same as other languages where all output goes to stdout and all input comes from stdin. Write-Output goes to the object pipeline, and that may or may not end up on screen, sometime after the pipeline finishes. If you want immediate screen display (and text only), `out-default` is forcing it. `Write-Host` does it too, but without the output formatters being involved. LaoFuZi1971's comment on your post is a bad answer - note that `$NICs` and `$showmewhatIwant` end up empty variables, and `select` does nothing – TessellatingHeckler Dec 05 '17 at 07:06
  • This behavior was introduces in PS ver. 5. Apparently an auto-format-list feature was added which use 300ms to find the best formatting of the output list.Fine, no problem! But don't break the synchronously code flow. This 300ms-auto-format-feature seem to be running asynchronously and in my view it shouldn't, or at least the primary thread should be "paused" until it finish after the 300ms. – MrCalvin Dec 05 '17 at 10:57

0 Answers0