VB Script to Disable AD User Accout

I need a VB Scrip that will allow a non admin user to quick disable a AD User account.  The Script needs to prompt the user for the persons first and last name to disable.  It will need to be able to crawel through all Sub OU's to find the user automatically.
What I have so far works like I want it, expect I have to sepecifiy the OU the user is located in which I don't want it needs to find the user by searching through all OU's


Const ADS_UF_ACCOUNTDISABLE = 2

' Prompt for user Common Name.
cn = InputBox("Enter the First & Last Name of the Employee you want to Disable Example: Firstname Lastname")

On Error Resume Next
Set objUser = GetObject _
    ("LDAP://cn=" & CN & ",ou=Facilities,ou=Production,dc=mirrorinc,dc=org")
If (Err.Number = 0) Then
    On Error GoTo 0
    intUAC = objUser.Get("userAccountControl")

    objUser.Put "userAccountControl", intUAC OR ADS_UF_ACCOUNTDISABLE
    objUser.SetInfo
Else
    On Error GoTo 0
    Wscript.Echo " User " & CN & " not found"
End If
MirrorincAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Krzysztof PytkoSenior Active Directory EngineerCommented:
Might be helpful for you. There is ready script for that with explanation
http://www.codeproject.com/KB/vbscript/Disable_Stale_AD_Users.aspx

Regards,
Krzysztof
0
MirrorincAuthor Commented:
that is all really good but way more code then I think I need.  I just need to know how to apply a Domain Wide Search for users to my Code Above.  I see from the link they are doing what I need but I need to know how to apply to my existing code.  I Haven't had to write to many scripts that access AD directly.
0
jawa29Commented:
Hi

This code should do what you want, it will search AD based on the Firstname and Surname supplied then using the distinguishedName will disable the account.

Jawa29
Const ADS_UF_ACCOUNTDISABLE = 2

On Error Resume Next

Set oRoot = GetObject("LDAP://RootDSE")
vDomain = oRoot.Get("DefaultNamingContext")

' Prompt for user Common Name.
sCN = InputBox("Enter the First & Last Name of the Employee you want to Disable Example: Firstname Lastname")

If sCN <> "" Then
	sFirstName = Split(sCN, " ")(0)
	sLastName = Split(sCN, " ")(1)
Else
	WScript.Quit
End If

Set oConnection = CreateObject("ADODB.Connection")
Set oCommand =   CreateObject("ADODB.Command")
oConnection.Provider = "ADsDSOObject"
oConnection.Open "Active Directory Provider"
Set oCommand.ActiveConnection = oConnection
oCommand.Properties("Page Size") = 1

oCommand.CommandText = "<LDAP://" & vDomain & ">;(&((objectCategory=Person)(givenName=" & sFirstName & ")(sn=" & sLastName & ")));distinguishedName;Subtree"
Set oRS = oCommand.Execute

If Not oRS.EOF Then
	sDN = oRS("distinguishedName")
	Set objUser = GetObject("LDAP://cn=" & sCN & ",ou=Facilities,ou=Production,dc=mirrorinc,dc=org")
	
	On Error GoTo 0
		
	intUAC = objUser.Get("userAccountControl")
	
    objUser.Put "userAccountControl", intUAC OR ADS_UF_ACCOUNTDISABLE
    objUser.SetInfo
Else
	Wscript.Echo " User " & sCN & " not found"
End If

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.