Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

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

Avatar of tigermatt
tigermatt
Flag of United Kingdom of Great Britain and Northern Ireland image


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

Avatar of bsharath

ASKER

Thank you Matthew

I get this



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

** ENDED PROCESSING OF RUN **
**********************************
Nothing else gets logged...
Thank you Matthew

I get this



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

** ENDED PROCESSING OF RUN **
**********************************
Nothing else gets logged...

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!
I havent changed any thing in the script should i change?
I havent changed any thing in the script should i change?
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

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

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...
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...
Matthew does this script remove or delete?
Matthew does this script remove or delete?

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
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... :-))
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... :-))

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!
Yes Matthew
I want it to just be removed not deleted....
Yes Matthew
I want it to just be removed not deleted....
ASKER CERTIFIED SOLUTION
Avatar of tigermatt
tigermatt
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank U