Code Ipsun

Test code powershell.

Function While-UserLockInfo {
    Param (
        [string[]]$u, # searchbase, creates an array.
        [int]$t # timeout between lookups.
    )
    if(([string]::IsNullOrEmpty($u)) -or ($u -eq "help")) {
        Write-Output "Call function with While-UserLockInfo -u [string]accountname1, [string]accountname2 -t [int]timeout"
    } elseif(($t -eq $null) -or ($t -lt 1)) {
        Write-Output "Call function with While-UserLockInfo -u [string]accountname1, [string]accountname2 -t [int]timeout"
    } else {
        While($true) { # inf loop.
            # look up the array of users.
            foreach($users in $u) {
                $lookup = Get-ADUser $users -Properties Displayname, LockedOut, badPwdCount, AccountLockoutTime, PasswordExpired | Select-Object -Property Displayname, LockedOut, badPwdCount, AccountLockoutTime, PasswordExpired
                # get user account status.
                if($lookup.LockedOut -eq $true) {
                    Write-Warning "$users has been locked out for: $($lookup.AccountLockoutTime)"
                    # unlocks the AD-account.
                    Unlock-ADAccount -Identity $users
                # depending on settings in organization.
                } elseif($lookup.badPwdCount -ge 3) { 
                    Write-Warning "$users is about to be locked out. Bad PwdCount: $($lookup.badPwdCount)"
                    # Automatic unlock to reset the count while searching for a fix. Uncomment
                    # Unlock-ADAccount -Identity $users
                } else {
                    # Stop script with ctrl+c
                    Write-Output "$(Get-Date -Format HH:mm:ss) | $users Not Locked Out. Failed Password attempts: $($lookup.badPwdCount)"
                sleep $t
                }
            }
        }
    }
}