既不捕获也不捕获Powershell DirectoryService对象错误 [英] Powershell DirectoryService object error neither caught nor trapped

查看:70
本文介绍了既不捕获也不捕获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> & .\test.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变量

  • Catch or Trap [System.Exception]
  • Catch or Trap [System.Management]
  • to remove the -ErrorAction parameter
  • to use the $ErrorActionPreference variable

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

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对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