Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Re: Check if a list of user IDs exist/disabled

Reply
Thread Tools Display Modes

Re: Check if a list of user IDs exist/disabled

 
 
Richard Mueller [MVP]
Guest
Posts: n/a

 
      04-28-2009

"Tom" <> wrote in message
news:de24e323-581c-4bd4-82a0-...
> Hello
>
> I have a list of users & I would like to check via a script if their
> IDs exist in Active Directory & whether these IDs have been disabled.
> Thanks


If the list of users is a text file, one name per line, and the names are
the "pre-Windows 2000 logon" names, it would be most efficient to use the
IADsNameTranslate interface in a VBScript program to check for existence by
attempting to convert into the Distinguished Name. However, you would then
need to bind to the user object to find out if the account is disabled.
Overall, it might be best to use ADO to search AD for each user. The ADO
query can retrieve the value of the userAccountControl attribute, which will
indicate if the user is enabled. For example (not tested):
===========
Option Explicit

Dim objRootDSE, strDNSDomain, adoCommand, adoConnection
Dim strBase, strFilter, strAttributes, strQuery, adoRecordset
Dim strUserDFN, strFile, objFSO, objFile, strName, lngFlag

Const ForReading = 1
Const ADS_UF_ACCOUNTDISABLE = &H02

' Specify text file of user "pre-Windows 2000 logon" names.
strFile = "c:\scripts\users.txt"

' Open the file for read access.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = obFSO.OpenTextFile(strFile, ForReading)

' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use ADO to search Active Directory.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection

' Search entire domain.
strBase = "<LDAP://" & strDNSDomain & ">"

' Comma delimited list of attribute values to retrieve.
strAttributes = "distinguishedName,userAccountControl"

adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Read the each line of the file.
Do Until objFile.AtEndOfStream
strName = Trim(objFile.ReadLine)
' Skip blank lines.
If (strName <> "") Then
' Search for user.
strFilter = "(&(objectCategory=person)(objectClass=user)" _
& "(sAMAccountName=" & strName & "))"

' Construct the LDAP query.
strQuery = strBase & ";" & strFilter & ";" _
& strAttributes & ";subtree"

' Run the query.
adoCommand.CommandText = strQuery
Set adoRecordset = adoCommand.Execute

If (adoRecordset.EOF = True) Then
Wscript.Echo "User " & strName & " does not exist."
End If

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
' Retrieve values.
strUserDN = adoRecordset.Fields("distinguishedName").Value
lngFlag = CLng(adoRecordset.Fields("userAccountControl").Val ue)
If (lngFlag And ADS_UF_ACCOUNTDISABLE) <> 0 Then
Wscript.Echo "User " strUserDN & " is disabled."
Else
Wscript.Echo "User " strUserDN & " is NOT disabled."
End If
adoRecordset.MoveNext
Loop
adoRecordset.Close
End If
Loop

' Clean up.
adoConnection.Close

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Check if a list of user IDs exist/disabled Mathieu CHATEAU Scripting 0 04-28-2009 01:52 PM
How to check user account creation and modification (disabled) Jeff Active Directory 2 08-11-2006 06:36 PM
List Date a user was disabled Jeff Scripting 1 05-26-2005 04:38 PM
Generate List of Disabled User Accounts latour Active Directory 2 02-23-2004 03:30 AM
How-to check if user is disabled (in AD) Hadi Scripting 3 09-03-2003 11:59 AM



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