How can i check each users Direct reportees if they have both Ntlogin & Contact added to them. (Active directory)

Hi,

How can i check each users Direct reportees if they have both Ntlogin & Contact added to them.
In many users i could see the Ntlogin and Contact both have added and they show duplicates. Is there any way i can scan all users in an OU and find the Users who have reportees with the same names with contacts and users accounts.

Regards
Sharath
LVL 11
bsharathAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tigermattCommented:

How's this? Program in your OU at the top where the Managers are found. Then this script goes and scans all the users, checks direct reportees, and if one direct reportee has the same name, it will throw an error.

Run at a cscript prompt so you can catch the output.

-Matthew

' Set OU where the *Managers* can be found, below
strOUPath = "OU=Advanced Level,OU=Finance Officers,OU=Management,OU=Departments,OU=Internal Users,OU=Users,OU=,DC=company,DC=internal"
 
Set objOU = GetObject("LDAP://" & strOUPath)
 
Dim objReports()
 
For Each objUser in objOU
	
	intArrMax = 0
 
	If TypeName(objUser.directReports) = "Variant()" Then
		
		
		For Each strValue In objUser.directReports
			
			Set objReport = GetObject("LDAP://" & strValue)
			lookupname = objReport.givenName & " " & objReport.sn
			
			For Each x In objReports
				If x = lookupname Then
					WScript.Echo "Direct Report: " & lookupname & " (Manager: " & objUser.cn & ") duplicated!"
				End If
			Next
			redim preserve objReports(intArrMax)
			objreports(intArrMax) = lookupname
			intArrMax = intArrMax + 1
			Set objReport = Nothing
			lookupname = ""
		Next
		
	End If
	
	redim objReports(0)
	
 
Next

Open in new window

0
bsharathAuthor Commented:
Thank U shall check and get back...
0
bsharathAuthor Commented:
The script runs but no results...

I have a user who has the same names user and contact in the managers. But the script does not detect
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

tigermattCommented:

Well it did work, but now doesn't :-(

Will work on and get back.
0
tigermattCommented:

Ok, I've just rewritten it from scratch. The main points to consider are:

*Ensure you run it from the command prompt using cscript, as it uses WScript.Echo to give the output. If you just double-click the VBS you will not see any output (this is expected).

*Objects in Active Directory CANNOT have the same Full Name. It must be unique. Thus, the only way I have been able to compare whether a User and Contact are the same user are based on their First Name and Surname. The First Name and Surname fields must match between the user and computer accounts for a duplicate to be detected. Let me know if you want me to use Display Name, or some other field to compare, instead.

-Matthew
' Set OU where the *Managers* can be found, below
strOUPath = "OU=Standard Users,OU=Users,OU=GJB,DC=GJB,DC=Bawden"
 
Set objOU = GetObject("LDAP://" & strOUPath)
 
Dim objReports()
 
For Each objUser in objOU
        
        intArrMax = 0
 
        If (TypeName(objUser.directReports) = "Variant()") Then
		
		WScript.Echo "Processing " & objUser.givenName & " " & objUser.sn & " for duplicated Direct Reports"
		
		For Each strValue in objUser.directReports
			
			' Fetch object for this direct report
			Set objReportee = GetObject("LDAP://" & strValue)
			
			lookupname = objReportee.givenName & " " & objReportee.sn
			
			' Loop through the list of users we've already seen to see if this one is duplicated
			For Each strAlreadyProcessed In objReports
				
				If (LCase(strAlreadyProcessed) = LCase(lookupname)) Then
					
					' Duplicate detected
					WScript.Echo "** Duplicate Detected: " & lookupname & " (Manager: " & objUser.cn & ") **"
				
				End If
				
			Next
			
			' Add lookup name to array
			intArrMax = intArrMax + 1
			redim preserve objReports(intArrMax)
			objReports(intArrMax) = lookupname
			
		Next
	
	End If
        
        redim objReports(0)
        
 
Next

Open in new window

0
tigermattCommented:

And just to confirm, when I ran it on an OU where my Managers are located, it did find a duplicated entry where the first name and surname matched between the two.
0
bsharathAuthor Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Check all users if they have users and contacts as direct reportees.vbs
Line:      2
Char:      63
Error:      Unterminated string constant
Code:      800A0409
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------

When i commented line 2 i get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Check all users if they have users and contacts as direct reportees.vbs
Line:      49
Char:      2
Error:      Unterminated string constant
Code:      800A0409
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
0
bsharathAuthor Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Check all users if they have users and contacts as direct reportees.vbs
Line:      2
Char:      63
Error:      Unterminated string constant
Code:      800A0409
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------

