Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Created April 20, 2018 13:00
Show Gist options
  • Save JohnLBevan/69b38d42466e5612e22c2c651daf52c0 to your computer and use it in GitHub Desktop.
Save JohnLBevan/69b38d42466e5612e22c2c651daf52c0 to your computer and use it in GitHub Desktop.
#heavily based on https://gallery.technet.microsoft.com/scriptcenter/Verify-the-Active-021eedea/view/Discussions#content
function Test-ADCredential {
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[System.Management.Automation.Credential()]
[System.Management.Automation.PSCredential]$Credential
)
begin {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
}
process {
if (!($Credential.UserName) -or !($Credential.GetNetworkCredential().Password)) {
Write-Warning 'Test-ADCredential: Please specify both user name and password'
} else {
$DS = [System.DirectoryServices.AccountManagement.PrincipalContext]::new([System.DirectoryServices.AccountManagement.ContextType]::Domain)
$DS.ValidateCredentials($Credential.UserName, $Credential.GetNetworkCredential().Password) #NB: Username should not contain domain prefix (i.e. use myUsername instead of myDomain\myUsername)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment