Created
April 20, 2018 13:00
-
-
Save JohnLBevan/69b38d42466e5612e22c2c651daf52c0 to your computer and use it in GitHub Desktop.
Script to test AD credentials; heavily based on https://gallery.technet.microsoft.com/scriptcenter/Verify-the-Active-021eedea/view/Discussions#content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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