Solved

Comma delimitted, VBS Script needed

Posted on 2013-01-28
10
310 Views
Last Modified: 2013-01-28
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

0
Comment
Question by:mystikal1000
  • 4
  • 3
  • 3
10 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38826642
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
0
 
LVL 1

Expert Comment

by:DoveSupport
ID: 38826709
Hi, try this out :-

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

TestUserExport.vbs
0
 
LVL 1

Accepted Solution

by:
DoveSupport earned 400 total points
ID: 38826714
Pasted here just in case :-


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.Write objUser.Name
      For Each strGroup In objUser.memberOf
     If mid(strGroup,4,4)="test_" Then  
      Set objGroup = GetObject("LDAP://" & strGroup)
      objOutput.Write "," & 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

0
 
LVL 1

Author Comment

by:mystikal1000
ID: 38827173
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.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38827201
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:mystikal1000
ID: 38827205
Actually dragon, yours is duplicating...

Dove support it works!!!!
0
 
LVL 1

Expert Comment

by:DoveSupport
ID: 38827215
Excellent !
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 100 total points
ID: 38827245
Again I haven't tested as not got a domain controller booted at the moment but have added check to show if no groups too.

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)

     Groups=""
      For Each strGroup In objUser.memberOf

        If lcase(left(strGroup,5)="test_" Then  
          Set objGroup = GetObject("LDAP://" & strGroup)
          Groups=Groups & chr(34) & objGroup.CN & chr(34) & ","
        End If
	If Groups<>"" then
	  objOutput.WriteLine strUser & "," & chr(34) & objUser.Name & chr(34) & "," & Groups
        Else
          objOutput.WriteLine strUser & "," & chr(34) & objUser.Name & chr(34) & "," & chr(34) & "* no test_ groups *" & chr(34)
        End If
      Next

      objOutput.WriteLine
      objRecordSet.MoveNext
    Loop
  Else 
     objOutput.WriteLine chr(34) & strUser & "," & chr(34) & "* no groups*"
  End If

Next

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

Open in new window

0
 
LVL 1

Author Closing Comment

by:mystikal1000
ID: 38827253
Thanks everyone for the help!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38827270
Glad you got there, sorry being hassled here so not too much concentration on that one :-)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now