Sign in to follow this  
Followers 0
joshiieeii

How to validate username in domain?

14 posts in this topic

#1 ·  Posted (edited)

*Edit*

Ok, made a pretty robust little Active Directory (AD) helper

Lists most stuff found in the AD users and computers under a user's profile, if the account is locked a button will allow you to unlock it (untested).

Let me know what you think.....

*Edit: Updated to latest code 7/13/2006

Changes:

**Added unlock button

**Changed colors if locked or unlocked

**Changed colors for number of bad logons

**Filtered the info for manager, last password change, and last logon to a standard format

**Added 90 Day expiration date

**Added some notification for the unlock button

#include <GUIConstants.au3>
#include <Misc.au3>
Const $ADS_NAME_INITTYPE_GC = 3
Const $ADS_NAME_TYPE_NT4 = 3
Const $ADS_NAME_TYPE_1779 = 1
DIM $unlock
DIM $mgrvalue
DIM $mgrsplit
DIM $manager
DIM $mgr
DIM $title
DIM $pwdexpires
$oMyError = ObjEvent("AutoIt.Error", "ComError")
$objRootDSE = ObjGet("LDAP://RootDSE")
$username = InputBox("Username","Please input a username:")  
If @error Then
    MsgBox(0, 'username', 'Username does not exist or not able to communicate with ' & @LogonDomain)
