This script that find Direct reportees who have the contact and users within them. Need to remove the User account.

Hi,

This script that find Direct reportees who have the contact and users within them. Need to remove the User account.
this code is from Matthew.
Need to be able to remove the contact or User.

Please even suggest on what to remove the contact or user Nt account and add that feature in the below script.

REgards
Sharath
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

LVL 11
bsharathAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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:

Hi Sharath,

This will do it. When you run the script, you will need to specify a parameter on the command line. There are two modes: logging mode and deletion mode. I would suggest you run it in the logging mode first (just run it normally as you have been doing for any VBS script for logging mode). In this mode, it simply checks through and records any duplicates it finds in the log file. Once you check the duplicates list, you should then run the script in delete mode by entering a 2 at the prompt (i.e. cscript C:\myscript.vbs 2). By doing this, the script will then go through and delete all the NTLogins for the duplicated accounts which were previously logged. It adds this change to the log.

Let me know if you need any more changes, and remember, don't run it in mode 2 first, otherwise it will delete all the duplicate accounts!

Matthew
on error resume next
Const ForWriting = 2
Const ForAppending = 8
 
' Set OU where the *Managers* can be found, below
strOUPath = "ou=myou,dc=dc,dc=co,dc=uk"
 
'Log File path
strLog = "C:\logfile.log"
 
'Logging or deletion mode?
Dim args: Set args = WScript.Arguments
If args.Count > 0 Then
	Dim vbsmode: vbsmode = CInt(args.Item(0))
	
	If vbsmode > 2 OR vbsmode < 1 Then
		WScript.Echo "Invalid parameter for VBS mode entered"
	End If
	
	If vbsmode = 1 Then
		WScript.Echo "Running in logging mode"
	Elseif vbsmode = 2 Then
		WScript.Echo "Running in deletion mode. Are you sure?"
		proceedVal = MsgBox("The script is running in deletion mode. This means it will delete the NtLogins of any users which have a duplicate contact and user account."&vbcrlf&vbcrlf& _
			"Do you wish to proceed?", 36, "Deletion Mode Warning")
		
		If proceedVal <> 6 Then
			WScript.Echo "Reverting to logging mode"
			vbsmode = 1
		End If
		
	End If
Else
	WScript.Echo "Defaulting to logging mode"
	 vbsmode = 1
End if
 
WScript.Echo "VBS Mode: " & vbsmode
 
 
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()
Dim objDNs()
 
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 & ") **"
 
                                        If vbsmode = 2 Then
                                          
                                            'fetch DN from array of already processed users
                                            for i = 0 to ubound(objreports) step 1
                                            
                                              If objreports(i) = lookupname Then
                                                Set objntlogin = getobject("LDAP://" & objdns(i))
                                                Set objparent = getobject(objntlogin.parent)
                                                objparent.delete "user","cn=" & objntlogin.cn
                                              end if
                                            next
                                          end if
                                End If
                                
                        Next
                        
                        ' Add lookup name to dict. obj
			intArrMax = intArrmax + 1
			redim preserve objreports(intarrmax)
      redim preserve objDns(intArrMax)
			objreports(intarrmax) = lookupname
			objdns(intarrmax) = objreportee.distinguishedName
                        
                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
bsharathAuthor Commented:
Thank you Matthew

I get this



** SCRIPT RUN STARTING @ 11/18/2008 8:20:49 AM **

** ENDED PROCESSING OF RUN **
**********************************
Nothing else gets logged...
0
bsharathAuthor Commented:
Thank you Matthew

I get this



** SCRIPT RUN STARTING @ 11/18/2008 8:20:49 AM **

** ENDED PROCESSING OF RUN **
**********************************
Nothing else gets logged...
0
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

tigermattCommented:

Is that in Logging mode? (I.e. just running the script normally?) If you go back to the old script at the start of the question and run it in the same OU, do you get some output? At least then we'll know exactly where the problem is!
0
bsharathAuthor Commented:
I havent changed any thing in the script should i change?
0
bsharathAuthor Commented:
I havent changed any thing in the script should i change?
0
bsharathAuthor Commented:
Matthew do you mean you want me to run the main code?
Yes when i ran the accepted code it showed that a user has duplicates
0
tigermattCommented:

What I meant was if you try to run the code below, do you get the expected duplicates? The code below is without the delete feature, so I just need to track down if it is the delete feature causing this or not.

Sorry for not replying sooner! Not at my desk for a while today.
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
bsharathAuthor Commented:
Yes Matthew i get 60 + duplicates populated in the txt file.
When i run this script with a 2 i get some removed but not all.

I gave it some time to even see if its replication time. But still some are not removed.

