Is there a simple way to do this? I done some scripting but never any LDAP
queires,only stuff using the WinNT provider. The only thing I can think of
is trying to bind to a computer account using the WinNT provider, if it
gives me an error the computer name does not exist. their must be a better
way to do this. Can someone throw me a link?
Thanks in advance
Hi,
Given the NetBIOS name of the computer (and the NetBIOS name of the domain),
there are several ways to determine if the corresponding object (computer
account) exists in AD.
1. Bind with the WinNT provider and trap the error if the object does not
exist:
=========
strComputer = "MyComputer"
strDomain = "MyDomain"
On Error Resume Next
Set objComputer = GetObject("WinNT://" & strDomain & "/" & strComputer &
",computer")
If (Err.Number = 0) Then
On Error GoTo 0
Wscript.Echo "Computer object exists in AD."
Else
On Error GoTo 0
Wscript.Echo "Computer object does not exist in AD."
End If
=========
2. Use the NameTranslate object to retrieve the Distinguished Name of the
computer. Again, trap the error if the object does not exist, which will be
raised by the Set method:
=========
strComputer = "MyComputer"
strDomain = "MyDomain"
' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
' Use the NameTranslate object to convert the NetBIOS name to the
' Distinguished Name.
Set objTrans = CreateObject("NameTranslate")
' Initialize NameTranslate by locating the Global Catalog.
objTrans.Init ADS_NAME_INITTYPE_GC, ""
' Use the Set method to specify the NT format of the object name.
' Append "$" to the end of the NetBIOS name of the computer.
' Trap error if computer object does not exist.
On Error Resume Next
objTrans.Set ADS_NAME_TYPE_NT4, strDomain & "\" & strComputer & "$"
If (Err.Number = 0) Then
On Error GoTo 0
Wscript.Echo "Computer object exists in AD."
Else
On Error GoTo 0
Wscript.Echo "Computer object does not exist in AD."
End If
' Use the Get method to retrieve the RPC 1779 Distinguished Name.
' With this you can bind with the LDAP provider, if desired.
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
==========
3. Use ADO to retrieve the Distinguished Name of the computer that has
sAMAccountName equal to the NetBIOS name of the computer with "$" appended
to the end:
===========
strComputer = "MyComputer"
strDomain = "dc=MyDomain,dc=com"
' Setup ADO objects.
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
' Search entire domain.
strBase = "<LDAP://" & strDomain & ">"
' Filter on computer objects with given sAMAccountName.
' sAMAccountName is the NetBIOS name with "$" appended.
strFilter = "(&(objectCategory=computer)(sAMAccountName=" & strComputer &
"$))"
' Comma delimited list of attribute values to retrieve.
strAttributes = "distinguishedName"
' Construct the query, using LDAP syntax.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
' Run the query.
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
' Check if record retrieved.
If adoRecordset.EOF Then
Wscript.Echo "Computer object does not exist in AD."
Else
Wscript.Echo "Computer object exists in AD."
End If
============
The first method is the easiest to code, but probably the slowest, as the
WinNT provider is inefficent. The second method (NameTranslate) is probably
the fastest. The third method (ADO) can be used to retrieve more
information.
For more on NameTranslate:
http://www.rlmueller.net/NameTranslateFAQ.htm
For more on using ADO to retrieve attribute values from AD:
http://www.rlmueller.net/ADOSearchTips.htm
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net