?
Solved

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

Posted on 2008-11-16
20
Medium Priority
?
290 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:bsharath
  • 14
  • 6
20 Comments
 
LVL 58

Expert Comment

by:tigermatt
ID: 22978325

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
 
LVL 11

Author Comment

by:bsharath
ID: 22981693
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
 
LVL 11

Author Comment

by:bsharath
ID: 22981696
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 58

Expert Comment

by:tigermatt
ID: 22982618

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
 
LVL 11

Author Comment

by:bsharath
ID: 22982692
I havent changed any thing in the script should i change?
0
 
LVL 11

Author Comment

by:bsharath
ID: 22982693
I havent changed any thing in the script should i change?
0
 
LVL 11

Author Comment

by:bsharath
ID: 22985106
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
 
LVL 58

Expert Comment

by:tigermatt
ID: 22987256

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
 
LVL 11

Author Comment

by:bsharath
ID: 22991625
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
 
LVL 11

Author Comment

by:bsharath
ID: 22991626
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
 
LVL 11

Author Comment

by:bsharath
ID: 22991978
Matthew does this script remove or delete?
0
 
LVL 11

Author Comment

by:bsharath
ID: 22991979
Matthew does this script remove or delete?
0
 
LVL 58

Expert Comment

by:tigermatt
ID: 22992236

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
 
LVL 11

Author Comment

by:bsharath
ID: 22992260
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
 
LVL 11

Author Comment

by:bsharath
ID: 22992262
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
 
LVL 58

Expert Comment

by:tigermatt
ID: 22998205

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
 
LVL 11

Author Comment

by:bsharath
ID: 23000905
Yes Matthew
I want it to just be removed not deleted....
0
 
LVL 11

Author Comment

by:bsharath
ID: 23000906
Yes Matthew
I want it to just be removed not deleted....
0
 
LVL 58

Accepted Solution

by:
tigermatt earned 2000 total points
ID: 23007016

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
 
LVL 11

Author Comment

by:bsharath
ID: 23087847
Thank U
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

862 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