Solved

Comma delimitted, VBS Script needed

Posted on 2013-01-28
10
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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