locked
Why does a call to a module within a foreach loop seem to run separately while the next command in the script continues to run? RRS feed

  • General discussion

  • This short script will output all $place values before any Instances are printed in the console from the previous command.

    Import-Module AWSPowerShell

    $allregions = (Get-EC2Region).RegionName
    foreach ($place in $allregions){
       (Get-EC2Instance -Region $place).Instances
        write-host $place
    }

    • Changed type jrv Wednesday, May 8, 2019 3:47 PM It became a discussion
    Wednesday, May 1, 2019 5:18 PM

All replies

  • Write-Host writes directly to the console and NOT to the pipeline.  Console output always comes out first.

    This is normal behavior and cannot be changed. 

    This will work:

    $allregions = (Get-EC2Region).RegionName
    foreach ($place in $allregions) {
        [pscustomobject]@{
            Place = $place
            Instances = (Get-EC2Instance -Region $place).Instances
        }
    }

    Or this:

    $allregions = (Get-EC2Region).RegionName
    foreach ($place in $allregions) {
        (Get-EC2Instance -Region $place).Instances
        Write-Output $place
    }
    


    \_(ツ)_/

    Wednesday, May 1, 2019 5:33 PM
  • To quote Don Jones, "Every time you use Write-Host, God kills a puppy."

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, May 1, 2019 6:25 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Wednesday, May 8, 2019 9:09 AM
  • @jsnover
    "With PowerShell v5 Write-Host no longer "kills puppies". data is captured into info stream"

    12:49 PM · May 4, 2016

    https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Write-Information?view=powershell-5.1

    PS C:\Users\js2010> write-host hi 6>info
    PS C:\Users\js2010> cat info
    hi

    • Edited by JS2010 Wednesday, May 8, 2019 2:47 PM
    Wednesday, May 8, 2019 2:29 PM
  • Well, I'll be darned!

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, May 8, 2019 2:48 PM
  • Well, I'll be darned!

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    It still does not change the behavior of Write-Host in a pipeline.  It does when interleaving Write-Host between a list of commands.  The cosole will still get the output from write-host before any aggregate  pipeline output.

    The big plus is that we can redirect Write-Host to a file which was never available before WMF 5.

    I always read release notes carefully.  In this case I skipped the WMF 5 release notes and only read the WMF 5.1 release notes which did not have this info. 

    ALWAYS read all of the release nots.  I am going to print a big sign to remind myself of this and post it on the wall in front of me.  It is interesting that one of the few times I didn't read the notes I missed some really good changes.  Makes me feel like a newbie.


    \_(ツ)_/

    Wednesday, May 8, 2019 3:04 PM
  • I dunno. It looks like it puts stuff in the expected order in this (very simple) example:

    1..5 | ForEach {$_ ; Write-Host $_ -ForegroundColor Red; "-----"}

    And in this one, too (outside a loop):

    1
    Write-Host 1 -ForegroundColor Red
    "-----"
    2
    Write-Host 2 -ForegroundColor Red
    "-----"
    3
    Write-Host 3 -ForegroundColor Red
    "-----"


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, May 8, 2019 3:34 PM
  • Depends on the CmdLets used in the pipeline.  Remoting CmdLets misbehave to most.  Azure CmdLets are particularly an issue.


    \_(ツ)_/

    Wednesday, May 8, 2019 3:42 PM
  • Such as this one:

     1..5 | ForEach {$_ ; Write-Host $_ -ForegroundColor Red; "-----"} | sort

    Any CmdLet that uses aggregation or any CmdLet that remotes using a call back in any layer will not produce ordered output.  This is not bad but it will confuse many.  It is a residue of the concept of a pipelined system.

    It would be nice to add a "task" based pipeline to PowerShell.  This could be used to allow forcing of correctly interleaved console output.


    \_(ツ)_/

    Wednesday, May 8, 2019 3:46 PM