对象错误既未捕获也未捕获

对象错误既未捕获也未捕获

本文介绍了Powershell DirectoryService 对象错误既未捕获也未捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我脚本的一部分:

Trap {Write-Output 'Authentication Error trapped'}
Try {New-Object System.DirectoryServices.DirectoryEntry $strDistinguishedName,$strUsername,$strPlainPassword -ErrorAction stop}
Catch{Write-Output 'Authentication Error catched'}
Write-Output 'Script has not trapped nor catched the error but continued'

错误只是终止了脚本,我发现无法捕获或捕获错误.

The error is just terminating the script and I found no way to catch or trap the error.

脚本甚至没有写最后一行,这意味着它已经完全退出了脚本.

The script does even not write the last line which means it has completely exited the script.

这里是整个输出:

PS C:Temp> & .	est.ps1
format-default : The following exception occurred while retrieving member "distinguishedName": "Logon failure: unknown user name or bad password.
"+ CategoryInfo          : NotSpecified: (:) [format-default], ExtendedTypeSystemException
+ FullyQualifiedErrorId : CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand
PS C:Temp>

错误类型为:

PS C:Temp> $Error[0].GetType()
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     CmdletInvocationException                System.Management.Automation.RuntimeException
PS C:Temp>

我试过了:

  • 捕获或陷阱 [System.Exception]
  • 捕获或陷阱 [System.Management]
  • 删除 -ErrorAction 参数
  • 使用 $ErrorActionPreference 变量

有人知道如何捕获这个错误吗?

Does anybody know how to trap this error?

推荐答案

我遇到了类似的错误,@TessellatingHeckler 的评论帮助了我.将结果传送到 Out-Null 为我修复了它.因此,您可以尝试以下操作:

I had a similar error and the comment from @TessellatingHeckler helped me.Piping the result to Out-Null fixed it for me. So you can try the following:

Trap {Write-Output 'Authentication Error trapped'}
Try {New-Object System.DirectoryServices.DirectoryEntry $strDistinguishedName,$strUsername,$strPlainPassword -ErrorAction stop | Out-Null}
Catch{Write-Output 'Authentication Error catched'}
Write-Output 'Script has not trapped nor catched the error but continued'

这篇关于Powershell DirectoryService 对象错误既未捕获也未捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 22:58