Answers:
我的图书馆有这个:
$cred = Get-Credential #Read credentials
$username = $cred.username
$password = $cred.GetNetworkCredential().password
# Get current domain using logged-on user's credentials
$CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)
if ($domain.name -eq $null)
{
write-host "Authentication failed - please verify your username and password."
exit #terminate the script.
}
else
{
write-host "Successfully authenticated with domain $domain.name"
}
ActiveDirectory
模块进行LDAP查询?
这是我过去使用过的;它应该适用于本地计算机帐户和“应用程序目录”,但到目前为止,我仅将其与AD凭据一起成功使用:
function Test-Credential {
<#
.SYNOPSIS
Takes a PSCredential object and validates it against the domain (or local machine, or ADAM instance).
.PARAMETER cred
A PScredential object with the username/password you wish to test. Typically this is generated using the Get-Credential cmdlet. Accepts pipeline input.
.PARAMETER context
An optional parameter specifying what type of credential this is. Possible values are 'Domain','Machine',and 'ApplicationDirectory.' The default is 'Domain.'
.OUTPUTS
A boolean, indicating whether the credentials were successfully validated.
#>
param(
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[System.Management.Automation.PSCredential]$credential,
[parameter()][validateset('Domain','Machine','ApplicationDirectory')]
[string]$context = 'Domain'
)
begin {
Add-Type -assemblyname system.DirectoryServices.accountmanagement
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::$context)
}
process {
$DS.ValidateCredentials($credential.UserName, $credential.GetNetworkCredential().password)
}
}
$context
作为参数传递给构造函数。PowerShell将自动将字符串转换为枚举。更好的是,只需[System.DirectoryServices.AccountManagement.ContextType]
输入的类型$context
。另外,为什么您使用的begin
和process
在这里?管道似乎是使用此功能的一种奇怪方法。
$context
参数[System.DirectoryServices.AccountManagement.ContextType]
不是一种选择,因为包含的程序集要等到函数体执行后才加载;如果要验证多个凭据,使用管道很有帮助。
Add-Type
不能在执行其定义之前将调用移到该函数之外。我很犹豫是否要Add-Type
在函数中无条件地重复运行某个调用,即使它已经被加载也是如此。首先,同时验证多个凭据似乎很奇怪。在极少数情况下,这是您想要的,您可以轻松地将调用包装在中ForEach-Object
,因此我看不出将函数复杂化的原因。
我发现这篇文章很有用,但是并没有解决我的问题,因为我试图通过登录了本地管理员帐户的脚本来运行它。它似乎不能以本地管理员身份工作(仅当以域用户身份登录时)。
但是,我终于设法得到了一个可行的解决方案,并且由于遇到了太多麻烦,我想在这里分享它,因此遇到此问题的任何人都可以在这里找到答案。根据您的需要,两个答案都在一页上。
请注意,在scipt中的较高位置(此处不包含此内容,因为这只是get-credentials部分),已经安装了powergui,这是下面的代码(以及“ Add-PSSnapin Quest.ActiveRoles.ADManagement”行)的要求。不知道powergui做什么,这是不同的,但是没有人能告诉我,它是否有效。
在“ domain_name”部分中替换您自己的域名。
#Get credentials
$credential_ok = 0
while ($credential_ok -ne 1)
{
$credential = get-credential
$result = connect-qadservice -service *domain_name* -credential $credential
[string]$result_string = $result.domain
if ($result_string -eq "*domain_name*")
{
$credential_ok = 1
#authenticated
}
else
{
#failed
}
}
$username = $credential.username
$password = $credential.GetNetworkCredential().password
$date = get-date
Add-Content "c:\lbin\Install_log.txt" "Successfully authenticated XP script as $username $date"
(还)另一个版本:
param([string]$preloadServiceAccountUserName = "")
function HarvestCredentials()
{
[System.Management.Automation.PSCredential]$credentialsOfCurrentUser = Get-Credential -Message "Please enter your username & password" -UserName $preloadServiceAccountUserName
if ( $credentialsOfCurrentUser )
{
$credentialsOfCurrentUser = $credentialsOfCurrentUser
}
else
{
throw [System.ArgumentOutOfRangeException] "Gui credentials not entered correctly"
}
Try
{
# see https://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.path(v=vs.110).aspx
# validate the credentials are legitimate
$validateCredentialsTest = (new-object System.DirectoryServices.DirectoryEntry ("WinNT://"+$credentialsOfCurrentUser.GetNetworkCredential().Domain), $credentialsOfCurrentUser.GetNetworkCredential().UserName, $credentialsOfCurrentUser.GetNetworkCredential().Password).psbase.name
if ( $null -eq $validateCredentialsTest)
{
throw [System.ArgumentOutOfRangeException] "Credentials are not valid. ('" + $credentialsOfCurrentUser.GetNetworkCredential().Domain + '\' + $credentialsOfCurrentUser.GetNetworkCredential().UserName + "')"
}
else
{
$t = $host.ui.RawUI.ForegroundColor
$host.ui.RawUI.ForegroundColor = "Magenta"
Write-Output "GOOD CREDENTIALS"
$host.ui.RawUI.ForegroundColor = $t
}
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
$StackTrace = $_.Exception.StackTrace
$t = $host.ui.RawUI.ForegroundColor
$host.ui.RawUI.ForegroundColor = "Red"
Write-Output "Exception - $ErrorMessage"
Write-Output "Exception - $FailedItem"
Write-Output "Exception - $StackTrace"
$host.ui.RawUI.ForegroundColor = $t
throw [System.ArgumentOutOfRangeException] "Attempt to create System.DirectoryServices.DirectoryEntry failed. Most likely reason is that credentials are not valid."
}
}
Try
{
HarvestCredentials
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
$StackTrace = $_.Exception.StackTrace
$t = $host.ui.RawUI.ForegroundColor
$host.ui.RawUI.ForegroundColor = "Red"
Write-Output "Exception - " + $ErrorMessage
Write-Output "Exception - " + $FailedItem
Write-Output "Exception - " + $StackTrace
$host.ui.RawUI.ForegroundColor = $t
Break
}
Finally
{
$Time=Get-Date
Write-Output "Done - " + $Time
}
和
.\TestCredentials.ps1 -preloadServiceAccountUserName "mydomain\myusername"
AccountManagement.PrincipalContext.ValidateCredentials()
不正确(如果您提供密码的安全字符串)?