When i commented line 2 i get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Check all users if they have users and contacts as direct reportees.vbs
Line:      49
Char:      2
Error:      Unterminated string constant
Code:      800A0409
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
0
tigermattCommented:

Can you copy and paste the whole script you are using to a comment here?
Thanks!
0
tigermattCommented:

I mean the VBS you have stored with the OU path in it?
0
bsharathAuthor Commented:
Here it is
' Set OU where the *Managers* can be found, below
'strOUPath = "' Set OU where the *Managers* can be found, below
strOUPath = "OU=Test,dc=Development,dc=Group,dc=co,dc=uk"
 
Set objOU = GetObject("LDAP://" & strOUPath)
 
Dim objReports()
 
For Each objUser in objOU
        
        intArrMax = 0
 
        If (TypeName(objUser.directReports) = "Variant()") Then
		
		WScript.Echo "Processing " & objUser.givenName & " " & objUser.sn & " for duplicated Direct Reports"
		
		For Each strValue in objUser.directReports
			
			' Fetch object for this direct report
			Set objReportee = GetObject("LDAP://" & strValue)
			
			lookupname = objReportee.givenName & " " & objReportee.sn
			
			' Loop through the list of users we've already seen to see if this one is duplicated
			For Each strAlreadyProcessed In objReports
				
				If (LCase(strAlreadyProcessed) = LCase(lookupname)) Then
					
					' Duplicate detected
					WScript.Echo "** Duplicate Detected: " & lookupname & " (Manager: " & objUser.cn & ") **"
				
				End If
				
			Next
			
			' Add lookup name to array
			intArrMax = intArrMax + 1
			redim preserve objReports(intArrMax)
			objReports(intArrMax) = lookupname
			
		Next
	
	End If
        
        redim objReports(0)
        
 
Next
"
 
Set objOU = GetObject("LDAP://" & strOUPath)
 
Dim objReports()
 
For Each objUser in objOU
	
	intArrMax = 0
 
	If TypeName(objUser.directReports) = "Variant()" Then
		
		
		For Each strValue In objUser.directReports
			
			Set objReport = GetObject("LDAP://" & strValue)
			lookupname = objReport.givenName & " " & objReport.sn
			
			For Each x In objReports
				If x = lookupname Then
					WScript.Echo "Direct Report: " & lookupname & " (Manager: " & objUser.cn & ") duplicated!"
				End If
			Next
			redim preserve objReports(intArrMax)
			objreports(intArrMax) = lookupname
			intArrMax = intArrMax + 1
			Set objReport = Nothing
			lookupname = ""
		Next
		
	End If
	
	redim objReports(0)
	
 
Next

Open in new window

0
bsharathAuthor Commented:
Here it is
' Set OU where the *Managers* can be found, below
'strOUPath = "' Set OU where the *Managers* can be found, below
strOUPath = "OU=Test,dc=Development,dc=Group,dc=co,dc=uk"
 
Set objOU = GetObject("LDAP://" & strOUPath)
 
Dim objReports()
 
For Each objUser in objOU
        
        intArrMax = 0
 
        If (TypeName(objUser.directReports) = "Variant()") Then
		
		WScript.Echo "Processing " & objUser.givenName & " " & objUser.sn & " for duplicated Direct Reports"
		
		For Each strValue in objUser.directReports
			
			' Fetch object for this direct report
			Set objReportee = GetObject("LDAP://" & strValue)
			
			lookupname = objReportee.givenName & " " & objReportee.sn
			
			' Loop through the list of users we've already seen to see if this one is duplicated
			For Each strAlreadyProcessed In objReports
				
				If (LCase(strAlreadyProcessed) = LCase(lookupname)) Then
					
					' Duplicate detected
					WScript.Echo "** Duplicate Detected: " & lookupname & " (Manager: " & objUser.cn & ") **"
				
				End If
				
			Next
			
			' Add lookup name to array
			intArrMax = intArrMax + 1
			redim preserve objReports(intArrMax)
			objReports(intArrMax) = lookupname
			
		Next
	
	End If
        
        redim objReports(0)
        
 
Next
"
 
Set objOU = GetObject("LDAP://" & strOUPath)
 
Dim objReports()
 
