1

我试图很好地捕捉错误,就像我在 Java 中所做的那样。该程序与此类似:

try
{
    New-Object System.DirectoryServices.DirectoryEntry($SearchString, $username, $pass)
    Write-Ouput "Continue program"
}
catch
{
    Write-Output "Some error"
}

永远不会捕获异常。事实上,当一切顺利时,catch 方法就会执行。从我能得到的不同错误来看,看起来并没有引发异常

format-default : The following exception occurred while retrieving member "distinguishedName": "The server is not operational.
"
    + CategoryInfo          : NotSpecified: (:) [format-default], ExtendedTypeSystemException
    + FullyQualifiedErrorId : CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand


format-default : The following exception occurred while retrieving member "distinguishedName": "The user name or password is 
incorrect.
"
    + CategoryInfo          : NotSpecified: (:) [format-default], ExtendedTypeSystemException
    + FullyQualifiedErrorId : CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand
 

如何以更用户友好的方式捕获这些错误?

也许在这种情况下是不可能的


经过一些测试,我认为问题在于,尽管错误消息说明了什么,但由于某种原因没有引发异常。所以这就是为什么我无法捕捉它。我也尝试过使用getType,close和其他方法,但我得到了同样的错误:

The following exception occurred while retrieving member "GetType": "The user name or password is incorrect.
"
...

这个解决方案似乎也不起作用,同样的行为。

问题相关

4

4 回答 4

0

你也可以默认回到这个......

If ($(New-Object System.DirectoryServices.DirectoryEntry($SearchString, $username, $pass)) -eq $false)
{Write-Verbose 'Continue program' -Verbose}
Else {Write-Warning -Message 'Something went wrong with your directory command'}
# Results
<#
WARNING: Something went wrong with your directory command
#>

...虽然像你一样尝试/捕捉是我的偏好。我目前不在 ADDS 环境中测试 try/catch 之一。

知道你不是唯一一个为此感到压力的人。请参阅: Powershell DirectoryService 对象错误既未捕获也未捕获

然而,在对我过去关于 ADDS(过去的 ADSI 东西)的笔记进行了一些挖掘之后,这里是这个命名空间的真正交易。结果不是真正的 $true 或 $false,不像 System.DirectoryServices.AccountManagement;使用您正在使用的内容,提供了不正确的凭据,您会收到错误消息。如果凭据正确,则返回的对象将包含有效结果,结果为布尔输出。

因此,如果您真的想坚持使用 Try/Catch,请改为执行以下操作:

try
{
    $AuthResult = [bool](New-Object System.DirectoryServices.DirectoryEntry($SearchString, $username, $pass)).distinguishedName -ne 'False'
    'Continue program'
}
catch {Write-Warning -Message 'Bad credentials passed'}
# Resuls
<#
WARNING: Bad credentials passed
#>

当然,如果您愿意,您可以捕获错误数据并根据需要输出。

然而,这与 If/Then 中提供的检查没有太大区别。所以,你的选择。

于 2020-10-06T17:36:02.573 回答
0

需要添加'erroraction'

try

    {
        New-Object System.DirectoryServices.DirectoryEntry($SearchString, $username, $pass) -ErrorAction Stop
        Write-Ouput "Continue program"
    }
    catch
    {
        $Err = $error[0]
        Write-Output $Err.Exception
    }
于 2020-10-06T15:56:49.537 回答
0

我设法用这个解决方案做到了:

$o = 新对象 DirectoryServices.DirectoryEntry ($path, $user, $pass)

if ($path -eq 'LDAP://') { '计算机不是域的成员。' } elseif ($o.DistinguishedName) { '无效的用户凭据。' 但是如果由于某种原因你必须检索实际的错误信息,你可以这样做:

$o = 新对象 DirectoryServices.DirectoryEntry ($path, $user, $pass)

试试 { $o | Out-Default } 捕获 { $_.Exception.InnerException.Message }

于 2020-10-07T17:09:06.490 回答
0

这对我有用。即使在您稍后尝试使用它之前可能不会出现错误,该对象也会实例化。使用它,您可以设置一个布尔值来确定是否设置了 distinctName:

$Entry = New-Object System.DirectoryServices.DirectoryEntry($SearchString, $username, $pass)
$succeeded = [bool]$Entry.distinguishedName
if ($succeeded)
{
     Write-Ouput "Continue program"
}
else
{
     Write-Output "Some error"
}
于 2021-08-19T19:14:57.570 回答