Solved

Find users who have the same contacts from ADS

Posted on 2007-12-01
9
259 Views
Last Modified: 2010-07-27
We have many users and many contacts that we have the same.Is there a way a script can get only users and contacts that has the same names from the ADS.With the descriptions to a file.

Regards
Sharath
0
Comment
Question by:bsharath
  • 5
  • 2
  • 2
9 Comments
 
LVL 11

Expert Comment

by:cup
ID: 20390609
Please correct me if I'm wrong: I'm not sure about what you're asking or saying.

1) You have many users
2) You have many contacts
3) Some of the users and contacts are the same
4) There is something called the ADS which presumably has some details about the users and contacts.
5) You want to get the users and contacts that have the same name, look them up in the ADS and dump them into a file.

Is that a fair analysis of what you're trying to achieve?  If not, could you please explain what you really want.

Is ADS the Automated Deployment Service for Windows 2003 or is it something completely different?
0
 
LVL 11

Author Comment

by:bsharath
ID: 20390639
You are right but i just want to find users and contacts that have the same Fullname (Displayname)
Any way to get just the users that have contacts and user names the same.
0
 
LVL 11

Expert Comment

by:cup
ID: 20392367
Where are the users and contacts stored?  Is it somewhere in a file or database or do they just login into the system and their id is on some global server.

What is ADS?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 11

Author Comment

by:bsharath
ID: 20393421
The users and contacts are stored in ADS (Active Directory)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20562246
Sharath, try this script:

'====================
If Right(LCase(WScript.FullName), 11) = "wscript.exe" Then
      Set objShell = CreateObject("WScript.Shell")
      objShell.Run "cmd /k cscript """ & WScript.ScriptFullName & """", 1, False
      Set objShell = Nothing
      WScript.Quit
End If

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

 ' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")

strDNSDomain = objRootDSE.Get("defaultNamingContext")
'strOUPath = "ou=users,ou=TestOU,"
strOUPath = ""
strBase = "<LDAP://" & strOUPath & strDNSDomain & ">"

strFilter = "(&(objectCategory=person)(objectClass=contact))"
'(profilePath=" & strOldPath & "*)
'strFilter = "(&(objectClass=computer)(cn=" & strComputer & "))"

' Comma delimited list of attribute values to retrieve.
'strAttributes = "sAMAccountName,cn"
strAttributes = "displayName, distinguishedName"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query for contacts
Set adoContactsRecordset = adoCommand.Execute

' Enumerate the resulting recordset.
Do Until adoContactsRecordset.EOF
      ' Retrieve values and display.
      strContactName = adoContactsRecordset.Fields("displayName").Value
      strContactDN = adoContactsRecordset.Fields("distinguishedName").Value
      WScript.Echo "Checking Contact: " & strContactName & ": " & strContactDN

      'Change and run the query for user accounts
      strFilter = "(&(objectCategory=person)(objectClass=user)(displayName=" & strContactName & "))"
      strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
      adoCommand.CommandText = strQuery
      Set adoUsersRecordset = adoCommand.Execute

      Do Until adoUsersRecordset.EOF
            strUserName = adoUsersRecordset.Fields("displayName").Value
            strUserDN = adoUsersRecordset.Fields("distinguishedName").Value
            If LCase(strContactName) = LCase(strUserName) Then
                  WScript.Echo
                  WScript.Echo ">>> " & "CONTACT " & strContactName & ": " & strContactDN
                  WScript.Echo ">>> " & "USER    " & strUserName & ": " & strUserDN
                  WScript.Echo
            End If
            adoUsersRecordset.MoveNext
      Loop

      ' Move to the next record in the recordset.
      adoContactsRecordset.MoveNext
Loop

' Clean up.
adoContactsRecordset.Close
Set adoContactsRecordset = Nothing

adoConnection.Close

WScript.Echo
WScript.Echo "Done"
MsgBox "Done"
'====================

Regards,

Rob.
0
 
LVL 11

Author Comment

by:bsharath
ID: 20562313
Thanks Rob this works perfect.
How does the script nmatch the contacts and NTlogins?
Can i get the output to a file.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 20562342
It takes each full Display Name of each Contact object, then searches for a full Display Name from a User object that matches.

This will also output the same text to a file:

'=================
If Right(LCase(WScript.FullName), 11) = "wscript.exe" Then
      Set objShell = CreateObject("WScript.Shell")
      objShell.Run "cmd /k cscript """ & WScript.ScriptFullName & """", 1, False
      Set objShell = Nothing
      WScript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")
strOutputFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Contacts_With_Same_Display_Name_As_Users.txt"
Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

 ' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")

strDNSDomain = objRootDSE.Get("defaultNamingContext")
'strOUPath = "ou=users,ou=TestOU,"
strOUPath = ""
strBase = "<LDAP://" & strOUPath & strDNSDomain & ">"

strFilter = "(&(objectCategory=person)(objectClass=contact))"
'(profilePath=" & strOldPath & "*)
'strFilter = "(&(objectClass=computer)(cn=" & strComputer & "))"

' Comma delimited list of attribute values to retrieve.
'strAttributes = "sAMAccountName,cn"
strAttributes = "displayName, distinguishedName"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query for contacts
Set adoContactsRecordset = adoCommand.Execute

' Enumerate the resulting recordset.
Do Until adoContactsRecordset.EOF
      ' Retrieve values and display.
      strContactName = adoContactsRecordset.Fields("displayName").Value
      strContactDN = adoContactsRecordset.Fields("distinguishedName").Value
      WScript.Echo "Checking Contact: " & strContactName & ": " & strContactDN

      'Change and run the query for user accounts
      strFilter = "(&(objectCategory=person)(objectClass=user)(displayName=" & strContactName & "))"
      strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
      adoCommand.CommandText = strQuery
      Set adoUsersRecordset = adoCommand.Execute

      Do Until adoUsersRecordset.EOF
            strUserName = adoUsersRecordset.Fields("displayName").Value
            strUserDN = adoUsersRecordset.Fields("distinguishedName").Value
            If LCase(strContactName) = LCase(strUserName) Then
                  WScript.Echo
                  WScript.Echo ">>> " & "CONTACT " & strContactName & ": " & strContactDN
                  WScript.Echo ">>> " & "USER    " & strUserName & ": " & strUserDN
                  WScript.Echo
                  objOutputFile.WriteLine ""
                  objOutputFile.WriteLine ">>> " & "CONTACT " & strContactName & ": " & strContactDN
                  objOutputFile.WriteLine ">>> " & "USER    " & strUserName & ": " & strUserDN
                  objOutputFile.WriteLine ""
            End If
            adoUsersRecordset.MoveNext
      Loop

      ' Move to the next record in the recordset.
      adoContactsRecordset.MoveNext
Loop

' Clean up.
adoContactsRecordset.Close
Set adoContactsRecordset = Nothing

adoConnection.Close

objOutputFile.Close
Set objOutputFile = Nothing
Set objFSO = Nothing

WScript.Echo
WScript.Echo "Done"
MsgBox "Done"
'=================

Regards,

Rob.
0
 
LVL 11

Author Closing Comment

by:bsharath
ID: 31412102
Excellent Rob....
0
 
LVL 11

Author Comment

by:bsharath
ID: 20562474
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
rhino JavaScript import, load 25 94
React or Angular? 6 81
Excel file not created as expected 7 79
Where to download python requests library 6 30
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question