For Each objUser in objOU
	
	intArrMax = 0
 
	If TypeName(objUser.directReports) = "Variant()" Then
		
		
		For Each strValue In objUser.directReports
			
			Set objReport = GetObject("LDAP://" & strValue)
			lookupname = objReport.givenName & " " & objReport.sn
			
			For Each x In objReports
				If x = lookupname Then
					WScript.Echo "Direct Report: " & lookupname & " (Manager: " & objUser.cn & ") duplicated!"
				End If
			Next
			redim preserve objReports(intArrMax)
			objreports(intArrMax) = lookupname
			intArrMax = intArrMax + 1
			Set objReport = Nothing
			lookupname = ""
		Next
		
	End If
	
	redim objReports(0)
	
 
Next

Open in new window

0
tigermattCommented:

I see your problem. You have the script actually copied twice! If you look on line 49, you have a " mark which should not be there, and neither should anything after that point either.

Open up a clean Notepad and copy and paste the below, then save it. I've put the OU path in already for you.

-Matthew
' Set OU where the *Managers* can be found, below
strOUPath = "OU=Test,dc=Development,dc=Group,dc=co,dc=uk"
 
Set objOU = GetObject("LDAP://" & strOUPath)
 
Dim objReports()
 
For Each objUser in objOU
        
        intArrMax = 0
 
        If (TypeName(objUser.directReports) = "Variant()") Then
		
		WScript.Echo "Processing " & objUser.givenName & " " & objUser.sn & " for duplicated Direct Reports"
		
		For Each strValue in objUser.directReports
			
			' Fetch object for this direct report
			Set objReportee = GetObject("LDAP://" & strValue)
			
			lookupname = objReportee.givenName & " " & objReportee.sn
			
			' Loop through the list of users we've already seen to see if this one is duplicated
			For Each strAlreadyProcessed In objReports
				
				If (LCase(strAlreadyProcessed) = LCase(lookupname)) Then
					
					' Duplicate detected
					WScript.Echo "** Duplicate Detected: " & lookupname & " (Manager: " & objUser.cn & ") **"
				
				End If
				
			Next
			
			' Add lookup name to array
			intArrMax = intArrMax + 1
			redim preserve objReports(intArrMax)
			objReports(intArrMax) = lookupname
			
		Next
	
	End If
        
        redim objReports(0)
        
 
Next

Open in new window

0
bsharathAuthor Commented:
Sorry for that shall check and get back...
0
tigermattCommented:

It's not a problem, I like tracking bugs down and fixing them :-)
0
bsharathAuthor Commented:
Works Perfect now without the boxes how can i get the results to a file

With the user & Contact & Manager name

When found log the data to the file
0
tigermattCommented:

Can't test it at the moment but try the following - it should do the trick. Specify the log file in strLog = "...".

-Matthew
Const ForWriting = 2
 
' Set OU where the *Managers* can be found, below
strOUPath = "OU=Test,dc=Development,dc=Group,dc=co,dc=uk"
 
'Log File path
strLog = "C:\logfile.log"
 
Set objOU = GetObject("LDAP://" & strOUPath)
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
 
Set logFile = fso.OpenTextFile(strLog, ForWriting, true)
logFile.WriteLine vbCrLf & vbCrLf & "** SCRIPT RUN STARTING @ " & NOW() & " **"
 
Dim objReports()
 
For Each objUser in objOU
        
        intArrMax = 0
 
        If (TypeName(objUser.directReports) = "Variant()") Then
		
		logFile.WriteLine "Processing " & objUser.givenName & " " & objUser.sn & " for duplicated Direct Reports"
		
		For Each strValue in objUser.directReports
			
			' Fetch object for this direct report
			Set objReportee = GetObject("LDAP://" & strValue)
			
			lookupname = objReportee.givenName & " " & objReportee.sn
			
			' Loop through the list of users we've already seen to see if this one is duplicated
			For Each strAlreadyProcessed In objReports
				
				If (LCase(strAlreadyProcessed) = LCase(lookupname)) Then
					
					' Duplicate detected
					logFile.WriteLine "** Duplicate Detected: " & lookupname & " (Manager: " & objUser.cn & ") **"
				
				End If
				
			Next
			
			' Add lookup name to array
			intArrMax = intArrMax + 1
			redim preserve objReports(intArrMax)
			objReports(intArrMax) = lookupname
			
		Next
	
	End If
        
        redim objReports(0)
        
 
Next
 
logFile.WriteLine vbCrLf & "** ENDED PROCESSING OF RUN **" & vbCrLf & "**********************************" & vbCrLf & vbCrLf
Set fso = Nothing
Set logFile = Nothing

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bsharathAuthor Commented:
Thank U worked perfect
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.