I try to use the following code to search and delete computer account from non-domain workstation and it worked for me, hope it works for you too.
Code:
Const ADS_SCOPESUBTREE = 2
Const ADS_SECURE_AUTHENTICATION = 1
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Properties("User_ID") = "test\admin"
objConnection.Properties("Password") = "home"
objConnection.Properties("Encrypt_Password") = True
objConnection.Properties("ADSI Flag") = 1
strComputer = "test"
strDomain = "srv.test.com"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 100
objCommand.Properties("Cache_Results") = False
objCommand.Properties("Search_scope") = ADS_SCOPESUBTREE
objCommand.CommandText = _
"SELECT ADsPath FROM 'LDAP://" & strDomain & "' WHERE objectCategory='computer' " & _
"AND Name='" & strComputer & "'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
strADsPath = ""
While Not objRecordSet.EOF
strADsPath = objRecordSet.Fields("ADsPath").Value
objRecordSet.MoveNext
Wend
If strADsPath = "" Then
MsgBox "Computer not yet found."
Else
MsgBox "Computer path: " & strADsPath
Set objNS = GetObject("LDAP:")
Set objComputer = objNS.OpenDSObject(strADsPath, "test\admin", "home",ADS_SECURE_AUTHENTICATION)
objComputer.DeleteObject (0)
End If
Bookmarks