none
powershell running out of order RRS feed

  • Question

  • Import-Module activedirectory
    $opt = read-Host 'Would like to see a list of DNs from which to choose? (y/n)'
    switch ($opt)
    {
    y{Get-ADOrganizationalUnit -Filter 'Name -like "*"' | FT Name, DistinguishedName -A}
    n{}
    }
    
    Write-Host 'Enter the full DN of the OU you want to scan (for users): ' -ForegroundColor green
    $dn = Read-Host "example: ou=company,dc=domain,dc=local"
    get-aduser -SearchBase "$dn" -filter * -property * | select name, lastlogondate | Sort-Object lastlogondate 
    Write-Host 'there are your users'

    This runs fine, but if the read-host answer is Y, then the last line "write-host 'there are your users'" is outputted BEFORE the results are.  However, if the answer is N, then the last line is outputted last. What is wrong with my script that inputting a Y makes the script process out of order?


    • Edited by SlickUSA Tuesday, March 18, 2014 7:29 PM
    Tuesday, March 18, 2014 7:28 PM

Answers

  • Default pipeline output is a bit odd when you try to run multiple commands like that (and mix in Write-Host as well). You can tweak that behavior by explicitly piping your Get-ADOrganizationalUnit and Get-ADUser pipelines to Out-Host, instead of allowing them to go to Out-Default when the whole block of code is run.
    • Marked as answer by SlickUSA Thursday, March 20, 2014 6:17 PM
    Tuesday, March 18, 2014 8:58 PM
  • Not exactly sure, but I have found anomalies similar to this when mixing output between explicit write-host statements and just letting a pipeline spew its output onto the screen. I think there seems to be some kind of expectation that objects emitted to the pipeline should have the same shape, otherwise the output of the get-process command would not change from a table style to a list style when preceded by output from a different command, i.e.:

    PS M:\> get-process | select -first 5
    
    Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
    -------  ------    -----      ----- -----   ------     -- -----------
         76       8     1232       3960    42            4492 armsvc
       1163      41    23116      37652   151            1964 CcmExec
       1583      69    44972      16616   517            1864 ccSvcHst
        298      27     6140       3232    93     0.23   6920 ccSvcHst
         43       6     2268       5520    54     0.12   3676 conhost
    
    
    PS M:\> get-eventlog 'application' | select -first 5 ; get-process | select -first 5
    
       Index Time          EntryType   Source                 InstanceID Message
       ----- ----          ---------   ------                 ---------- -------
        8535 Mar 18 14:28  Error       Application Hang             1002 The program iexplore.exe version 9.0.8112.16533...
        8534 Mar 18 14:28  Information Windows Error Rep...         1001 Fault bucket , type 0...
        8533 Mar 18 10:13  0           Software Protecti...   1073742727 The Software Protection service has stopped....
        8532 Mar 18 10:13  Information Software Protecti...   1073758208 Successfully scheduled Software Protection serv...
        8531 Mar 18 10:08  0           Software Protecti...   1073742726 The Software Protection service has started....
    
    Id      : 4492
    Handles : 76
    CPU     :
    Name    : armsvc
    
    Id      : 1964
    Handles : 1163
    CPU     :
    Name    : CcmExec
    
    Id      : 1864
    Handles : 1583
    CPU     :
    Name    : ccSvcHst
    
    Id      : 6920
    Handles : 298
    CPU     : 0.2340015
    Name    : ccSvcHst
    
    Id      : 3676
    Handles : 43
    CPU     : 0.1404009
    Name    : conhost
    
    PS M:\>

    I think the answer lies in not expecting these various types of output to work as consistently as you might expect.


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    • Marked as answer by SlickUSA Thursday, March 20, 2014 6:15 PM
    Tuesday, March 18, 2014 9:04 PM

All replies

  • I've also tried it using IF statement, no dice this way either...

    $opt = read-Host 'Would like to see a list of DNs from which to choose? (y/n)'
    
    if ($opt -eq 'y')
        {Get-ADOrganizationalUnit -Filter 'Name -like "*"' | FT Name, DistinguishedName -A}
    else 
        {}
    
    Write-Host 'From the list above, enter the full DN of the OU you want to scan (for users): ' -ForegroundColor green
    $dn = Read-Host "example: ou=company,dc=domain,dc=local"
    get-aduser -SearchBase "$dn" -filter * -property * | select name, lastlogondate | Sort-Object lastlogondate 
    Write-Host 'there are your users'

    Tuesday, March 18, 2014 7:49 PM
  • Default pipeline output is a bit odd when you try to run multiple commands like that (and mix in Write-Host as well). You can tweak that behavior by explicitly piping your Get-ADOrganizationalUnit and Get-ADUser pipelines to Out-Host, instead of allowing them to go to Out-Default when the whole block of code is run.
    • Marked as answer by SlickUSA Thursday, March 20, 2014 6:17 PM
    Tuesday, March 18, 2014 8:58 PM
  • Not exactly sure, but I have found anomalies similar to this when mixing output between explicit write-host statements and just letting a pipeline spew its output onto the screen. I think there seems to be some kind of expectation that objects emitted to the pipeline should have the same shape, otherwise the output of the get-process command would not change from a table style to a list style when preceded by output from a different command, i.e.:

    PS M:\> get-process | select -first 5
    
    Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
    -------  ------    -----      ----- -----   ------     -- -----------
         76       8     1232       3960    42            4492 armsvc
       1163      41    23116      37652   151            1964 CcmExec
       1583      69    44972      16616   517            1864 ccSvcHst
        298      27     6140       3232    93     0.23   6920 ccSvcHst
         43       6     2268       5520    54     0.12   3676 conhost
    
    
    PS M:\> get-eventlog 'application' | select -first 5 ; get-process | select -first 5
    
       Index Time          EntryType   Source                 InstanceID Message
       ----- ----          ---------   ------                 ---------- -------
        8535 Mar 18 14:28  Error       Application Hang             1002 The program iexplore.exe version 9.0.8112.16533...
        8534 Mar 18 14:28  Information Windows Error Rep...         1001 Fault bucket , type 0...
        8533 Mar 18 10:13  0           Software Protecti...   1073742727 The Software Protection service has stopped....
        8532 Mar 18 10:13  Information Software Protecti...   1073758208 Successfully scheduled Software Protection serv...
        8531 Mar 18 10:08  0           Software Protecti...   1073742726 The Software Protection service has started....
    
    Id      : 4492
    Handles : 76
    CPU     :
    Name    : armsvc
    
    Id      : 1964
    Handles : 1163
    CPU     :
    Name    : CcmExec
    
    Id      : 1864
    Handles : 1583
    CPU     :
    Name    : ccSvcHst
    
    Id      : 6920
    Handles : 298
    CPU     : 0.2340015
    Name    : ccSvcHst
    
    Id      : 3676
    Handles : 43
    CPU     : 0.1404009
    Name    : conhost
    
    PS M:\>

    I think the answer lies in not expecting these various types of output to work as consistently as you might expect.


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    • Marked as answer by SlickUSA Thursday, March 20, 2014 6:15 PM
    Tuesday, March 18, 2014 9:04 PM