locked
Output Behavior / reading from XML vs select-string vs write-host RRS feed

  • Question

  • Some odd behavior - and maybe this is something simple, but I haven't been able to come up with a satisfactory google result, so here goes.

    I have a script in which I use select-string in some cases, calls to active directory, registry reads, and XML file reads.

    What I'm seeing is that the XML file read / output ends up coming out not when it's called in the script, but afterwards, amidst other data being output.

    I don't know if that makes sense - hard to share this script chunk without providing proprietary information but here goes:

    # SAMPLE XML FILE READ / OUTPUT
    if (test-path "\\$computer\path to file" -erroraction silentlycontinue)
        {
        [xml]$dbsauthconfig = (Get-Content -Path "\\$computer\d`$\Program Files\DBs\UserExit\bin\DBS.Authorization.SAPEFT.dll.config" -ErrorAction SilentlyContinue)
        Write-Host ""
        Write-Host "DBS.Authorization.SAPEFT.dll.config Settings" -ForegroundColor Green
        Write-Host "--------------------------------------------" -ForegroundColor Green
        Write-Output ($dbsauthconfig.configuration.appSettings.ChildNodes | Where-Object {$_.Key -like "DefaultState"})
        }
    else
        {
        write-host "FILE NOT PRESENT ACTION SKIPPED" -ForegroundColor Yellow
        }
    
    # THIS IS FOLLOWED BY, FOR INSTANCE IN THE CODE (OBFUSCATED)
    # NON-XML FILE CHECK
        Write-Host ""
        Write-Host "CLIENT.CONF" -ForegroundColor Green
        Write-Host "-------------" -ForegroundColor Green
        if (Test-Path -Path "\\$computer\path to file\filename.txt" -ErrorAction SilentlyContinue)
            {
                (Select-String -Path "\\$computer\path to file\filename.txt" -Pattern "for stores").Line
                }
            else
                {
                Write-Host "FILE NOT PRESENT ACTION SKIPPED" -ForegroundColor Yellow
                }
    

    The OUTPUT for this chunk, though, has the XML information coming out in the MIDDLE or after the select-string.

    Is this something in the mechanics of how XML output is dumped vs. a select-string?

    Frustrating.  Any help or advice is appreciated.

    Tuesday, February 19, 2019 9:41 PM

Answers

  • Write-Host does direct write to the console.  Other commands output is sent to the pipeline.  Console write will nearly always show up first.


    \_(ツ)_/

    • Marked as answer by BAWrites Wednesday, February 20, 2019 2:00 PM
    Tuesday, February 19, 2019 10:49 PM

All replies

  • Write-Host does direct write to the console.  Other commands output is sent to the pipeline.  Console write will nearly always show up first.


    \_(ツ)_/

    • Marked as answer by BAWrites Wednesday, February 20, 2019 2:00 PM
    Tuesday, February 19, 2019 10:49 PM
  • Test-Path does not respond to or need "erroraction silentlycontinue" since it only ever returns true/false. This is standard behavior for most "Test-" commands.


    \_(ツ)_/

    Tuesday, February 19, 2019 10:51 PM
  • Thanks!  I'll have to experiment a little more with the code then :)
    Wednesday, February 20, 2019 2:00 PM
  • Thanks again - less is more!
    Wednesday, February 20, 2019 2:01 PM