Link to home
Start Free TrialLog in
Avatar of mystikal1000
mystikal1000

asked on

Comma delimitted, VBS Script needed

I have a VB Script that will gather all accounts inside users.txt and search for "test" string for AD group prefix and output all groups that start with test.  However the output file has carriage returns on each group.  I would like the userid and group on one line seperated by a comma, to help manage using the output file via Excel.  I appreciate the help if someone can tweak the code.


Const ADS_SCOPE_SUBTREE = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
strDomain = "DC=Domain,DC=Controller,DC=com"
strUsers = "c:\Users.txt"

Set objOutput = objFSO.CreateTextFile("c:\Output.txt")

aryUsers = Split(objFSO.OpenTextFile(strUsers).ReadAll, vbNewLine)
For Each strUser In aryUsers
  Set objConnection = CreateObject("ADODB.Connection")
  Set objCommand = CreateObject("ADODB.Command")
  objConnection.Provider = "ADsDSOObject"
  objConnection.Open "Active Directory Provider"
  Set objCommand.ActiveConnection = objConnection
  objCommand.Properties("Page Size") = 1000
  objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
  objCommand.CommandText = _
    "SELECT ADsPath FROM 'LDAP://" & strDomain & "' WHERE objectCategory='user' " & _
        "AND samAccountName = '" & strUser & "'"
  
  Set objRecordSet = objCommand.Execute
  objRecordSet.MoveFirst
  If Not objRecordSet.EOF Then 
    Do Until objRecordSet.EOF
      strADsPath = objRecordSet.Fields("ADsPath").Value
      Set objUser = GetObject(strADsPath)
      objOutput.WriteLine objUser.Name & " is a member of the following groups:"
      For Each strGroup In objUser.memberOf
     If mid(strGroup,4,4)="test_" Then  
      Set objGroup = GetObject("LDAP://" & strGroup)
      objOutput.WriteLine objGroup.CN
    End If
 Next


      objOutput.WriteLine
      objRecordSet.MoveNext
    Loop
  Else 
     objOutput.WriteLine strUser & " does not have an AD account!"
  End If

Next

objOutput.Close
Set objOutput = Nothing
Set objGroup = Nothing
Set objUser = Nothing
Set objFSO = Nothing

Open in new window

Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Can't test at the mo. but try this.... keeps list of groups in the "Groups" variable then outputs it once.

Const ADS_SCOPE_SUBTREE = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
strDomain = "DC=Domain,DC=Controller,DC=com"
strUsers = "c:\Users.txt"

Set objOutput = objFSO.CreateTextFile("c:\Output.txt")

aryUsers = Split(objFSO.OpenTextFile(strUsers).ReadAll, vbNewLine)
For Each strUser In aryUsers
  Set objConnection = CreateObject("ADODB.Connection")
  Set objCommand = CreateObject("ADODB.Command")
  objConnection.Provider = "ADsDSOObject"
  objConnection.Open "Active Directory Provider"
  Set objCommand.ActiveConnection = objConnection
  objCommand.Properties("Page Size") = 1000
  objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
  objCommand.CommandText = _
    "SELECT ADsPath FROM 'LDAP://" & strDomain & "' WHERE objectCategory='user' " & _
        "AND samAccountName = '" & strUser & "'"
  
  Set objRecordSet = objCommand.Execute
  objRecordSet.MoveFirst
  If Not objRecordSet.EOF Then 
    Do Until objRecordSet.EOF
      strADsPath = objRecordSet.Fields("ADsPath").Value
      Set objUser = GetObject(strADsPath)

'     objOutput.WriteLine objUser.Name & " is a member of the following groups:"
 
      Groups=""      
      For Each strGroup In objUser.memberOf
 
     If mid(strGroup,4,4)="test_" Then  
      Set objGroup = GetObject("LDAP://" & strGroup)
      Groups=Groups & chr(34) & objGroup.CN & chr(34) & ","
     End If

    objOutput.WriteLine chr(34) & objUser.Name & chr(34) & "," & Groups
 Next


      objOutput.WriteLine
      objRecordSet.MoveNext
    Loop
  Else 
     objOutput.WriteLine strUser & "*none*"
  End If

Next

objOutput.Close
Set objOutput = Nothing
Set objGroup = Nothing
Set objUser = Nothing
Set objFSO = Nothing
                                  

Open in new window


You could probably put the code in an Excel macro too if you wish to save making a CSV file then opening excel etc.

Steve
Avatar of DoveSupport
DoveSupport

Hi, try this out :-

Basically, use "Write" instead of "WriteLine" while you want text on the same line.

TestUserExport.vbs
ASKER CERTIFIED SOLUTION
Avatar of DoveSupport
DoveSupport

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
Avatar of mystikal1000

ASKER

Steve - it didn't work.

I received the output...
cn=userid for about 30+ lines.




Dove support - I only get the output cn=userid just one line.
Hmm, well this won't help:

If mid(strGroup,4,4)="test_" Then  

If the 4th character onwards for 4 characters is test_ then.... never do anything, it is 5 characters :-)

So that is checking for:

XXXXtest_XXXXX

Is that what you wanted?

If you want it to START test then use:

if lcase(left(strGroup,5)) = "test_" then

and it will check left hand 5 chars in lower case.

Steve
Actually dragon, yours is duplicating...

Dove support it works!!!!
Excellent !
SOLUTION
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
Thanks everyone for the help!
Glad you got there, sorry being hassled here so not too much concentration on that one :-)