gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:22:25 24 4
gpt4 key购买 nike

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

Exception calling "ValidateCredentials" with "2" argument(s): "The server cannothandle 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com