Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - How to a CSV file in a VBS Script

Reply
 
Old 08-11-2008   #1 (permalink)
rademr1


 
 

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
Old 08-11-2008   #2 (permalink)
Tom Lavedas


 
 

Re: How to a CSV file in a VBS Script

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

> 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
Old 08-11-2008   #3 (permalink)
rademr1


 
 

Re: How to a CSV file in a VBS Script



"Tom Lavedas" wrote:
Quote:

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

> > 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
Old 08-11-2008   #4 (permalink)
Tom Lavedas


 
 

Re: How to a CSV file in a VBS Script

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

> "Tom Lavedas" wrote:
Quote:

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

> > > 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.
>
Quote:
Quote:

> > > On Error Resume Next
>
Quote:
Quote:

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

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

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

> > Maybe ...
>
Quote:

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

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

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

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

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

> > oCSV.close
>
Quote:

> > 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
Old 08-11-2008   #5 (permalink)
Wiseman82


 
 

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

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

>> "Tom Lavedas" wrote:
Quote:

>> > 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.
>>
Quote:

>> > > On Error Resume Next
>>
Quote:

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

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

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

>> > Maybe ...
>>
Quote:

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

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

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

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

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

>> > oCSV.close
>>
Quote:

>> > 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
Old 08-11-2008   #6 (permalink)
Richard Mueller [MVP]


 
 

Re: How to a CSV file in a VBS Script


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

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

Thread Tools


Similar Threads
Thread Forum
problem passing args to script 'There is no script engine for file extenstion' VB Script
VBS SCRIPT to clean OLD FILE VB Script
file copy operations using source file input and script? PowerShell
Script file has 'OS Handle' error when run from script PowerShell
Can you drag-n-drop a file on top of a PS script to run the script? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46