Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

VoidStar

I know this kind of feels like moving state in the "wrong direction" on the pipeline, but there are a few circumstances where this could be handy.

Is this code snippet possible?

if (Directed-To-Screen) {
    Write-Host "Error!" -ForegroundColor Red 
} else {
    Write-Output "Error!"
}    

Where Directed-To-Screen would return $false if the current code/script is being piped to a variable or file, etc. (I know [Console]::ForegroundColor\ $host.UI.RawUI.ForegroundColor can be used to color Write-Output in some consoles).

There is actually quite a bit that could be done to improve formatting if its going to be printed on the screen, whereas piped output from utility functions should probably be structured so that individual fields can be consumed. Being able to tell them apart could be pretty useful. (There isn't a way to make a PSObject override its ToString formatting so that it appears all pretty when printed to the screen, is there?)

It might not even be possible to detect if its coming out on the screen or not. Can anyone confirm either way?

Duncan

I think you are approaching this in the wrong way. Rather than using Write-Host and forcing a red foreground colour you should output an object that displays in red on the terminal but just looks like plain text when written to a file.

There already is an object that does that: any object of type System.Management.Automation.ErrorRecord will display as red if it reaches the end of the pipeline but can be written to a file. Unfortunately for your purposes it usually formats with CategoryId and FullyQualifiedErrorId fields, but it won't do that if the error object was created by a native command. We can fake that behaviour like this:

function Write-RedText
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $Text
    )

    Begin
    {
    }
    Process
    {
       foreach ($s in $Text)
       {
           #Wr$o = (Write-Error $s -ErrorId NativeCommandErrorMessage -TargetObject $None) 2>&1
           $o = new-object -TypeName "System.Management.Automation.ErrorRecord" -ArgumentList ($s, "NativeCommandErrorMessage", [System.Management.Automation.ErrorCategory]::FromStdErr, "c")
           $o | Add-Member -NotePropertyName writeErrorStream -NotePropertyValue $True
          Write-Output $o
       }
    }
    End
    {
    }
}

Now you can just do:

Write-RedText "Error!"

and it will appear in red if it reaches the host but just appears as plain text if it ends up in a file.

N.B. It is the writeErrorStream property on the object that actually makes it display in red, so it is possible to make other objects display red as well just by adding that property. e.g.

$out = ls
$out | Add-Member -NotePropertyName writeErrorStream -NotePropertyValue $True
$out

will give you a directory listing that is red on the console, or if you prefer set the WriteWarningStream property for orange text.

As to the rest of your question, I'm pretty sure there's no way to tell where the output will end up. There is an implicit | Out-Default on the end of the outermost pipeline. Out-Default formats the objects and sends them to the console, but there's no way to tell whether any objects reach that final command or if they do whether you've redefined Out-Default to do something totally different.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

From Dev

How does a Unix program know to change output if it's being piped?

From Dev

Powershell Object not being piped through to Functions

From Dev

Detect if user is directed from you website

From Dev

String is not being directed to Output file (C++)

From Dev

How to format piped output in powershell to a line

From Dev

How to detect if Node's process.stdout is being piped?

From Dev

Ping piped to txt does output nothing

From Dev

Why does `cd` have no effect if output is piped?

From Dev

ag output appears different in the terminal vs when piped to a file

From Dev

Why does bash output data instead of executing, when a script is piped?

From Dev

Why does make ignore escape sequence if the output is piped

From Dev

Why does piped FFMPEG output differ from an explicit file?

From Dev

How does a program output to a terminal when stdout is piped?

From Dev

vimscript detect piped input

From Dev

PowerShell - Show output / action on screen

From Dev

PowerShell - Show output / action on screen

From Dev

What happens if dd encounters errors while output of dd is being piped to gzip?

From Dev

Duplication of Piped output

From Dev

Output piped command to variable

From Dev

Unable to grep piped output

From Java

Why does Java let you cast to a collection?

From Dev

Why does Java let you cast to a collection?

From Dev

Android detect if Save/Discard screen is being shown after taking the picture

From Dev

powershell script create custom columns to screen output

From Dev

How to detect whether the command output is being used in unix pipe

From Dev

ls output differs stdout vs screen

From Dev

In what format does piped output get sent and received from one command/program to another?

From Dev

why does the output of `ls` look different when piped through ``tr "\n" "\n"`?

Related Related

  1. 1

    Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

  2. 2

    How does a Unix program know to change output if it's being piped?

  3. 3

    Powershell Object not being piped through to Functions

  4. 4

    Detect if user is directed from you website

  5. 5

    String is not being directed to Output file (C++)

  6. 6

    How to format piped output in powershell to a line

  7. 7

    How to detect if Node's process.stdout is being piped?

  8. 8

    Ping piped to txt does output nothing

  9. 9

    Why does `cd` have no effect if output is piped?

  10. 10

    ag output appears different in the terminal vs when piped to a file

  11. 11

    Why does bash output data instead of executing, when a script is piped?

  12. 12

    Why does make ignore escape sequence if the output is piped

  13. 13

    Why does piped FFMPEG output differ from an explicit file?

  14. 14

    How does a program output to a terminal when stdout is piped?

  15. 15

    vimscript detect piped input

  16. 16

    PowerShell - Show output / action on screen

  17. 17

    PowerShell - Show output / action on screen

  18. 18

    What happens if dd encounters errors while output of dd is being piped to gzip?

  19. 19

    Duplication of Piped output

  20. 20

    Output piped command to variable

  21. 21

    Unable to grep piped output

  22. 22

    Why does Java let you cast to a collection?

  23. 23

    Why does Java let you cast to a collection?

  24. 24

    Android detect if Save/Discard screen is being shown after taking the picture

  25. 25

    powershell script create custom columns to screen output

  26. 26

    How to detect whether the command output is being used in unix pipe

  27. 27

    ls output differs stdout vs screen

  28. 28

    In what format does piped output get sent and received from one command/program to another?

  29. 29

    why does the output of `ls` look different when piped through ``tr "\n" "\n"`?

HotTag

Archive