VBScript - Trying to find disabled users in AD from a spreadsheet

April 23rd, 2012 - 05:15 pm ET by Chris | Report spam
Hello,

This time I brought code. :) I'm trying to read an excel spreadsheet
and see if there are any users in that spreadsheet that are disabled
in AD, and if so, i need it to be flagged in column B as such:
I can get the the script to return a value if I leave out the
useraccountcontrol part (but all that is doing is seeing of the
account exists.) I'd like to be able to see if the account exists and
it is disabled, flag it as suchIf anyone could provide some help,
I'd really appreciate it. I've been working on this for 4 hours
today. :(

Thanks in advance


Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("c:\info
\CurrentCustodians.xls")
objExcel.Visible = True

i = 1

Do Until objExcel.Cells(i, 1).Value = ""
strName = objExcel.Cells(i,1)
objCommand.CommandText = _
"SELECT * FROM 'LDAP://dc=ughome,dc=com' WHERE objectCategory='user'
" & _
"AND samAccountName='" & strName & "'AND
UserAccountControl:1.2.840.113556.1.4.803:=2"

Set objRecordSet = objCommand.Execute

If objRecordset.RecordCount = 1 Then
objExcel.Cells(i,2) = "User is Disabled"

Else
objExcel.Cells(i,2) = "User is Active"

End If

i = i + 1
objRecordset.Close
Loop

objConnection.Close
email Follow the discussionReplies 1 replyReplies Make a reply

Replies

#1 GS
April 23rd, 2012 - 05:41 pm ET | Report spam
<FWIW>
Since you're using ADODB it's not necessary to create an instance of
Excel and open the workbook. ADODB can read the file same as a
database. Each worksheet is a 'table' and so can be loaded into a
recordset for processing accordingly. The table can also be updated in
the usual ADODB fashion. All is doable without starting Excel or
opening the workbook!

This assumes, of course, that the data stored in the Excel file is
formatted as it would be in a database. You just need to use the JET or
ACE data provider for the version of MS Office and you're good to go.

Alternatively, if the file is a data dump to a CSV file that you open
in Excel to save as XLS then you have even more efficient ways
available to do your task without this intermediate step.

Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion

Similar topics