How do you control password resets in your environment? I’ve worked for numerous companies where their forgotten password reset process was all over the board. Hopefully you have a process in place that allows you to sleep at night. Even with the best policies and procedures in place, what happens when someone on your help desk staff resets a users password to some default password and forgets to set the account so the password has to be changed at next logon? Is the user still using that default password weeks later?
I decided to write a PowerShell script to test user accounts for just that exact scenario.
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | #Requires -Version 3.0 -Modules ActiveDirectory function Test-MrADUserPassword { <# .SYNOPSIS Test-MrADUserPassword is a function for testing an Active Directory user account for a specific password. .DESCRIPTION Test-MrADUserPassword is an advanced function for testing one or more Active Directory user accounts for a specific password. .PARAMETER UserName The username for the Active Directory user account. .PARAMETER Password The password to test for. .PARAMETER ComputerName A server or computer name that has PowerShell remoting enabled. .PARAMETER InputObject Accepts the output of Get-ADUser. .EXAMPLE Test-MrADUserPassword -UserName alan0 -Password Password1 -ComputerName Server01 .EXAMPLE 'alan0'. 'andrew1', 'frank2' | Test-MrADUserPassword -Password Password1 -ComputerName Server01 .EXAMPLE Get-ADUser -Filter * -SearchBase 'OU=AdventureWorks Users,OU=Users,OU=Test,DC=mikefrobbins,DC=com' | Test-MrPassword -Password Password1 -ComputerName Server01 .INPUTS String, Microsoft.ActiveDirectory.Management.ADUser .OUTPUTS PSCustomObject .NOTES Author: Mike F Robbins Website: http://mikefrobbins.com Twitter: @mikefrobbins #> [CmdletBinding(DefaultParameterSetName='Parameter Set UserName')] param ( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName='Parameter Set UserName')] [Alias('SamAccountName')] [string[]]$UserName, [Parameter(Mandatory)] [string]$Password, [Parameter(Mandatory)] [string]$ComputerName, [Parameter(ValueFromPipeline, ParameterSetName='Parameter Set InputObject')] [Microsoft.ActiveDirectory.Management.ADUser]$InputObject ) BEGIN { $Pass = ConvertTo-SecureString $Password -AsPlainText -Force $Params = @{ ComputerName = $ComputerName ScriptBlock = {Get-Random | Out-Null} ErrorAction = 'SilentlyContinue' ErrorVariable = 'Results' } } PROCESS { if ($PSBoundParameters.UserName) { Write-Verbose -Message 'Input received via the "UserName" parameter set.' $Users = $UserName } elseif ($PSBoundParameters.InputObject) { Write-Verbose -Message 'Input received via the "InputObject" parameter set.' $Users = $InputObject } foreach ($User in $Users) { if (-not($Users.SamAccountName)) { Write-Verbose -Message "Querying Active Directory for UserName $($User)" $User = Get-ADUser -Identity $User } $Params.Credential = (New-Object System.Management.Automation.PSCredential ($($User.UserPrincipalName), $Pass)) Invoke-Command @Params [pscustomobject]@{ UserName = $User.SamAccountName PasswordCorrect = switch ($Results.FullyQualifiedErrorId -replace ',.*$') { LogonFailure {$false; break} AccessDenied {$true; break} default {$true} } } } } } |
Test one or more Active Directory user accounts for a password of “Password1”:
1 | Test-MrADUserPassword -UserName alan0, david1 -Password Password1 -ComputerName web01 |
Same test except using pipeline input of strings for the usernames:
1 | 'alan0', 'david1' | Test-MrADUserPassword -Password Password1 -ComputerName web01 |
Test all of the user accounts in a specific OU (Organizational Unit) in Active Directory:
1 2 3 | Get-ADUser -Filter * -SearchBase 'OU=AdventureWorks Users,OU=Users,OU=Test,DC=mikefrobbins,DC=com' | Test-MrPassword -Password Password1 -ComputerName web01 | Sort-Object -Property PasswordCorrect |
Same as the previous example except only return the accounts where the password matched.
1 2 3 | Get-ADUser -Filter * -SearchBase 'OU=AdventureWorks Users,OU=Users,OU=Test,DC=mikefrobbins,DC=com' | Test-MrPassword -Password Password1 -ComputerName web01 | Where-Object PasswordCorrect -eq $true |
Be sure to test this and to get permission from someone in your chain of command before running it in a production environment. Be careful when using this function because it does count as a failed login for the user account if the password doesn’t match. It will show up on your audit login failures report if you’re performing any type of auditing for login failures. You could also end up locking out the user account if you run this enough times to meet the account lockout threshold set in your domain or in the fine grained password policies if they’re enabled in your environment.
The function shown in this blog article can be downloaded from my ActiveDirectory repository on GitHub.
Update: While at the PowerShell + DevOps Global Summit this week, I was discussing this function with a group of attendees and I discovered that there’s a better way to accomplish this task. I’ll post a follow-up blog article next week.
µ
Excellent post Mike!!!
Yes, validateCredentials method on instance of System.DirectoryServices.AccountManagement.PrincipalContext class… But slick script!
Thank you! I was searching for exactly something like this
Hi, the script is not working for me. If I run the script nothing happens. No error is given it just jumps to the next line no matter what Parameters I use.
Same here.
Hi Mark,
Is there any way to search a whole domain? Didnt seem to like the below for me.
Get-ADUser -Filter * -SearchBase ‘DC=Company,DC=Local’ |
Test-MrPassword -Password Password1 -ComputerName web01 |
Where-Object PasswordCorrect -eq $true
Thanks in advance,
Trent