Windows Vista Forums

How to a CSV file in a VBS Script
  1. #1


    rademr1 Guest

    How to a CSV file in a VBS Script

    I am looking for a little help on how to take the script below and put the
    results in a CSV file. I got this script from the Microsoft Scriptcenter and
    it displays all the members of an Active Directory Group.

    On Error Resume Next

    Set objGroup = GetObject _
    ("LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com")
    objGroup.GetInfo



    arrMemberOf = objGroup.GetEx("member")

    WScript.Echo "Members:"
    For Each strMember in arrMemberOf
    WScript.echo strMember
    Next



      My System SpecsSystem Spec

  2. #2


    Tom Lavedas Guest

    Re: How to a CSV file in a VBS Script

    On Aug 11, 2:48 pm, rademr1 <rade...@xxxxxx> wrote:

    > I am looking for a little help on how to take the script below and put the
    > results in a CSV file. I got this script from the Microsoft Scriptcenter and
    > it displays all the members of an Active Directory Group.
    >
    > On Error Resume Next
    >
    > Set objGroup = GetObject _
    > ("LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com")
    > objGroup.GetInfo
    >
    > arrMemberOf = objGroup.GetEx("member")
    >
    > WScript.Echo "Members:"
    > For Each strMember in arrMemberOf
    > WScript.echo strMember
    > Next
    Maybe ...

    sFilespec = "D:\Someplace\SomefileName.csv"

    Set fso = CreateObject("Scripting.FileSystemObject")
    set oCSV = fso.opentextfile(sFilespec, 1)

    Set objGroup = GetObject _
    ("LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com")
    objGroup.GetInfo

    arrMemberOf = objGroup.GetEx("member")

    oCSV.Writeline "Members"
    For Each strMember in arrMemberOf
    oCSV.Writeline strMember
    Next

    oCSV.close

    Tom Lavedas
    ===========
    http://members.cox.net/tglbatch/wsh/

      My System SpecsSystem Spec

  3. #3


    rademr1 Guest

    Re: How to a CSV file in a VBS Script



    "Tom Lavedas" wrote:

    > On Aug 11, 2:48 pm, rademr1 <rade...@xxxxxx> wrote:

    > > I am looking for a little help on how to take the script below and put the
    > > results in a CSV file. I got this script from the Microsoft Scriptcenter and
    > > it displays all the members of an Active Directory Group.
    > >
    > > On Error Resume Next
    > >
    > > Set objGroup = GetObject _
    > > ("LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com")
    > > objGroup.GetInfo
    > >
    > > arrMemberOf = objGroup.GetEx("member")
    > >
    > > WScript.Echo "Members:"
    > > For Each strMember in arrMemberOf
    > > WScript.echo strMember
    > > Next
    >
    > Maybe ...
    >
    > sFilespec = "D:\Someplace\SomefileName.csv"
    >
    > Set fso = CreateObject("Scripting.FileSystemObject")
    > set oCSV = fso.opentextfile(sFilespec, 1)
    >
    > Set objGroup = GetObject _
    > ("LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com")
    > objGroup.GetInfo
    >
    > arrMemberOf = objGroup.GetEx("member")
    >
    > oCSV.Writeline "Members"
    > For Each strMember in arrMemberOf
    > oCSV.Writeline strMember
    > Next
    >
    > oCSV.close
    >
    > Tom Lavedas
    > ===========
    > http://members.cox.net/tglbatch/wsh/
    >
    I changed where the file is suppose to save and also changed the LDAP
    settings and I got an error when I ran the script.

    Script: C:\members1.vbs
    Line: 4
    Char: 1
    Error: File not found
    Code: 800A00035
    Source: Microsoft VBScript runtime error

    Thanks for your help.




      My System SpecsSystem Spec

  4. #4


    Tom Lavedas Guest

    Re: How to a CSV file in a VBS Script

    On Aug 11, 3:42 pm, rademr1 <rade...@xxxxxx> wrote:

    > "Tom Lavedas" wrote:

    > > On Aug 11, 2:48 pm, rademr1 <rade...@xxxxxx> wrote:

    > > > I am looking for a little help on how to take the script below and put the
    > > > results in a CSV file. I got this script from the Microsoft Scriptcenter and
    > > > it displays all the members of an Active Directory Group.
    >

    > > > On Error Resume Next
    >

    > > > Set objGroup = GetObject _
    > > > ("LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com")
    > > > objGroup.GetInfo
    >

    > > > arrMemberOf = objGroup.GetEx("member")
    >

    > > > WScript.Echo "Members:"
    > > > For Each strMember in arrMemberOf
    > > > WScript.echo strMember
    > > > Next
    >

    > > Maybe ...
    >

    > > sFilespec = "D:\Someplace\SomefileName.csv"
    >

    > > Set fso = CreateObject("Scripting.FileSystemObject")
    > > set oCSV = fso.opentextfile(sFilespec, 1)
    >

    > > Set objGroup = GetObject _
    > > ("LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com")
    > > objGroup.GetInfo
    >

    > > arrMemberOf = objGroup.GetEx("member")
    >

    > > oCSV.Writeline "Members"
    > > For Each strMember in arrMemberOf
    > > oCSV.Writeline strMember
    > > Next
    >

    > > oCSV.close
    >

    > > Tom Lavedas
    > > ===========
    > >http://members.cox.net/tglbatch/wsh/
    >
    > I changed where the file is suppose to save and also changed the LDAP
    > settings and I got an error when I ran the script.
    >
    > Script: C:\members1.vbs
    > Line: 4
    > Char: 1
    > Error: File not found
    > Code: 800A00035
    > Source: Microsoft VBScript runtime error
    >
    > Thanks for your help.
    Sorry, my fault. This line ...

    set oCSV = fso.opentextfile(sFilespec, 1)

    should read ...

    set oCSV = fso.opentextfile(sFilespec, 2)

    The constant is ForReading = 1, ForWriting = 2 and ForAppending = 8.
    I put in the wrong constant.

    Tom Lavedas
    ===========
    http://members.cox.net/tglbatch/wsh/

      My System SpecsSystem Spec

  5. #5


    Wiseman82 Guest

    Re: How to a CSV file in a VBS Script

    You might also find this script useful for listing all groups/members:

    http://www.wisesoft.co.uk/scripts/vb...rs_report.aspx

    Hope this helps,

    David


    "Tom Lavedas" <tglbatch@xxxxxx> wrote in message
    news:c3addbfc-fd22-4766-9a2b-8ace1e28fdd8@xxxxxx

    > On Aug 11, 3:42 pm, rademr1 <rade...@xxxxxx> wrote:

    >> "Tom Lavedas" wrote:

    >> > On Aug 11, 2:48 pm, rademr1 <rade...@xxxxxx> wrote:
    >> > > I am looking for a little help on how to take the script below and
    >> > > put the
    >> > > results in a CSV file. I got this script from the Microsoft
    >> > > Scriptcenter and
    >> > > it displays all the members of an Active Directory Group.
    >>

    >> > > On Error Resume Next
    >>

    >> > > Set objGroup = GetObject _
    >> > > ("LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com")
    >> > > objGroup.GetInfo
    >>

    >> > > arrMemberOf = objGroup.GetEx("member")
    >>

    >> > > WScript.Echo "Members:"
    >> > > For Each strMember in arrMemberOf
    >> > > WScript.echo strMember
    >> > > Next
    >>

    >> > Maybe ...
    >>

    >> > sFilespec = "D:\Someplace\SomefileName.csv"
    >>

    >> > Set fso = CreateObject("Scripting.FileSystemObject")
    >> > set oCSV = fso.opentextfile(sFilespec, 1)
    >>

    >> > Set objGroup = GetObject _
    >> > ("LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com")
    >> > objGroup.GetInfo
    >>

    >> > arrMemberOf = objGroup.GetEx("member")
    >>

    >> > oCSV.Writeline "Members"
    >> > For Each strMember in arrMemberOf
    >> > oCSV.Writeline strMember
    >> > Next
    >>

    >> > oCSV.close
    >>

    >> > Tom Lavedas
    >> > ===========
    >> >http://members.cox.net/tglbatch/wsh/
    >>
    >> I changed where the file is suppose to save and also changed the LDAP
    >> settings and I got an error when I ran the script.
    >>
    >> Script: C:\members1.vbs
    >> Line: 4
    >> Char: 1
    >> Error: File not found
    >> Code: 800A00035
    >> Source: Microsoft VBScript runtime error
    >>
    >> Thanks for your help.
    >
    > Sorry, my fault. This line ...
    >
    > set oCSV = fso.opentextfile(sFilespec, 1)
    >
    > should read ...
    >
    > set oCSV = fso.opentextfile(sFilespec, 2)
    >
    > The constant is ForReading = 1, ForWriting = 2 and ForAppending = 8.
    > I put in the wrong constant.
    >
    > Tom Lavedas
    > ===========
    > http://members.cox.net/tglbatch/wsh/

      My System SpecsSystem Spec

  6. #6


    Richard Mueller [MVP] Guest

    Re: How to a CSV file in a VBS Script


    "rademr1" <rademr1@xxxxxx> wrote in message
    news:45CAECA0-64B8-415B-8C94-428C68F60150@xxxxxx

    >I am looking for a little help on how to take the script below and put the
    > results in a CSV file. I got this script from the Microsoft Scriptcenter
    > and
    > it displays all the members of an Active Directory Group.
    >
    > On Error Resume Next
    >
    > Set objGroup = GetObject _
    > ("LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com")
    > objGroup.GetInfo
    >
    > arrMemberOf = objGroup.GetEx("member")
    >
    > WScript.Echo "Members:"
    > For Each strMember in arrMemberOf
    > WScript.echo strMember
    > Next
    >
    >
    First, since your script uses Wscript.Echo, you can also redirect the output
    to a text file. You would run the script at a command prompt using cscript.
    For example:

    cscript //nologo Example.vbs > report.txt

    Next, the GetEx method in your script will raise an error if the group has
    no members. That's why it has "On Error Resume Next". I would trap the error
    and echo this condition (no members) with code similar to:
    ==========
    Set objGroup = GetObject _
    ("LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com")

    On Error Resume Next
    arrMemberOf = objGroup.GetEx("member")
    If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "No members"
    Else
    On Error GoTo 0
    WScript.Echo "Members:"
    For Each strMember in arrMemberOf
    WScript.echo strMember
    Next
    End If
    ===========
    Finally, it might make sense to use the Members method of the group object,
    which never raises an error. This method returns a collection of member
    objects (not just DN's), so you have access to all attributes of the
    members:
    ==========
    Set objGroup = GetObject _
    ("LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com")

    For Each objMember In objGroup.Members
    Wscript.Echo objMember.distinguishedName
    Next
    ==========
    If instead you wanted to document the "pre-Windows 2000 logon" name, you
    could use objMember.sAMAccountName.

    --
    Richard Mueller
    MVP Directory Services
    Hilltop Lab - http://www.rlmueller.net
    --



      My System SpecsSystem Spec

How to a CSV file in a VBS Script problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem passing args to script 'There is no script engine for file extenstion' James VB Script 5 31 Oct 2008
file copy operations using source file input and script? Ryan Rupert PowerShell 4 08 Aug 2008
Help with a Batch file script (FTP) MariusK Vista General 2 31 Jul 2008
Script file has 'OS Handle' error when run from script Jay PowerShell 1 18 Sep 2007
Can you drag-n-drop a file on top of a PS script to run the script? Jen Taylor PowerShell 6 06 Apr 2007