Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to suppress the "extra" output from Write-Error cmdlet?

Tags:

powershell

Looked for this but cannot find a resolution. By "extra" output, I mean the extra text that the Write-Output emits after your error message text. i.e.:

write-error -Message "test"

produces:

write-error -Message "test" : test
+ CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

I would like to only see the text "test", and retrieve it from stderr via a Process object that runs the PS script.

I know a complicated regex (to catch ALL the potential chars that could appear in the category, etc.) could be developed, but I want to avoid that. Don't forget that Write-Error also injects a newline char at every 80th char position due to console, so that would have to be factored in as well.

Is there a way to tell Powershell (2.0) to not be so wordy when writing to stderr and write only the message part?

like image 866
joebalt Avatar asked Jul 12 '12 19:07

joebalt


People also ask

How do I check for errors in PowerShell script?

Use the try block to define a section of a script in which you want PowerShell to monitor for errors. When an error occurs within the try block, the error is first saved to the $Error automatic variable. PowerShell then searches for a catch block to handle the error.

What are PowerShell errors?

An error in PowerShell is a message that's displayed on screen when something goes wrong. By default, PowerShell displays its errors in red text on a black background (in the console host, that is; the Integrated Scripting Environment (ISE) uses red text).

How do I print a value in PowerShell?

The echo command is used to print the variables or strings on the console. The echo command has an alias named “Write-Output” in Windows PowerShell Scripting language. In PowerShell, you can use “echo” and “Write-Output,” which will provide the same output.


2 Answers

I think isn't possible limit the output of an error but you can workaround like this:

$Host.UI.WriteErrorLine("My custom error")
like image 74
CB. Avatar answered Oct 12 '22 10:10

CB.


$Host.UI.WriteErrorLine doesn't update the $error variable:

PS> $error.Clear()
PS> $Host.UI.WriteErrorLine("My custom error")
My custom error

PS> $Error.Count
0

You should use the Write-Error cmdlet but the output is still verbose:

PS> Write-Error "My custom error"
Write-Error "My custom error" : My custom error
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

There's an option to output less verbose message with the built-in $ErrorView variable, its default value is 'Normal' (verbose). You can set it to "CategoryView" for brief messages.

PS> $ErrorView="CategoryView"
PS> Write-Error "My custom error"
NotSpecified: (:) [Write-Error], WriteErrorException
like image 24
Shay Levy Avatar answered Oct 12 '22 10:10

Shay Levy