powershell - 异常在 "ValidateCredentials" "The server cannot handle directory requests."

标签 powershell ssl exception domaincontroller networkcredentials

我在安装过程中使用 Windows PowerShell 查询和验证用户的 Windows 凭据。直到昨天,它都运行良好。现在我公司的 IT 部门更改了域 Controller 的一些配置,现在我得到以下异常。

Exception calling "ValidateCredentials" with "2" argument(s): "The server cannot
handle directory requests."
At line:32 char:5
+ if ($pc.ValidateCredentials($username, $credential.GetNetworkCredenti ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DirectoryOperationException

From my research I already found out that it has to do with the missing SSL connection. I have to add ContextOptions.SecureSocketLayer somewhere in the code. The question is: Where is the right place to put this parameter? I cannot find any examples for PowerShell.

Here's the script I used to check the credentials:

$credential = $Host.UI.PromptForCredential("Need credentials.", "For using Windows Integrated Authentication please provide the login information for the user that has access to the Microsoft SQL Server database.", "", "")
if (!$credential) {
    Write-Output "No credentials provided"
    return
}

[System.Reflection.Assembly]::LoadWithPartialName('System.DirectoryServices.AccountManagement')

$system = Get-WmiObject -Class Win32_ComputerSystem

if ($credential.GetNetworkCredential().Domain) {
    Write-Output "Credentials contain domain"
    if ($credential.GetNetworkCredential().Domain -eq $system.Name) {
        Write-Output "Domain is local system"
        $pc = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext 'Machine', $system.Name
    } else {
        Write-Output "Domain is network domain"
        $pc = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext 'Domain', $credential.GetNetworkCredential().Domain
    }
    $username = $credential.UserName
} elseif (0, 2 -contains $system.DomainRole) {
    $pc = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext 'Machine', $system.Name
    $username = $system.Name + '\' + $credential.GetNetworkCredential().UserName
} else {
    $pc = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext 'Domain', $system.Domain
    $username = $system.Domain + '\' + $credential.GetNetworkCredential().UserName
}

if ($pc.ValidateCredentials($username, $credential.GetNetworkCredential().Password)) {
    Write-Output "Validation successfull"
} else {
    Write-Output "Validation failed"
}

最佳答案

作为mentioned by Kiran in the comments ,您可以将 ContextOptions 值传递给 PrincipalContext 构造函数:

$DefaultNC = "DC=$($system.Domain -replace '\.',',DC=')"
# ...
$pc = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext 'Domain', $system.Domain, $DefaultNC, ([System.DirectoryServices.AccountManagement.ContextOptions]'SecureSocketLayer,Negotiate')

必须指定身份验证选项(NegotiateSimpleBind),因此 'SecureSocketLayer,Negotiate'

关于powershell - 异常在 "ValidateCredentials" "The server cannot handle directory requests.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46170531/

相关文章:

powershell - 确定 Powershell 中不同时区的夏令时状态

powershell - Nagios PNP4Nagios平均整数值?

Android WebView 不从 https 网站加载验证码图像

ssl - 签名证书和错误 :- x509: certificate signed by unknown authority 之间的混淆

java - java中的多个catch语句

c# - 静态抛出类 : good or bad practice

powershell - Unix换行符到Windows换行符(在Windows上)

java - 启动 powershell 脚本的 HTML 网页

ssl - Cloudify 与 Openstack :SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

java - NetBeans 并停止/中断所有异常?