PowerShell-Docs | The official PowerShell documentation sources | Command Line Interface library
kandi X-RAY | PowerShell-Docs Summary
kandi X-RAY | PowerShell-Docs Summary
The official PowerShell documentation sources
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of PowerShell-Docs
PowerShell-Docs Key Features
PowerShell-Docs Examples and Code Snippets
Community Discussions
Trending Discussions on PowerShell-Docs
QUESTION
The following is not an actual question but a cautionary tale about some unexpected PowerShell syntax. The only real question is "Is this behaviour well known to many or only by a few PowerShell developers (i.e. those working ON PowerShell not just WITH PowerShell)?" Note: the examples are only to demonstrate the effect and do not represent meaningful code (no need to ask what the purpose is).
While playing with a PowerShell (5.1.18362.145) switch
statement, I received the following error,
ANSWER
Answered 2022-Jan-14 at 15:17As - perhaps unfortunate - syntactic sugar, PowerShell allows you to shorten:
QUESTION
PowerShell 5.1
I've written a wrapper script that launches another script, logs its output (via Start-Transcript), sends an email if it exited with a non-zero code, and finally rotates the log file based on file size.
I only want to catch errors that would cause the called script to exit when executed on its own. (Script-terminating term based on the discussion here: https://github.com/MicrosoftDocs/PowerShell-Docs/issues/1583).
...ANSWER
Answered 2021-Aug-30 at 12:14A try { ... } catch { ... }
statement makes no distinction between a statement-terminating (Test-Path -Foo bar
) and a script-terminating error (throw "Error"
): both sub-types of terminating errors trigger the catch
block.
In other words: Any intrinsically statement-terminating error, such as 1 / 0
or Test-Path -Foo bar
(an invocation-syntax error, because there is no such parameter) will invariably also trigger your catch
block.
So will any non-terminating error that is promoted to a script-terminating one, either with $ErrorActionPreference = 'Stop'
or -ErrorAction Stop
(which is what you want).
- As an aside:
$ErrorActionPreference = 'Stop'
- unlike-ErrorAction Stop
- will also promote statement-terminating errors to script-terminating ones, which is just one of the problematic aspects of PowerShell's surprisingly complex error handling, discussed in detail in the GitHub docs issue linked to in your question.
Related information (which won't solve your problem):
With respect to post-mortem error analysis, the best you can do to analyze what kind of error occurred is to check if it was a script-terminating one triggered by a throw
statement, by examining $_.Exception.WasThrownFromThrowStatement
, but you won't generally be able to distinguish between a statement-terminating error, a non-terminating error, and a non-terminating error promoted to a script-terminating one.
GitHub issue #4781 discusses a potential future improvement for detecting the intrinsic vs. effective "fatality" of an error.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install PowerShell-Docs
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page