Powershell 如何获取异常的名称

发布时间:2020-07-15 06:32:03 作者:beanxyz
来源:网络 阅读:1081

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


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


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

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是什么,这样就可以有的放矢了。

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 a 
                        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 : {}



比如这样就行了


PS C:\> 
try{
a=nnnnn
}catch [System.Management.Automation.CommandNotFoundException]{
"error1"
}
error1


推荐阅读:
  1. 使用PowerShell操作Windows服务的命令小结
  2. PowerShell对象——PowerShell三分钟(五)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

error powershell exception

上一篇:关于Objective-C 对象release操作的一个小问题探讨

下一篇:用户转化率的七大关键因素,突飞猛进不再是梦

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》