aboutsummaryrefslogtreecommitdiffstats
path: root/AD-powershell-tools/test-ad-credentials.ps1
blob: bd0ba84dad65248d2c014ad2d871c9dc19d76670 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Adapted from: https://itpro-tips.com/test-ad-authentication-with-powershell/
# The interesting bit about this one is that it doesn't seem to get logged by AD,
# so you won't end up with false positives from testing creds

function Test-ADAuthentication {
        Param(
                [Parameter(Mandatory)]
                [string]$User,
                [Parameter(Mandatory)]
                $Password,
                [Parameter(Mandatory = $false)]
                $Server,
                [Parameter(Mandatory = $false)]
                [string]$Domain = $env:USERDOMAIN
        )

        Add-Type -AssemblyName System.DirectoryServices.AccountManagement

        $contextType = [System.DirectoryServices.AccountManagement.ContextType]::Domain

        $argumentList = New-Object -TypeName "System.Collections.ArrayList"
        $null = $argumentList.Add($contextType)
        $null = $argumentList.Add($Domain)

        if($null -ne $Server){
                $argumentList.Add($Server)
        }

        $principalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $argumentList -ErrorAction SilentlyContinue

        if ($null -eq $principalContext) {
                Write-Warning "$Domain\$User - AD Authentication failed"
        }

        if ($principalContext.ValidateCredentials($User, $Password)) {
                Write-Output "$Domain\$User - AD Authentication OK"
        }
        else {
                Write-Warning "$Domain\$User - AD Authentication failed"
        }
}

$csv = Import-Csv $args[0]
ForEach ($userpass in $csv) {
        Test-ADAuthentication -User $userpass.user -Password $userpass.password
}