Advertisement

05.01.2008 at 06:50AM PDT, ID: 23368467
[x]
Attachment Details

Search for and delete objects in Active Directory

Asked by astranoc in Active Directory, VB Script

Tags: VBscript

Hi,

I've got a script that reads computer account names from a text file, then seaches for and deletes them from Active Directory. The script works perfectly when deleteing computer accounts. I'm however unable to adapt it to do the same with user accounts. The only thing that I should change is "objectClass = 'user'". It doesn't work though.

Every time I execute the user script I get the following error:

C:\script\deluseracc.vbs(25, 1) ADODB.Recordset: Either BOF or EOF is True, or t
he current record has been deleted. Requested operation requires a current record.

The account for sure is there. I know that this error can be caused by script trying to walk though recordset from the end. I don't understand why it's not doing it with the "computer version" of it.

The source codes are included below, please note that I didn;t bother to change variable names so where it should be strUser it's strComputer

I appriciate any suggestions.

Regards,
KarolStart Free Trial
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:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
properly working computer version:
 
Const ADS_SCOPE_SUBTREE = 2
 
TxtFile = "comps.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(TxtFile)
 
Do until  objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
     
    strComputer = strNextLine
   
   
   Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
 
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "Select ADsPath From " & _
    "'LDAP://DC=xxxx,DC=xxxx' Where objectClass='computer'" & _
        " and Name = '" & strComputer & "'"  
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
 
Do Until objRecordSet.EOF
    Set objComputer = GetObject(objRecordSet.Fields("ADsPath").Value)
    objComputer.DeleteObject (0)
    objRecordSet.MoveNext
Loop
 
*******************************************
not working "user version":
 
 
Const ADS_SCOPE_SUBTREE = 2
 
TxtFile = "users.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(TxtFile)
 
Do until  objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
     
    strComputer = strNextLine
   
   
   Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
 
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "Select ADsPath From " & _
    "'LDAP://DC=xxxx,DC=xxxx' Where objectClass='user'" & _
        " and Name = '" & strComputer & "'"  
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
 
Do Until objRecordSet.EOF
    Set objComputer = GetObject(objRecordSet.Fields("ADsPath").Value)
    objComputer.DeleteObject (0)
    objRecordSet.MoveNext
Loop
   
Loop
[+][-]05.01.2008 at 02:57PM PDT, ID: 21483092

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Active Directory, VB Script
Tags: VBscript
Sign Up Now!
Solution Provided By: RobSampson
Participating Experts: 2
Solution Grade: B
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628