Else
; DNS domain name.
    $objTrans = ObjCreate("NameTranslate")
    $objTrans.Init ($ADS_NAME_INITTYPE_GC, "")
    $objTrans.Set ($ADS_NAME_TYPE_1779, @LogonDomain)
    $objTrans.Set ($ADS_NAME_TYPE_NT4, @LogonDomain & "\" & $username)
    $strUserDN = $objTrans.Get ($ADS_NAME_TYPE_1779)
    $UserObj = ObjGet("LDAP://" & $strUserDN)
    If @error Then
        MsgBox(0, 'username', 'Username does not exist or not able to communicate with ' & @LogonDomain)
    Else
        
        Call ("Displayinfo")
        
    
    EndIf
EndIf
$UserObj = ""
$oMyError = ObjEvent("AutoIt.Error", "")
;COM Error function
Func ComError()
    If IsObj($oMyError) Then
        $HexNumber = Hex($oMyError.number, 8)
        SetError($HexNumber)
    Else
        SetError(1)
    EndIf
    Return 0
EndFunc ;==>ComError


Func Displayinfo()
    GUICreate ( "Active Directory Information", 500, 600, 300, 300)
    
    GUICtrlCreateLabel ("Username: ", 10, 10, 60, 20)   
    GUICtrlCreateLabel ("First Name: ", 10, 30, 60, 20) 
    GUICtrlCreateLabel ("Last Name: ", 200, 30, 60, 20) 
    GUICtrlCreateLabel ("Display Name: ", 10, 50, 100, 20)  
    GUICtrlCreateLabel ("Title: ", 10, 70, 100, 20) 
    GUICtrlCreateLabel ("Manager: ", 10, 90, 100, 20)   
    GUICtrlCreateLabel ("Description: ", 10, 150, 100, 20)  
    GUICtrlCreateLabel ("Office: ", 10, 190, 60, 20)    
    GUICtrlCreateLabel ("Department: ", 10, 250, 100, 20)   
    GUICtrlCreateLabel ("Telephone Number: ", 10, 290, 90, 40)  
    GUICtrlCreateLabel ("Mobile Number: ", 10, 320, 100, 20)    
    GUICtrlCreateLabel ("Home Number: ", 10, 350, 100, 20)  
    GUICtrlCreateLabel ("Email Address: ", 10, 370, 100, 20)    
    GUICtrlCreateLabel ("Logon Script: ", 10, 410, 100, 20)
    GUICtrlCreateLabel ("Account:", 10, 430, 100, 20)
    GUICtrlCreateLabel ("Number of bad logon attempts since last reset: ", 310, 420, 120, 40)
    GUICtrlCreateLabel ("Password Last Changed: ", 10, 460, 100, 40)
    GUICtrlCreateLabel ("90 Day Password Expiration: ", 10, 490, 100, 40)
    GUICtrlCreateLabel ("Last Logon: ", 10, 540, 100, 20)   

$font="Tahoma"
GUISetFont (9, 600, $font)   ; will display underlined characters
$unlock = GUICtrlCreateButton ( "UNLOCK Account", 180, 425, 120, 25)
GUICtrlSetState ( $unlock, $Gui_Disable )
GUICtrlCreateLabel ( ''& $username, 100, 10, 100, 20)
GUICtrlSetColor(-1,0x0000CC)    ; Blue
GUICtrlCreateLabel (''& $UserObj.FirstName, 100, 30, 100, 20)   
GUICtrlCreateLabel (''& $UserObj.LastName, 300, 30, 100, 20)    
GUICtrlCreateLabel (''& $UserObj.FullName, 100, 50, 300, 20)    
GUICtrlCreateLabel (''& $UserObj.Title, 100, 70, 100, 20)
$title = GUICtrlRead ( $title )
If $title = 0 Then
    GUICtrlCreateLabel ('', 100, 70, 100, 20)
Endif

$mgr = GUICtrlCreateLabel (''& $UserObj.Manager, 100, 90, 400, 70)  
$mgrvalue = GUICtrlRead ( $mgr )
$mgrsplit = StringSplit ( ""& $mgrvalue, ",")
$manager = StringTrimLeft ( ''& $mgrsplit[1], 3 )
GUICtrlCreateLabel (''& $manager, 100, 90, 400, 70)
GUICtrlCreateLabel (''& $UserObj.Description, 100, 150, 300, 40)    
GUICtrlCreateLabel (''& $UserObj.physicalDeliveryOfficeName, 100, 190, 100, 50) 
GUICtrlCreateLabel (''& $UserObj.Department, 100, 250, 200, 20) 
GUICtrlCreateLabel (''& $UserObj.TelephoneNumber, 100, 300, 250, 20)    
GUICtrlCreateLabel (''& $UserObj.TelephoneMobile, 100, 320, 250, 20)    
GUICtrlCreateLabel (''& $UserObj.TelephoneHome, 120, 350, 250, 20)  
GUICtrlCreateLabel (''& $UserObj.EmailAddress, 100, 370, 300, 20)   
GUICtrlCreateLabel (''& $UserObj.LoginScript, 100, 410, 200, 15)
$locked = GUICtrlCreateLabel (""& $UserObj.IsAccountLocked, 100, 430, 10, 20)
If GuiCtrlread ($locked) = 0 or 39 Then
GUICtrlCreateLabel ("NOT Locked", 100, 430, 80, 15)
GUICtrlSetBkColor(-1, 0x00ff00);Green
Else
    MsgBox(0, 'INFO', "User Account Lock value is: "& $locked)
GUICtrlCreateLabel ("LOCKED", 10, 430, 60, 15)
GUICtrlSetBkColor(-1, 0xff0000) ; Red
GUICtrlSetState ( $unlock, $Gui_Enable )

EndIf
$lastchange = $UserObj.PasswordLastChanged

$Date = StringMid($lastchange, 5, 2) & "/" & StringMid($lastchange, 7, 2) & "/" & StringMid($lastchange, 1, 4)
$Time = StringMid($lastchange, 9, 2) & ":" & StringMid($lastchange, 11, 2) & ":" & StringMid($lastchange, 13, 2)
GUICtrlCreateLabel ($Date & " "& $Time, 100, 460, 150, 20)
$pwdexpires = StringMid($lastchange, 5, 2) + 3 & "/" & StringMid($lastchange, 7, 2) & "/" & StringMid($lastchange, 1, 4)
GUICtrlCreateLabel ( $pwdexpires & ' ' & $Time, 100, 490, 150, 20)





$lastlogin = $UserObj.LastLogin

$Date = StringMid($lastlogin, 5, 2) & "/" & StringMid($lastlogin, 7, 2) & "/" & StringMid($lastlogin, 1, 4)
$Time = StringMid($lastlogin, 9, 2) & ":" & StringMid($lastlogin, 11, 2) & ":" & StringMid($lastlogin, 13, 2)
GUICtrlCreateLabel ($Date & " "& $Time, 100, 540, 150, 20)
$badlogin = GUICtrlCreateLabel (""& $UserObj.BadLoginCount, 430, 430, 20, 15)
If GuiCtrlread ($badlogin) = 0 Then
GUICtrlSetBkColor(-1, 0x00ff00);Green
Else
GUICtrlSetBkColor(-1, 0xff0000) ; Red
EndIf

    GUISetState ()
    
    
    
 While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $unlock
                If $UserObj.IsAccountLocked Then
                $UserObj.IsAccountLocked = False
                $UserObj.SetInfo
                MsgBox(0, 'INFO', "User Account was Unlocked. It will take approximately 5 mins to reflect this change.")
                GUICtrlCreateLabel (""& $UserObj.IsAccountLocked, 100, 430, 10, 20)
                EndIf
            
            Case $msg = $GUI_EVENT_CLOSE
            Exit
        EndSelect
    WEnd
    
    
    
    EndFunc
Edited by joshiieeii

Share this post


Link to post
Share on other sites



$oMyError = ObjEvent("AutoIt.Error", "ComError")
;$IDomain = @ComputerName
$IDomain = 'Your domain name'
$IUserId = InputBox("Username","username")  
$usrWinNT = ObjGet("WinNT://" & $IDomain & "/" & $IUserId)
$Ret = @error
If $Ret Then
    ; User not found
    ConsoleWrite('Not Found' & @lf) ;### Debug Console
Else
    ; User Found
    ConsoleWrite('Found' & @lf) ;### Debug Console
EndIf
Exit

;COM Error function
Func ComError()
    if IsObj($oMyError) Then
        $HexNumber = Hex($oMyError.number, 8)
        SetError($HexNumber)
    endif   
    return 0
EndFunc   ;==>ComError


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

$oMyError = ObjEvent("AutoIt.Error", "ComError")
;$IDomain = @ComputerName
$IDomain = 'Your domain name'
$IUserId = InputBox("Username","username")  
$usrWinNT = ObjGet("WinNT://" & $IDomain & "/" & $IUserId)
$Ret = @error
If $Ret Then
    ; User not found
    ConsoleWrite('Not Found' & @lf) ;### Debug Console
Else
    ; User Found
    ConsoleWrite('Found' & @lf) ;### Debug Console
EndIf
Exit

;COM Error function
Func ComError()
    if IsObj($oMyError) Then
        $HexNumber = Hex($oMyError.number, 8)
        SetError($HexNumber)
    endif   
    return 0
EndFunc   ;==>ComError
Thanks!! I am learning a lot from this code!! :D

Share this post


Link to post
Share on other sites

Since I am still learning about COM objects, is it possible to make and save changes in a similar way?

For Example, if I wanted to unlock someone's account, would it be possible to (with authentication of course) do this via the same script?

Share this post


Link to post
Share on other sites

Since I am still learning about COM objects, is it possible to make and save changes in a similar way?

For Example, if I wanted to unlock someone's account, would it be possible to (with authentication of course) do this via the same script?

Sure:

If $UserObj.IsAccountLocked Then
    $UserObj.IsAccountLocked = False
    $UserObj.SetInfo
EndIf

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Thank you very much JdeB!~!

I am incorporating it into my script....now if I can only get someone to lock their account....hehe :D:wacko:

Open CMD

type command:

Runas /user:testuser cmd

Enter and type wrong password.

repeat this the number of times you need to lockout the account...

:D


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Open CMD

type command:

Runas /user:testuser cmd

Enter and type wrong password.

repeat this the number of times you need to lockout the account...

:wacko:

HAHA...I wish I could do that...If I lock my own account, I can't unlock it, I have to get someone higher than I to unlock it. So yeah..... :D

Share this post


Link to post
Share on other sites

JdeB,

You have any idea how you would format some of the numbers that "Password last changed" and "LastLogin" spits out....

I am getting numbers like this:

20060526091415

20060629143432

I was thinking about a Date Time split, but I am still trying to figure out how I would do that

Let's see....

2006 06 29 143432

YYYY MM DD HHMMSS

So how would you translate that into a MM DD YYYY HH:MM??

Share this post


Link to post
Share on other sites

$s = "20060526091415" 
$Date = StringMid($s, 5, 2) & "/" & StringMid($s, 7, 2) & "/" & StringMid($s, 1, 4)
$Time = StringMid($s, 9, 2) & ":" & StringMid($s, 11, 2) & ":" & StringMid($s, 13, 2)

:D


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Figured it out.......

$number = 20060526091415
$year = StringTrimRight(""& $number, 10)
$month = StringTrimRight(""& $number, 8)
$month = StringTrimleft(""& $month, 4)
$day = StringTrimRight(""& $number, 6)
$day = StringTrimleft(""& $day, 6)  
$hour = StringTrimRight(""& $number, 4)
$hour = StringTrimleft(""& $hour, 8)
$min = StringTrimRight(""& $number, 2)
$min = StringTrimleft(""& $min, 10)
$sec = StringTrimleft(""& $number, 12)

*edit* Boy you are fast JdeB!!

**Edit** And I like your code betta!! Thanks!!

Edited by joshiieeii

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Updated the first post with the latest version and included the major changes and additions.

Thanks a ton JdeB for all your help!! :D

I have truly learned a lot just from what you have shown me.

Edited by joshiieeii

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0