How can I install powershell script to show only short error descriptions

Is there a way to get only an error message when displaying error descriptions when running a script?

I have a script that runs, and when an error occurs, I get error messages at different times, for example, if I do not pass the parameter to the function that I wrote, I will get:

    No Setup Location specified (i.e. \\server\share\folder)
At \\server\share\folder\script.ps1:9 char:31
+     [string] $setupHome = $(throw <<<<  "No Setup Location specified (i.e. \\server\share\folder)")
    + CategoryInfo          : OperationStopped: (No Setup Locati...ojects\something):String) [], RuntimeException
    + FullyQualifiedErrorId : No Setup Location specified (i.e. \\server\share\folder)

This is good, but I would like to set a preference (for example, ErrorAction), which will show me an error message, and not all the extra cream that is nice to have but clutters up my console. Therefore, instead of the foregoing, I would like to see only the first line:

No Setup Location specified (i.e. \\server\share\folder)
+4
source share
3 answers

$ErrorView 'CategoryView', :

$ErrorView = 'CategoryView'
Get-Item foo

ObjectNotFound: (C:\foo:String) [Get-Item], ItemNotFoundException

'NormalView', :

$ErrorView = 'NormalView'

Get-Item foo

ObjectNotFound: (C:\foo:String) [Get-Item], ItemNotFoundException
Get-Item : Cannot find path 'C:\foo' because it does not exist.
At line:8 char:1
+ Get-Item foo
+ ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\foo:String) [Get-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
+5

trap, script, - , , -

trap {
    Write-Host $Error[0].PSMessageDetails
    continue;
}
+1

Paste the code into the try / catch block, and in the catch block print the first line contained in the $ error variable, with $error.Exception

+1
source

Source: https://habr.com/ru/post/1584020/


All Articles