Solved

Comma delimitted, VBS Script needed

Posted on 2013-01-28
10
319 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

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

Technology Partners: 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!

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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