In one user there were 3 duplicated where 2 were removed and 1 still there.
I even ran it 3 times but still all do not get removed...
0
bsharathAuthor Commented:
Yes Matthew i get 60 + duplicates populated in the txt file.
When i run this script with a 2 i get some removed but not all.

I gave it some time to even see if its replication time. But still some are not removed.

In one user there were 3 duplicated where 2 were removed and 1 still there.
I even ran it 3 times but still all do not get removed...
0
bsharathAuthor Commented:
Matthew does this script remove or delete?
0
bsharathAuthor Commented:
Matthew does this script remove or delete?
0
tigermattCommented:

Hi Sharath,

Not the latest script I posted but the one above is the one which deleted. Bear in mind that it will only delete NTLogins if it finds a duplicate - it won't go and delete the contact too. Is this what you want? Only  ntlogin deleted if a duplicate is found?

-Matthew
0
bsharathAuthor Commented:
Sorry matthew for not being clear on giving you info.

Actually i want to remove the user not delete the user

Say i have 2 guys who report to me. Both the NTlogin and contact is added. So i want to remove NTlogin so just the contact is shown.

In some cases we have both shown which is of no use. I just want to remove not delete.

I ended up deleting few logins but no problem i managed creating them back... :-))
0
bsharathAuthor Commented:
Sorry matthew for not being clear on giving you info.

Actually i want to remove the user not delete the user

Say i have 2 guys who report to me. Both the NTlogin and contact is added. So i want to remove NTlogin so just the contact is shown.

In some cases we have both shown which is of no use. I just want to remove not delete.

I ended up deleting few logins but no problem i managed creating them back... :-))
0
tigermattCommented:

I see, so the script was working, just not doing the right thing. I'll have a look as soon as work here goes quieter!
0
bsharathAuthor Commented:
Yes Matthew
I want it to just be removed not deleted....
0
bsharathAuthor Commented:
Yes Matthew
I want it to just be removed not deleted....
0
tigermattCommented:

Hi Sharath,

Try this one. I'm on a secure domain so can't test at the moment, so I'd suggest you create a few duplicate test users and a manager to test it first.

Remember that the script works by checking the Forename and Surname of the Contact and NTLogins, and that is what it uses to find duplicates. So for example the contact must have a forename of Joe and a surname of Bloggs, and the NTLogin must also have a forename of Joe, and a surname of Bloggs, in order for the duplicate to be flagged. It won't flag it on Full Name or Display Name (it can't).

-Matthew
on error resume next
Const ForWriting = 2
Const ForAppending = 8
Const ADS_PROPERTY_CLEAR = 1
 
' Set OU where the *Managers* can be found, below
strOUPath = "ou=myou,dc=dc,dc=co,dc=uk"
 
'Log File path
strLog = "C:\logfile.log"
 
'Logging or deletion mode?
Dim args: Set args = WScript.Arguments
If args.Count > 0 Then
        Dim vbsmode: vbsmode = CInt(args.Item(0))
        
        If vbsmode > 2 OR vbsmode < 1 Then
                WScript.Echo "Invalid parameter for VBS mode entered"
        End If
        
        If vbsmode = 1 Then
                WScript.Echo "Running in logging mode"
        Elseif vbsmode = 2 Then
                WScript.Echo "Running in deletion mode. Are you sure?"
                proceedVal = MsgBox("The script is running in deletion mode. This means it will delete the NtLogins of any users which have a duplicate contact and user account."&vbcrlf&vbcrlf& _
                        "Do you wish to proceed?", 36, "Deletion Mode Warning")
                
                If proceedVal <> 6 Then
                        WScript.Echo "Reverting to logging mode"
                        vbsmode = 1
                End If
                
        End If
Else
        WScript.Echo "Defaulting to logging mode"
         vbsmode = 1
End if
 
WScript.Echo "VBS Mode: " & vbsmode
 
 
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()
Dim objDNs()
 
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 = Trim(objReportee.givenName) & " " & Trim(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 & ") **"
 
                                        If vbsmode = 2 Then
                                          
                                            'fetch DN from array of already processed users
                                            for i = 0 to ubound(objreports) step 1
                                            
                                              If objreports(i) = lookupname Then
                                                Set objntlogin = getobject("LDAP://" & objdns(i))
                                                objntlogin.putex ADS_PROPERTY_CLEAR, "manager", 0
                                                objntlogin.setinfo
                                              end if
                                            next
                                          end if
                                End If
                                
                        Next
                        
                        ' Add lookup name to dict. obj
                        intArrMax = intArrmax + 1
                        redim preserve objreports(intarrmax)
      redim preserve objDns(intArrMax)
                        objreports(intarrmax) = lookupname
                        objdns(intarrmax) = objreportee.distinguishedName
                        
                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
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.