We help IT Professionals succeed at work.

vbscript: Getting "Unknown Error" (0x80005000) when connecting to LDAP (Windows Server 2008R2)

I am using the following code as part of a script that I use to join computers to the domain :



DeleteExistingComputerAccount

WScript.Quit(0)


' ===================================================================

Sub DeleteExistingComputerAccount()

    msgbox "Delete Existing"

    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.CommandText = "Select Name from 'LDAP://DC=altasens,DC=com' Where objectClass='computer'"  
    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

    Set objRecordSet = objCommand.Execute

    objRecordSet.MoveFirst

    Do Until objRecordSet.EOF

        'Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
        'Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value

        If objRecordSet.Fields("Name").Value = ComputerName Then

        msgbox "found existing Computer account for " & ComputerName

            Set objComputer = GetObject("LDAP://CN=" & ComputerName & "CN=Computers,DC=altasens,DC=com")
            objComputer.DeleteObject (0)

            Exit Do

        End If

        objRecordSet.MoveNext

    Loop

End Sub


Function ComputerName()

    On Error Resume Next
   
    Dim objWMI : Set objWMI = GetObject("winmgmts:")
    Dim colSettingsComp : Set colSettings = objWMI.ExecQuery("Select * from Win32_ComputerSystem")
    Dim colSettingsBios : Set colSettingsBios = objWMI.ExecQuery("Select * from Win32_BIOS")
    Dim objComputer
   
    For Each objComputer in colSettingsBios
   
      ComputerName = Trim(objComputer.SerialNumber)
   
    Next
   
    On Error Goto 0

    If ComputerName = "" Then

        MsgBox "ERROR: Can't determine serial number",vbCritical, "Rename Local Machine"
        WScript.Quit(0)

    End If

End Function


I get an error message (0x80005000) on this line :


           Set objComputer = GetObject("LDAP://CN=" & ComputerName & "CN=Computers,DC=altasens,DC=com")



What I am essentially trying to do here is to reference a specific computer (ComputerName)  however I won't know for sure in what specific container it resides.   So here are my questions:

1.  is there anything obviously wrong with the above LDAP query?  (I got this from activexperts.com)

2.  is there a way to retrieve a computer object regardless of what specific container or OU it is located in?  (necessary for this process to work)


As always, thanks for the help, Experts!
Comment
Watch Question

LAN/WAN Systems Administrator
CERTIFIED EXPERT
Commented:
Try using

Set ADInfo = CreateObject("ADSystemInfo")
Set objComputer = GetObject("LDAP://" & ADInfo.ComputerName)

This should retrieve the computer's object from Active Directory.

Author

Commented:
I was able to solve this by pulling the distinguished name and using that in the GetObject statement.

Author

Commented:
thanks for the suggestion!