I would like to authenticate an user in my ActiveDirectory with the Username and the Password. Is there any chance to do that with开发者_运维百科 powershell and the activeDirectory module. Thank you
There are multiple ways of doing this. Here is a quick and simple function which authenticates a user to AD.
Function Test-ADAuthentication {
param($username,$password)
(new-object directoryservices.directoryentry "",$username,$password).psbase.name -ne $null
}
PS C:\> Test-ADAuthentication "dom\myusername" "mypassword"
True
PS C:\>
It might not be the best function for your needs but your question lacks details.
Requires .NET 3.5 and PowerShell V2
$UserName = 'user1'
$Password = 'P@ssw0rd'
$Domain = $env:USERDOMAIN
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext $ct,$Domain
$pc.ValidateCredentials($UserName,$Password)
here is my version of the script. In this vesion the credentials are not stored in plain text, when you run the function it will prompt you to enter the credentials and
Function Test-ADAuthentication { $Cred = Get-Credential (New-Object DirectoryServices.DirectoryEntry "",$($Cred.UserName),$($cred.GetNetworkCredential().password)).psbase.name -ne $null }
If pass is wrong return false, if pass is right return true
精彩评论