vbscript to enable all disabled users in a specific OU

Posted on 2011-10-31
Last Modified: 2013-12-24
dear experts!

i have a server 2003 with DC. under that DC i have an OU.
i have a bunch of disabled users that i want to enable and write the list of the disabled users to a text log file with a time stamp.

can any one help?!?!?

all the best!
Question by:hamzani
    LVL 35

    Accepted Solution

    Here's a script for that, you can run it with:

    cscript //nologo enable.vbs >> enable.log

    Open in new window

    Option Explicit
    Const C_LDAP_SERVER = ""
    Const C_LDAP_DOMAIN = "dc=DOMAIN,dc=local"
    Const C_LDAP_OU = "TheOU"
    Const C_LDAP_USR = "DOMAIN\administrator"
    Const C_LDAP_PWD = "xxx"
    Dim objConnection, objCommand, objRecordSet, strUser, strDN, objUser, blnDisabled
    Set objConnection = CreateObject("ADODB.Connection")
    objConnection.Open "Provider=ADsDSOObject;",C_LDAP_USR,C_LDAP_PWD
    Set objCommand = CreateObject("ADODB.Command")
    Set objCommand.ActiveConnection = objConnection
    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("SearchScope") = 2 '' ADS_SCOPE_SUBTREE
    objCommand.CommandText = _
      "<LDAP://" & IIF(C_LDAP_SERVER = "", "", C_LDAP_SERVER & "/") & IIF(C_LDAP_OU = "", "", "OU=" & C_LDAP_OU & ",") & C_LDAP_DOMAIN & ">;" & _
      "(&(objectCategory=User)(userAccountControl:1.2.840.113556.1.4.803:=2));sAMAccountName,distinguishedName;OneLevel" '' or: subtree
    Set objRecordSet = objCommand.Execute
    While Not objRecordset.EOF
    	strUser = objRecordSet.Fields("sAMAccountName").Value & ""
    	strDN = objRecordSet.Fields("distinguishedName").Value & ""
    	Set objUser = GetObject("LDAP://" & IIF(C_LDAP_SERVER = "", "", C_LDAP_SERVER & "/") & strDN)
    	If Not objUser Is Nothing Then
    		WScript.Echo Now() & " - enable account " & strUser
    		''objUser.AccountDisabled = False
    		Set objUser = Nothing
    	End If
    Set objRecordset = Nothing
    Set objCommand.ActiveConnection = Nothing
    Set objCommand = Nothing
    Set objConnection = Nothing
    Function IIF(a,b,c)
    	If a Then IIF=b Else IIF=c
    End Function

    Open in new window

    This is from various sources, put together. The actual enabling is not tested.

    Author Comment

    tnx robert!

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now