Link to home
Start Free TrialLog in
Avatar of reason4xistence
reason4xistenceFlag for United States of America

asked on

How do I create a VBscript that will list the groups in the Member Of tab for each AD User listed in a text file?

I am trying to combine two VBscripts into one. The first script will pull information from a text file and output information into a csv file. The second script will pull up an account in Active Directory and display the list of groups in the Member Of tab. The final result of the combined scripts should be that the script will read a list of employee numbers from a text file, pull up the AD account for each employee number, create a list of the groups in the Member Of tab for each employee number, and display and output the list of groups into a csv file.
Both of the original scripts work properly and they both have function statements. And the VBscript "rules" will not allow me to insert a function statement inside of another function statement. So my problem is that I don't know how to combine the two function statements.
With the combined script that I created, I have been able to pull the employee numbers from the text file and display them as headers on the csv file. But I have not been able to list the groups in the Member Of tab.
I have attached a sample of my text file with the list of employee numbers (test.txt). I have also attached the csv file that is created by the script (test.xls). And I have attached the combined script that I created.
'=========================================================================
' VBScript Source File -- Created with XLnow OnScript
'
' AUTHOR:  Joel Pace II 
' COMPANY: First Tennessee Bank N A
' DATE:    10/21/2008
' COMMENT: Pull from a .txt file a list of employee numbers and then list
'          the Member Of tab information for each employee. Save information
'          to a .csv file.
'=========================================================================
 
'OPTION EXPLICIT
' *************************************************
' * Constants / Decleration
' *************************************************
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
Const ADS_PROPERTY_CLEAR = 1
 
' Declare variables.
'Dim fileDate
'Dim objFSO, objFSO2, objOutfile
'Dim objFile
 
'Dim strDate
'Dim strLine
'Dim strCharacter, strFile
 
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\myscripts\script io\tests\test.txt", 1)
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objOutFile = objFSO.OpenTextFile("c:\myscripts\script io\tests\test.csv", 8, True)
 
Do Until objFile.AtEndOfStream
 
strCharacter = objFile.ReadLine
'wscript.echo strCharacter
Call getUser(strCharacter)
 
Loop
 
'Destroy objects.
objFile.Close
objOutFile.Close
' *************************************************
' * Functions
' *************************************************
Function getUser(Byval strEid)
            Const adOpenStatic = 3
            Const adLockOptimistic = 3
            Const adCmdText = &H0001
            Const ADS_PROPERTY_CLEAR = 1
            
            'DIM objRoot
            'DIM getUserCn,getUserCmd,getUserRS
            'DIM strText, strLine, strName, userPath, objUser, strRootName, strUser, strFName, strLName
			'DIM strFullName, strDName, strADisabled, strLogon
			'DIM strSpaces, strObjectADSPath, strGroupDN
			'DIM DisplayGroups, dicSeenGroup, objObject, colGroups
 
            on error resume next
            set objRoot = getobject("LDAP://fhhlc")
            set getUserCn = createobject("ADODB.Connection")
            set getUserCmd = createobject("ADODB.Command")
            set getUserRS = createobject("ADODB.Recordset")
 
            getUserCn.open "Provider=ADsDSOObject;"
            
            getUserCmd.activeconnection=getUserCn
            getUserCmd.commandtext="<LDAP://dc=fhhlc,dc=ftn,dc=com>;" & _
                                    "(&(objectCategory=user)(samAccountName=" & strEid & "));" & _
                                    "adsPath;subtree"
            set getUserRs = getUserCmd.execute
            strRootName = Right(Left((objRoot.Get("defaultNamingContext")),8),5)
 
            if getUserRS.recordcount = 0 then
                        strLine = Chr(34) & "The account for " & strEID & " was not found in the " & strRootName & " domain." & Chr(34) & "," & Chr(34) & "" & Chr(34) 
                        objOutFile.WriteLine strLine      
                        wscript.echo strRootname & "\" & streID & " was not found."                      
                        
            
            elseif getUserRS.recordcount = 1 then
                         userPath = getUserRs(0)
                        set objUser = getobject(userPath)
						Wscript.Echo "Group membership for " & objUser.Get("cn") & " (" & strEID & ")" & ":"
						strSpaces = ""
                        set dicSeenGroup = CreateObject("Scripting.Dictionary")
                        DisplayGroups "LDAP://" & userPath, strSpaces, dicSeenGroup
						
						'Function DisplayGroups ( strObjectADsPath, strSpaces, dicSeenGroup)
						
						set objObject = GetObject(strObjectADsPath)
                            WScript.Echo strSpaces & objObject.Name
                            on error resume next ' Doing this to avoid an error when memberOf is empty
                                    if IsArray( objObject.Get("memberOf") ) then
                                                colGroups = objObject.Get("memberOf")
                                    else
                                                colGroups = Array( objObject.Get("memberOf") )
                                    end if
   
                                    for each strGroupDN In colGroups
                                    if Not dicSeenGroup.Exists(strGroupDN) then
                                               dicSeenGroup.Add strGroupDN, 1
                                               DisplayGroups "LDAP://" & strGroupDN, strSpaces & " ", dicSeenGroup
				                    end if
                             next
						'end function
						
						strLine = "Group membership for: " & strEid
                        objOutFile.WriteLine strLine
						for each objObject in dicSeenGroup
							objOutFile.Write objObject.Name
							objOutFile.WriteLine
						Next
						WScript.Echo strSpaces & objObject.Name
            end if
            getUserCn.close
 
 
end function
Function DisplayGroups (strObjectADsPath, strSpaces, dicSeenGroup)
end function

Open in new window

test.txt
test.xls
MemberofbyEID.txt
Avatar of Procastin8or
Procastin8or
Flag of United States of America image

Can you post the two scripts separately that you are trying to combine?
Avatar of reason4xistence

ASKER

Attached are the two scripts that I am trying to combine (search.vbs & MemberOfUser.vbs). Thanks for looking into this.
MemberOfUser.txt
search.txt
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia 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
This is exactly what I am looking for. This worked perfectly on my first run. I was working so hard on this, but I am new to scripting and teaching myself. I really don't think I would have been able to accomplish what you just did on my own. I am so very appreciative! I graded this as an A, and answered "Yes" to all three questions about your solution being complete, accurate, and easy to understand. Thanks!
Thank you so much! This is exactly what I was trying to do.
No problem. Thanks for the grade.  It's takes a while to get used to combining things like this, but once you can do it, you can make some pretty good scripts.

Regards,

Rob.
Welcome to EE by the way.  I hope you find this a useful resource for yourself.

Regards,

Rob.