Re: check if computer exists in AD

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hans wrote:

I'm looking for a way to found out wether a computer exists in AD or not.
When running the script, I prompt for a computer-name
which then has to be checked if exists in AD - Computers...

I assume you have the NetBIOS name of a computer. One method would be to use
ADO to search AD for objects that have the corresponding value assigned to
the sAMAccountName attribute. The sAMAccountName of computer objects is the
NetBIOS name of the computer with "$" appended on the end. If you want to
know if the computer "TestComputer" exists in your domain, the LDAP syntax
query could be:

strComputer = "TestComputer$"
strQuery = "<LDAP://ou=Sales,dc=MyDomain,dc=com>;" _

& "(sAMAccountName=" & strComputer & ")" _

& ";distinguishedName;Subtree"



For more info on using ADO to search AD, see this link:



http://www.rlmueller.net/ADOSearchTips.htm



However, a more efficient method would be to use the NameTranslate object.
Convert the given value for sAMAccountName (the NetBIOS name of the computer
with "$" appended) to the distinguishedName. If the object does not exist,
and error is raised on by the Set method. For example:

=========

' Constants for the NameTranslate object.

Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

' Specify the NetBIOS name of the domain.

strDomain = "MyDomain"
' Specify the NetBIOS name of the computer.

strComputer = "TestComputer"

' Use the NameTranslate object to convert the NT name to the
' Distinguished Name required for the LDAP provider.
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.

' Trap the error if object with specifed NT name 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 " & strComputer & " exists."

Else

On Error GoTo 0

Wscript.Echo "Computer " & strComputer & " does NOT exist."

End If



' Use the Get method to retrieve the RPC 1779 Distinguished Name.
strComputerDN = objTrans.Get(ADS_NAME_TYPE_1779)

=============



For more info on NameTranslate, see this link:



http://www.rlmueller.net/NameTranslateFAQ.htm



You can use the InputBox function to prompt for the NetBIOS name of the
computer. You can hard code the NetBIOS name of your domain, or retrieve it
programmatically as shown in the link above on NameTranslate from the
RootDSE object.


--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net


.



Relevant Pages

  • Re: Move computers account to another OU from a txt list
    ... the script worked great!!. ... to the end of the NetBIOS names of the computers. ... ' Constants For the NameTranslate object. ... ' Specify the Distinguished Name of the target OU. ...
    (microsoft.public.windows.server.scripting)
  • Re: Disable and Move to a new OU
    ... I assume your text file is a list of the NetBIOS names of computers, ... ' Constants for the NameTranslate object. ... ' Specify the text file of computer NetBIOS names. ... ' Specify the Distinguished Name of the target OU. ...
    (microsoft.public.scripting.vbscript)
  • Re: move computer to ou
    ... Your using the WINNT provider and converting to a DN which you can then bind to using the ldap provider. ... My guess is that the value assigned to the variable strComputer is the NetBIOS name of the computer. ... I would use the NameTranslate object to convert to the Distinguished Name. ... ' Specify the new container. ...
    (microsoft.public.windows.server.scripting)
  • Re: Move computers account to another OU from a txt list
    ... I have the list of the 7200 computers, but I need to find a script to ... NameTranslate object to convert this to the Distinguished Name. ... ' Specify the NetBIOS name of the domain. ... ' Use the Set method to specify the NT format of the object name. ...
    (microsoft.public.windows.server.scripting)
  • Re: Move computers account to another OU from a txt list
    ... to the end of the NetBIOS names of the computers. ... you could skip the statement that strips the trailing backslash from the ... ' Constants For the NameTranslate object. ... ' Specify the Distinguished Name of the target OU. ...
    (microsoft.public.windows.server.scripting)