Validar credenciales contra Active Directory con Powershell

Se me ocurren muchas maneras de comprobar si unas claves que te han dado son validas, pero tal vez la mas rapida y sencilla sea preguntarselo a LDAP directamente:

$username=read-host "usuario?"
$password=read-host "contraseña?"
$domaininfo = new-object DirectoryServices.DirectoryEntry("LDAP://cn=sites,cn=configuration,dc=dominio,dc=local",$username,$password)
$searcher = New-Object System.DirectoryServices.DirectorySearcher($domaininfo)
$searcher.filter = "((objectClass=site))"
$searcher.FindAll()|out-null
trap {'{0}' -f $_.Exception.Message; continue }
if ($?){write-host $username "credenciales OK" -fore green}
else{write-host $username "credenciales ERRONEAS" -fore red}

Comentarios