Re: check if computer exists in AD
- From: "Richard Mueller" <rlmueller-NOSPAM@xxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 15 Dec 2006 09:55:52 -0600
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
.
- Follow-Ups:
- Re: check if computer exists in AD
- From: Hans
- Re: check if computer exists in AD
- Prev by Date: Re: IpSecurity settings on webfiles with ADSI
- Next by Date: Re: Adding Missing printer if they not there
- Previous by thread: Re: IpSecurity settings on webfiles with ADSI
- Next by thread: Re: check if computer exists in AD
- Index(es):
Relevant Pages
|