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

Powershell DirectoryService对象错误既未捕获也未捕获,powershell,error-handling,active-directory,Powershell,Error Handling,Active Directory,这是我脚本的一部分: Trap {Write-Output 'Authentication Error trapped'} Try {New-Object System.DirectoryServices.DirectoryEntry $strDistinguishedName,$strUsername,$strPlainPassword -ErrorAction stop} Catch{Write-Output 'Authentication Error catched'} Write-Out

这是我脚本的一部分:

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'
错误只是终止脚本,我找不到捕获或捕获错误的方法

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

以下是整个输出:

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>
我试过:

  • 捕获或陷阱[系统异常]
  • 捕获或陷阱[系统管理]
  • 删除-ErrorAction参数的步骤
  • 要使用$ErrorActionPreference变量

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

我最近遇到了相同的错误,试图在与当前域不信任的域上将DirectoryEntry对象实例化为RootDSE。 我发现,对结果对象调用方法可以成功捕获/捕获异常

因此,这并不像预期的那样有效:

try 
{
    $RDSE = [adsi]"LDAP://UntrustedService/RootDSE"
}
catch
{
    Write-Warning "Unable to connect to service"
}
但这确实:

try 
{
    $RDSE=[adsi]"LDAP://UntrustedService/RootDSE"
    [void]$RDSE.ToString()
}
catch [System.Management.Automation.RuntimeException]
{
    Write-Warning "Unable to connect to service"
}
catch #Default
{
    Write-Warning "Some other error"
}

我也有一个类似的错误,@tessellingheckler的评论帮助了我。 将结果传输到
输出Null
为我修复了它。因此,您可以尝试以下方法:

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'

奇怪的是,这个异常实际上来自
格式默认值
,它是用于在控制台中显示内容的输出格式化程序,例如管道到
输出默认值
newobject
调用没有失败,您可以通过编写
$x=try{newobject…}catch{}
看到,没有错误。但是如果您尝试
$x
查看它是什么,则会出现错误。然后,尝试
“$x”
,它声称是一个
系统.DirectoryServices.DirectoryEntry
。但是您不能
$x.GetType()
,因为它会抛出
parentContainesErrorRecordException
。这里发生了什么?这里讨论的是同一件事:你的链接是正确的。我搜索了一下,但没有找到一个话题已经在谈论这种奇怪的行为。你的询问比我的更有效;-)你可以发布你的链接作为答案。谢谢你的建议。但是,我不能使用您的对象,因为我需要ValidateCredentials方法。也许它可以帮助其他人阅读这篇文章…:-)