Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Comma delimitted, VBS Script needed

Posted on 2013-01-28
10
Medium Priority
?
337 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 1600 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

618 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