locked
Wierd little thing about the order in which my script displays its output RRS feed

  • Question

  • When running this active directory related script (below), the text "script is complete" appears before the actual output. My question is, why? PS is essentially a serial or top down language so the results of Get-ADGroupMember should be displayed before the text "script is complete" I would think

    Script

    cls
    
    Get-ADGroupMember -Identity "myADGroup" | select name, objectclass
    
    write-host ""
    write-host "script is complete"
    

    Output

    script is complete
    name   objectclass
    ----   -----------
    user1 user       
    user2 user       
    
    
    
    
    PS F:\>

    Tuesday, November 13, 2018 1:32 PM

All replies

  • Write-Host only outputs to the console .  All other output goes to the pipeline before appearing at the console.


    \_(ツ)_/

    Tuesday, November 13, 2018 1:56 PM
  • As jrv mentioned the Get-AdGroupMember output is going to the pipeline, but you can redirect it to console with Out-Host.

    Get-ADGroupMember -Identity "myADGroup" | select name, objectclass | Out-Host
    
    write-host ""
    write-host "script is complete"

    Tuesday, November 13, 2018 2:06 PM
  • As jrv mentioned the Get-AdGroupMember output is going to the pipeline, but you can redirect it to console with Out-Host.

    Get-ADGroupMember -Identity "myADGroup" | select name, objectclass | Out-Host
    
    write-host ""
    write-host "script is complete"

    Which will not solve the issue when script is executed as a PS1.  You cannot alter this behavior.


    \_(ツ)_/

    Tuesday, November 13, 2018 2:13 PM
  • What if you used write-output instead?

    • Proposed as answer by ComputerScott Monday, November 26, 2018 9:29 PM
    Tuesday, November 13, 2018 2:22 PM
  • What if you used write-output instead?



    It would be in order but included in any objects you output. 

    \_(ツ)_/


    • Edited by jrv Tuesday, November 13, 2018 3:22 PM
    Tuesday, November 13, 2018 3:22 PM
  • As PS1 then:

    Start-Job -Name ADQuery -Scriptblock {
    Get-ADGroupMember -Identity "mygroup"| select name, objectclass | Out-Host} | Out-Null
    
    Wait-Job -name ADQuery | Out-Null
    Receive-Job -name ADQuery
    Remove-Job -name ADQuery
    
    write-host ""
    write-host "script is complete"

    Wednesday, November 14, 2018 1:05 PM
  • As PS1 then:

    Start-Job -Name ADQuery -Scriptblock {
    Get-ADGroupMember -Identity "mygroup"| select name, objectclass | Out-Host} | Out-Null
    
    Wait-Job -name ADQuery | Out-Null
    Receive-Job -name ADQuery
    Remove-Job -name ADQuery
    
    write-host ""
    write-host "script is complete"

    Very interesting but what does it have to do with the purpose of the question?   Yes I know that it prevents the code from continuing but the question was "why" and not "how to fix it".


    \_(ツ)_/

    Wednesday, November 14, 2018 1:33 PM
  • As PS1 then:

    Start-Job -Name ADQuery -Scriptblock {
    Get-ADGroupMember -Identity "mygroup"| select name, objectclass | Out-Host} | Out-Null
    
    Wait-Job -name ADQuery | Out-Null
    Receive-Job -name ADQuery
    Remove-Job -name ADQuery
    
    write-host ""
    write-host "script is complete"

    Very interesting but what does it have to do with the purpose of the question?   Yes I know that it prevents the code from continuing but the question was "why" and not "how to fix it".


    \_(ツ)_/

    "Which will not solve the issue when script is executed as a PS1.  You cannot alter this behavior."

    *shrugh*

    Wednesday, November 14, 2018 3:24 PM
  • Is that tickermouse language?  Can you translate?

    \_(ツ)_/

    Wednesday, November 14, 2018 3:28 PM
  • As someone else commented the other day - you are not a suitable moderator.
    Wednesday, November 14, 2018 5:23 PM
  • Its a joke.  We call MCSEs mouses because then letters look like they are short for mouse.

    Lighten up.   Your example works but it does not address the question asked.  I just wanted you to understand that.


    \_(ツ)_/

    Wednesday, November 14, 2018 5:30 PM
  • What if you used write-output instead?

    I tested this and found it does make a difference. 
    Monday, November 26, 2018 9:31 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.

    Tuesday, November 27, 2018 2:38 AM