Jump to content

Searching for Computer Account with ADSI


_WearingPants

Recommended Posts

Afternoon,

We are using Windows PE in a decommissiong process:

- tech boots to PE via F12 pxe

- vbs script runs that gets Manufacturer, model and Serial number of machine using WMI

- vbs script runs that looks up the serial number to computer name in an SMS 2003 SQL database

- vbs script runs that writes this information to a seperate SQL database for reporting

- vbs script runs that kicks off a gdisk32.exe DOD standard wipedisk

What we would like to do is before the HDD is wiped, delete the machine account from Active Directory for an end-to-end decomm process, with minimal tech involvement. Everything above is wokring fine, apart from the delete from AD bit.

We have code that searches through AD (copied below) but it does not work in PE, it returns an error that the table does not exist (lack of credentials) when it goes to execute the command.

By binding directly to the cmputer object/parent OU in AD we can delete it no worries but the problem is that the AD structure has branches of OU's all over the place and we need to be able to search.

Any ideas?

Rob.

---------------------------------

code

---------------------------------

Dim objRecordSet

Const ADS_SCOPE_SUBTREE = 2

Set objCommand = CreateObject("ADODB.Command")

Set objConnection = CreateObject("ADODB.Connection")

objConnection.Provider = "ADsDSOObject;USER ID=" & DOM & "\" & USERNAME & ";Password=" & PASSWORD

objConnection.Open "Active Directory Provider"

objCommand.ActiveConnection = objConnection

strBase = "<LDAP://dc=xxx,dc=xxx,dc=xxx>"

strFilter = "(&(objectClass=computer)" & " (cn=" & StrCOMPUTERNAME & "))"

strAttributes = "cn,distinguishedname"

strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

objCommand.CommandText = strQuery

objCommand.Properties("Page Size") = 1000

objCommand.Properties("Timeout") = 30

objCommand.Properties("Cache Results") = False

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF

StrNTName = objRecordSet.Fields("cn")

If StrNTName = StrCOMPUTERNAME Then

DN = objRecordSet.Fields("distinguishedname")

End If

objRecordSet.MoveNext

Loop

objConnection.Close

Set objRootDSE = Nothing

Set objCommand = Nothing

Set objConnection = Nothing

Set objRecordSet = Nothing

Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...