Powershell: How can I stop display errors in a script?

When my PowerShell script tries, for example, to create a SQL Server object for a server that does not exist ("bla" in my case), PowerShell displays many PowerShell errors in red.

So how $? my script check the value of $? after such calls and displays and logs errors, I would prefer not to show a few lines of PowerShell errors either.

How to disable the ones that are displayed for my script?

+65
powershell error-handling
Dec 05 2018-11-12T00:
source share
6 answers

You have several options. The simplest is to use the ErrorAction settings.

-Erroraction is a universal parameter for all cmdlets. If there are special commands that you want to ignore, you can use -Erroraction 'silentlycontinue' which basically ignores all error messages generated by this command. You can also use the Ignore value (in PowerShell 3+):

Unlike SilentlyContinue, Ignore does not add an error message to the $ Error automatic variable.

If you want to ignore all errors in the script, you can use the $ErrorActionPreference system variable and do the same: $ErrorActionPreference= 'silentlycontinue'

See About_CommonParameters for more information on -Erroraction. See about_preference_variables for more information on $ ErrorActionPreference.

+99
Dec 05 2018-11-12T00:
source share

Windows PowerShell provides two mechanisms for reporting errors: one mechanism for interrupting errors and another mechanism for non-termination errors.

CmdLets internal code can call the ThrowTerminatingError method when an error occurs that prevents or prevents the cmdlet from continuing to process its input objects. The creator of the script may use an exception to catch this error.

EX:

 try { Your database code } catch { Error reporting/logging } 

The CmdLets internal code may call the WriteError method to report invalid errors when the cmdlet can continue to process input objects. A script can use the -ErrorAction parameter to hide messages, or use $ErrorActionPreference to configure all script behavior.

+15
Dec 05 '11 at 17:26
source share

I had a similar problem when trying to resolve [system.net.dns] names using [system.net.dns] . If the IP address was not resolved .Net made a completion error. To prevent a final error and maintain control over the output, I created a function using TRAP .

eg.

 Function Get-IP {PARAM ([string]$HostName="") PROCESS {TRAP {"" ;continue} [system.net.dns]::gethostaddresses($HostName) } } 
+6
Dec 06 '11 at 8:16
source share

You are doing it wrong.

You already have a nice, big error message. Why on earth would you like to write code that checks $? obviously after each team? This is extremely cumbersome and error prone. Correct solution - stop checking $? ,

Instead, use the built-in PowerShell engine to blow you up. You turn it on, setting the error preference to the highest level:

 $ErrorActionPreference = 'Stop' 

I put this at the beginning of every script that I have ever written, and now I do not need to check $? This makes my code a lot easier and more reliable.

If you are faced with situations where you really need to disable this behavior, you can either catch error or pass the configuration of a specific function using the common -ErrorAction . In your case, you probably want your process to stop at the first error, catch the error, and then write it down.

Please note that this does not handle the case when external executables stop working (the completion code is non-zero, usually), so you still need to check $LASTEXITCODE if you call any of them. Despite this limitation, customization still saves a lot of code and effort.

Extra reliability

You may also consider using strict mode :

 Set-StrictMode -Version Latest 

This prevents PowerShell from running quietly when using a nonexistent variable and in other strange situations. (See -Version for details on what it restricts.)

The combination of these two settings makes PowerShell a much more fault-tolerant language, which greatly simplifies programming on it.

+1
May 15 '18 at 20:40
source share

Add -ErrorAction SilentlyContinue to your script and everything will be fine.

0
Jun 12 '19 at 12:53 on
source share

If you want errsessage powershell for the cmdlet to be suppressed, but still want to catch the error, use "-erroraction" silentStop "

-one
Oct 20 '14 at 11:26
source share



All Articles