【问题标题】:Test-ADCredential fails on Windows 10 but works on 7Test-ADCredential 在 Windows 10 上失败,但在 7 上有效
【发布时间】:2018-03-13 17:58:02
【问题描述】:

在任何其他现实中,我都不能被视为系统管理员。相反,我支持 ERP 系统并使用 PowerShell 作为编程语言来做 BI 类的事情,因为我无法使用编写 ERP 的语言。所以,请忽略我对这个问题的无知.

为了使我们的 ERP 与工程系统保持同步,我创建了一套程序。第一个验证用于登录 PC 的 AD 帐户密码并将其输出到文件以供以后使用。该程序适用于开发它的 Windows 7 机器。但是,在 Windows 10 机器上运行时会出现此错误。 “Test-ADCredential”函数是从实习生那里偷来的,不是我的。

Exception calling "ValidateCredentials" with "2" argument(s): "The server cannot handle directory requests."
At C:\GitRepo\PowerShellScripts\TeamCenter2M3AdCheck.ps1:20 char:9
+         $DS.ValidateCredentials($UserName, $Password)
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DirectoryOperationException

开发机器上的 PowerShell (Windows 7):

Name                           Value
----                           -----
PSVersion                      5.1.14409.1012
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14409.1012
CLRVersion                     4.0.30319.34209
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Windows 10 版本:

Name                           Value
----                           -----
PSVersion                      5.1.15063.786
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.786
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

这是程序:

# TeamCenter2M3AdCheck.ps1  Verify the password for current user and pass the user/id for MDI update
#  02/05/2018

param ([string]$IniFile)

Import-Module "C:\GitRepo\PowerShellScripts\TeamCenter2M3Setup.psm1" -Force

function Test-ADCredential {
  [CmdletBinding()]
    Param ([string]$UserName,
           [string]$Password)
    if (!($UserName) -or !($Password)) {
        Write-Warning 'Test-ADCredential: Please specify both user name and password'
    } else {
        Add-Type -AssemblyName System.DirectoryServices.AccountManagement
        $DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('domain')
        $DS.ValidateCredentials($UserName, $Password)
    }
}  # function Test-ADCredential

# Initilize globlal variables from the INI file
IniParameters -IniFile $IniFile

$PasswordFile = $TC2M3Dir + "TeamCenterPassword.csv"
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
[Byte[]] $Key = (1..16)

$UserId = $env:USERNAME
Do {  #loop until password is okay
    # Request the account's password
    $SecurePassword = Read-Host -AsSecureString  -Prompt "Enter password for $UserId"
    $UnsecurePassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword))
    $Okay = Test-ADCredential -UserName $UserId -Password $UnsecurePassword

    If ($Okay) {
      $PasswordString = ConvertFrom-SecureString -SecureString $SecurePassword -Key $Key
     $CsvText = "AdAccount,Password`n$UserId,$PasswordString"
     Out-File -FilePath $PasswordFile -InputObject $CsvText -Encoding ascii
   }
   else {
        Write-Host "Password failed.  Please try again or press Ctrl+c" -ForegroundColor Red
    }
} Until ($Okay)

提前感谢您的帮助。

【问题讨论】:

  • 等等,你正在将密码保存在一个打开的文本文件中?
  • "验证用于登录 PC 的 AD 帐户密码并将其输出到文件以供以后使用。"我只想说这听起来很疯狂,请不要。关于您描述的问题,机器和运行脚本的用户帐户是否都在同一个域中?
  • 这至少会让你变得严厉,并且经常被我工作过的大多数公司解雇。
  • 从这个程序传出的文本文件是加密的。是的,两台机器在同一个域中,密码相同,因为我使用的是我的帐户进行测试。
  • 这两个系统上是否有不同的 .net 框架?或者是否加入了一个系统域而另一个没有加入?两个系统都以管理员身份运行脚本吗?

标签: powershell active-directory


【解决方案1】:

变化:

$DS.ValidateCredentials($UserName, $Password)

收件人:

$DS.ValidateCredentials($UserName, $Password, [DirectoryServices.AccountManagement.ContextOptions]::Negotiate -bor [DirectoryServices.AccountManagement.ContextOptions]::Sealing)

基本上,在新机器上,它可能会回退到尝试使用基本身份验证,而不是使用“协商”(即使用 Kerberos 或 NTLM 加密与 AD 的连接)来进行验证。通过明确设置上下文选项以强制协商或密封(另一种加密变体),这应该可以解决问题。

我确认我能够让代码在 Windows 10 上运行。

参考资料:

【讨论】:

  • 先生。 HAL9256 -- 非常感谢。现在我可以把它丢给错误了。把它交给工程师。
猜你喜欢
  • 2014-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-14
相关资源
最近更新 更多