Skip to content

Instantly share code, notes, and snippets.

@kpatnayakuni
Created November 6, 2017 15:29
Show Gist options
  • Save kpatnayakuni/fc0f7f8da7a995ec18db33666843064e to your computer and use it in GitHub Desktop.
Save kpatnayakuni/fc0f7f8da7a995ec18db33666843064e to your computer and use it in GitHub Desktop.
Validates domain credentials
function Test-ADCredential
{
$Credentials = Get-Credential -Message 'Enter your domain credentials'
$UserName = $Credentials.UserName
$Password = $Credentials.GetNetworkCredential().Password
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('domain')
try
{
$Status = $DS.ValidateCredentials($UserName, $Password)
if ($Status) { Write-Host -ForegroundColor Green "Credentials authenticated successfully!" }
else { Write-Host -ForegroundColor Red "Credentials authentication failed!" }
}
catch { Write-Host -ForegroundColor Red "Credentials authentication failed!" }
}
Test-ADCredential
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment