Powershell 如何获取异常的名称

简介:

Powershell里面一般处理异常分为中断类型和不可中断类型。前者一般是通过try..catch..finally处理,后者一般通过ErrorAction, ErrorVariable处理。


通过try..catch处理的时候有一个问题就是catch后面跟的异常,他的名字到底怎么获取的?


比如说我执行,他会报错,因为 nnnnn这个命令不存在。

1
2
3
4
5
6
7
PS C:\> a=nnnnn
a=nnnnn : The term  'a=nnnnn'  is not recognized as the name of a cmdlet,  function , script file, or operable program. Check the spelling of the name, or  if  a path was included, verify that the path is correct and try again.
At line:1 char:1
+ a=nnnnn
+ ~~~~~~~
     + CategoryInfo          : ObjectNotFound: (a=nnnnn:String) [], CommandNotFoundException
     + FullyQualifiedErrorId : CommandNotFoundException


如果我想使用try ..catch捕获这个异常,如何知道这个异常的具体名字是什么?

可以通过$error这个变量来获取,最新的报错就是 $error[0], 通过他可以知道具体的Exception是什么,这样就可以有的放矢了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
PS C:\>  $Error [0] | fl *  -f
PSMessageDetails      : 
Exception             : System.Management.Automation.CommandNotFoundException: The term  'a=nnnnn'  is not recognized as the name of a cmdlet,  function , script file, or operable program. Check the spelling of the name, or  if 
                         path was included, verify that the path is correct and try again.
                            at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandTypes commandTypes, SearchResolutionOptions searchResolutionOptions, CommandOrigin commandOrigin, 
                         ExecutionContext context)
                            at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)
                            at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)
                            at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, 
                         ExecutionContext context)
                            at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] 
                         commandRedirections, FunctionContext funcContext)
                            at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
                            at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject          : a=nnnnn
CategoryInfo          : ObjectNotFound: (a=nnnnn:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}



比如这样就行了


1
2
3
4
5
6
7
PS C:\> 
try{
a=nnnnn
}catch  [System.Management.Automation.CommandNotFoundException] {
"error1"
}
error1






本文转自 beanxyz 51CTO博客,原文链接:http://blog.51cto.com/beanxyz/1843821,如需转载请自行联系原作者

目录
相关文章
完美解决->“pip : 无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。”
完美解决->“pip : 无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。”
完美解决->“pip : 无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。”
|
8月前
如何使用PowerShell批量删除注册表项
如何使用PowerShell批量删除注册表项呢?
122 0
|
PHP
php : 无法将“php”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
php : 无法将“php”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
1828 0
php : 无法将“php”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
|
11月前
|
C++
VS Code 用户自定义配置推荐 #52
VS Code 用户自定义配置推荐 #52
58 0
|
Windows 开发工具
UWP项目生成错误: 未能使用“CompileXaml”任务的输入参数初始化该任务。“CompileXaml”任务不支持“PlatformXmlDir”参数。请确认该参数存在于此任务中,并且是可设置的公共实例属性。
项目属性: 目标版本 16299  最低版本 14393   解决方法:目标版本 15063 最低版本 14393   The issue is a bug in the Windows SDK that is causing an incompatible MSBuild tasks as...
1444 0

热门文章

最新文章

相关实验场景

更多