Powershell选择不同的异常
我正在创建一个脚本,在为文件服务器创建配额时,它需要处理所有可用的异常Powershell选择不同的异常,powershell,exception,error-handling,Powershell,Exception,Error Handling,我正在创建一个脚本,在为文件服务器创建配额时,它需要处理所有可用的异常 try { ### Create cuota ### } catch [System.Management.Automation.MethodInvocationException] { Write-Host "Exception 0x80045306" } catch [System.Management.Automation.MethodInvocationException] { Write-H
try
{
### Create cuota ###
}
catch [System.Management.Automation.MethodInvocationException]
{
Write-Host "Exception 0x80045306"
}
catch [System.Management.Automation.MethodInvocationException]
{
Write-Host " Exception 0x80045307 "
}
在这个脚本中,我有两个使用相同网络代码System.Management.Automation.MethodInvocationException标识的异常:我需要能够在代码中的两个异常之间进行选择,并为每个异常执行不同的修复任务
我的问题是谁可以选择不同的异常,以使用相同的.net代码对这两个不同的异常进行适当的错误处理
完全例外
异常:System.Management.Automation.MethodInvocationException:使用1个参数调用CreateQuota的异常:来自HRESULT的异常:0x80045306-->System.Reflection.TargetInvocationException:调用的目标已引发异常。-->System.Runtime.InteropServices.COMException 0x80045306
来自HRESULT的异常:0x80045306
-内部异常堆栈跟踪的结束--
位于System.RuntimeType.InvokeDispMethodString名称、BindingFlags invokeAttr、对象目标、对象[]参数、布尔[]byrefModifiers、Int32区域性、字符串[]namedParameters
在System.RuntimeType.InvokeMemberString名称、BindingFlags BindingFlags、Binder Binder、对象目标、对象[]提供的参数、参数修改器[]修饰符、CultureInfo区域性、字符串[]namedParams
在System.Management.Automation.CommMethod.InvokeMethodPSMethod方法中,对象[]参数
-内部异常堆栈跟踪的结束--
在System.Management.Automation.CommMethod.InvokeMethodPSMethod方法中,对象[]参数
在System.Management.Automation.ComAdapter.MethodInvokePSMethod中,对象[]参数
在System.Management.Automation.Adapter.BaseMethodInvokePSMethod中,对象[]参数
位于System.Management.Automation.ParserOps.CallMethodToken标记、对象目标、字符串methodName、对象[]paramArray、布尔callStatic、对象valueToSet
在System.Management.Automation.MethodCallNode.InvokeMethodObject目标中,对象[]参数,对象值
位于System.Management.Automation.MethodCallNode.ExecuteArray输入、管道输出管道、ExecutionContext上下文
位于System.Management.Automation.AssignmentStatementNode.ExecuteArray输入、管道输出管道、ExecutionContext上下文
位于System.Management.Automation.StatementListNode.ExecuteStatementParseTreeNode语句、数组输入、管道输出管道、ArrayList和resultList、ExecutionContext上下文
异常:System.Management.Automation.MethodInvocationException:使用0个参数调用Commit的异常:来自HRESULT的异常:0x80045307
System.Reflection.TargetInvocationException:调用的目标已引发异常。
System.Runtime.InteropServices.COMException 0x80045307:来自HRESULT的异常:0x80045307
-内部异常堆栈跟踪的结束--
位于System.RuntimeType.InvokeDispMethodString名称、BindingFlags invokeAttr、对象目标、对象[]参数、布尔[]byrefModifiers、Int32区域性、字符串[]namedParameters
在System.RuntimeType.InvokeMemberString名称、BindingFlags BindingFlags、Binder Binder、对象目标、对象[]提供的参数、参数修改器[]修饰符、CultureInfo区域性、字符串[]namedParams
在System.Management.Automation.CommMethod.InvokeMethodPSMethod方法中,对象[]参数
-内部异常堆栈跟踪的结束--
在System.Management.Automation.CommMethod.InvokeMethodPSMethod方法中,对象[]参数
在System.Management.Automation.ComAdapter.MethodInvokePSMethod中,对象[]参数
在System.Management.Automation.Adapter.BaseMethodInvokePSMethod中,对象[]参数
位于System.Management.Automation.ParserOps.CallMethodToken标记、对象目标、字符串methodName、对象[]paramArray、布尔callStatic、对象valueToSet
在System.Management.Automation.MethodCallNode.InvokeMethodObject目标中,对象[]参数,对象值
位于System.Management.Automation.MethodCallNode.ExecuteArray输入、管道输出管道、ExecutionContext上下文
位于System.Management.Automation.ParseTreeNode.ExecuteArray输入、管道输出管道、ArrayList&resultList、ExecutionContext上下文
位于System.Management.Automation.StatementListNode.ExecuteStatementParseTreeNode语句、数组输入、管道输出管道、ArrayList和resultList、ExecutionContext上下文
您可以尝试以下方法:
try {
1/0
}
catch{
$et = $_.Exception.gettype().Name
switch (et) {
'RuntimeException' : { 'runtime' }
}
}
您可以尝试以下方法:
try {
1/0
}
catch{
$et = $_.Exception.gettype().Name
switch (et) {
'RuntimeException' : { 'runtime' }
}
}
catch块可以按类区分异常:
try {
...
} catch [System.Net.WebException], [System.IO.IOException] {
# handle WebException and IOException
} catch [System.Management.Automation.MethodInvocationException] {
# handle MethodInvocationException
} catch {
# handle all other exceptions
}
他们无法区分同一类的不同异常。您需要在catch块中区分自己,例如通过HRESULT:
如果该异常嵌套在另一个异常中,则需要首先展开它:
try {
...
} catch [OuterException] {
$e = $_.Exception.InnerException
switch ($e.HResult) {
...
}
}
有关try..catch的更多信息,请参阅。catch块可以按类区分异常:
try {
...
} catch [System.Net.WebException], [System.IO.IOException] {
# handle WebException and IOException
} catch [System.Management.Automation.MethodInvocationException] {
# handle MethodInvocationException
} catch {
# handle all other exceptions
}
他们无法区分同一类的不同异常。您需要在catch块中区分自己,例如
HRESULT:
如果该异常嵌套在另一个异常中,则需要首先展开它:
try {
...
} catch [OuterException] {
$e = $_.Exception.InnerException
switch ($e.HResult) {
...
}
}
有关try..catch的更多信息,请参阅