-ErrorAction update

-ErrorAction update

  • Comments 3

Responding to  my blog entry on -ErrorAction HERE,  Jon Newman reminded me that the reason we didn't use the term IGNORE for -ErrorAction was that we don't really ignore the error.  We still record it in $ERROR.

He also reminded me that I should have talked about $ErrorActionPreference.  This variable defines the action that will occur if the user does NOT specify what to do on the commandline.  Check it out:

PS>$ErrorActionPreference
Continue
PS>Stop-Process 13,23
Stop-Process : Cannot find a process with the process identifier 13.
At line:1 char:13
+ Stop-Process <<<<  13,23
Stop-Process : Cannot find a process with the process identifier 23.
At line:1 char:13
+ Stop-Process <<<<  13,23
PS>$ErrorActionPreference ="SilentlyContinue"
PS>Stop-Process 13,23
PS>$ErrorActionPreference ="Stop"
PS>Stop-Process 13,23
Stop-Process : Command execution stopped because the preference variable "ErrorActionPreference" or common parameter i
set to Stop: Cannot find a process with the process identifier 13.
At line:1 char:13
+ Stop-Process <<<<  13,23
PS>$ErrorActionPreference ="Inquire"
PS>Stop-Process 13,23

Confirm
Cannot find a process with the process identifier 13.
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"): h
Stop-Process : Command execution stopped because the user selected the Halt option.
At line:1 char:13
+ Stop-Process <<<<  13,23

Enjoy!

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 6 and 6 and type the answer here:
  • Post
  • Messages “Stop-Process : Command execution stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Cannot find a process with the process identifier 13.” look ugly with “Command execution stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop”. The real message should be in the first place, then “Command execution stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop” may come if you like. “Command execution stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop” makes it harder to find needed information :)

  • When ErrorActionPreference is set to Inquire then information about error source (i.e. + ...\script.ps1 At line:... char:...) has to be included into the Inquire message. In fact, my decision often completely depends on where exactly an error is.

  • ErrorActionPreference I read if set to "stop" Trap will always be called. this does not seem to be true. it seems that the script or shell halts at the error and trap is not called

Page 1 of 1 (3 items)