We help IT Professionals succeed at work.
Get Started

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

Last Modified: 2012-05-12
I am using the following code as part of a script that I use to join computers to the domain :



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

Sub DeleteExistingComputerAccount()

    msgbox "Delete Existing"


    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


    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



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)
    On Error Goto 0

    If ComputerName = "" Then

        MsgBox "ERROR: Can't determine serial number",vbCritical, "Rename Local Machine"

    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!
Watch Question
LAN/WAN Systems Administrator
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE