zanderkid
asked on
vbs script to read list of users and export a file with the groups they belong to
I would like to take a txt file that contains the list of multiple user accounts. Then have it look at the domain and tell me what groups the user is in and export that info to a txt or csv file. Below is an example but only allows you to enter one username and then gives you a text file with the groups that one user belongs to. I need to remove that and allow it to read mulitple users.
On Error Resume Next
Const ForWriting = 2
UserName = InputBox("Enter Username","Enter Username")
Set objUser = GetObject("LDAP://" & SearchDistinguishedName(Us erName))
For Each strGroup in objUser.memberOf
Set objGroup = GetObject("LDAP://" & strGroup)
Report = Report & objGroup.CN & vbCrLf
Next
Set fso = CreateObject("Scripting.Fi leSystemOb ject")
Set ts = fso.CreateTextFile (UserName & "GroupMembership.txt", ForWriting)
ts.Write Report
WScript.Echo "Done"
Public Function SearchDistinguishedName(By Val vSAN)
' Function: SearchDistinguishedName
' Description: Searches the DistinguishedName for a given SamAccountName
' Parameters: ByVal vSAN - The SamAccountName to search
' Returns: The DistinguishedName Name
Dim oRootDSE, oConnection, oCommand, oRecordSet
Set oRootDSE = GetObject("LDAP://rootDSE" )
Set oConnection = CreateObject("ADODB.Connec tion")
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Comman d")
oCommand.ActiveConnection = oConnection
oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamin gContext") & _
">;(&(objectCategory=User) (samAccoun tName=" & vSAN & "));distinguishedName;subt ree"
Set oRecordSet = oCommand.Execute
On Error Resume Next
SearchDistinguishedName = oRecordSet.Fields("Disting uishedName ")
On Error GoTo 0
oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
Set oRootDSE = Nothing
End Function
On Error Resume Next
Const ForWriting = 2
UserName = InputBox("Enter Username","Enter Username")
Set objUser = GetObject("LDAP://" & SearchDistinguishedName(Us
For Each strGroup in objUser.memberOf
Set objGroup = GetObject("LDAP://" & strGroup)
Report = Report & objGroup.CN & vbCrLf
Next
Set fso = CreateObject("Scripting.Fi
Set ts = fso.CreateTextFile (UserName & "GroupMembership.txt", ForWriting)
ts.Write Report
WScript.Echo "Done"
Public Function SearchDistinguishedName(By
' Function: SearchDistinguishedName
' Description: Searches the DistinguishedName for a given SamAccountName
' Parameters: ByVal vSAN - The SamAccountName to search
' Returns: The DistinguishedName Name
Dim oRootDSE, oConnection, oCommand, oRecordSet
Set oRootDSE = GetObject("LDAP://rootDSE"
Set oConnection = CreateObject("ADODB.Connec
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Comman
oCommand.ActiveConnection = oConnection
oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamin
">;(&(objectCategory=User)
Set oRecordSet = oCommand.Execute
On Error Resume Next
SearchDistinguishedName = oRecordSet.Fields("Disting
On Error GoTo 0
oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
Set oRootDSE = Nothing
End Function
ASKER
I think this is searching all the users in th entire domain. I'm looking for the script to read a list of specific users. I ran this script and got the following:
The size limit for this request was exceeded.
The size limit for this request was exceeded.
Oops. Sorry...I missed the input file part. Try this.
Regards,
Rob.
Regards,
Rob.
On Error Resume Next
Const ForReading = 1
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set objInput = fso.OpenTextFile("usernames.txt", ForReading, False)
Set objOutput = fso.CreateTextFile("GroupMemberships.txt", True)
While objInput.AtEndOfStream
UserName = Trim(objInput.ReadLine)
If UserName <> "" Then
Set objUser = GetObject("LDAP://" & SearchDistinguishedName(UserName))
strLine = objUser.samAccountName
If TypeName(objUser.MemberOf) = "Empty" Then
strLine = strLine & ",<<NONE>>"
ElseIf TypeName(objUser.MemberOf) = "String" Then
strLine = strLine & "," & objUser.MemberOf
Else
For Each strGroup in objUser.memberOf
strLine = strLine & "," & strGroup
Next
End If
objOutput.WriteLine strLine
End If
Wend
objInput.Close
objOutput.Close
WScript.Echo "Done"
Public Function SearchDistinguishedName(ByVal vSAN)
' Function: SearchDistinguishedName
' Description: Searches the DistinguishedName for a given SamAccountName
' Parameters: ByVal vSAN - The SamAccountName to search
' Returns: The DistinguishedName Name
Dim oRootDSE, oConnection, oCommand, oRecordSet
Set oRootDSE = GetObject("LDAP://rootDSE")
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Command")
oCommand.ActiveConnection = oConnection
oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
">;(&(objectCategory=User)(samAccountName=" & vSAN & "));distinguishedName;subtree"
Set oRecordSet = oCommand.Execute
On Error Resume Next
SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")
On Error GoTo 0
oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
Set oRootDSE = Nothing
End Function
ASKER
The Last code posted does complete without error however it does not have any data.
Can you comment out the On Error Resume Next line and run it again? Maybe you're getting an error you can't see.
Regards,
Rob.
Regards,
Rob.
ASKER
I comment ou the On Error Resume next and the script still completed without error but no data in the output file
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Regards,
Rob,
Open in new window