Discussion:
Issue with Getting Output Data to txt or csv file
(too old to reply)
jamiejambo
2009-09-09 14:11:58 UTC
Permalink
Hi Guys
Looking for some help. I have the script below and I am trying to
output the data to a txt or csv file but I do not know how to do it.
Please could someone help.

Option Explicit

Dim strFilePath, objFSO, objFile, objRootDSE
Dim strDNSDomain, objTrans, strNetBIOSDomain
Dim strNTName, strUserDN, objUser

Const ForReading = 1
' Constants for NameTranslate.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

' Specify the text file of user names.
strFilePath = "C:\Documents and Settings\joh149\bulk.txt"

' Open the file for read access.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFilePath, ForReading)

' Determine DNS domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use the NameTranslate object to find the NetBIOS domain name
' from the DNS domain name.
Set objTrans = CreateObject("NameTranslate")
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
' Remove trailing backslash.
strNetBIOSDomain = Left(strNetBIOSDomain, _
Len(strNetBIOSDomain) - 1)

' Read NT names from text file.
Do Until objFile.AtEndOfStream
strNTName = Trim(objFile.ReadLine)
' Skip blank lines.
If (strNTName <> "") Then
' Use Set method to specify NT name.
' Trap error if name not found.
On Error Resume Next
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strNTName
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "User " & strNTName _
& " not found in Active Directory"
End If
On Error GoTo 0
' Use Get method to retrieve Distinguished Name.
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
' Bind to user object in AD.
Set objUser = GetObject("LDAP://" & strUserDN)
' Output values desired. This outputs:
' NT Name;Common Name;first name;last name;street;city;
wscript.Echo objUser.sAMAccountName _
& ";" & objUser.sn _
& ";" & objUser.givenName _
& ";" & objUser.l _
& ";" & objUser.c _
& ";" & objUser.title _
& ";" & objUser.company _
& ";" & objUser.department _
& ";" & objUser.physicaldeliveryofficename
End If
Loop
objFile.Close
--
jamiejambo
------------------------------------------------------------------------
jamiejambo's Profile: http://forums.techarena.in/members/133698.htm
View this thread: http://forums.techarena.in/server-scripting/1244187.htm

http://forums.techarena.in
Pegasus [MVP]
2009-09-09 14:28:41 UTC
Permalink
Post by jamiejambo
Hi Guys
Looking for some help. I have the script below and I am trying to
output the data to a txt or csv file but I do not know how to do it.
Please could someone help.
Option Explicit
Dim strFilePath, objFSO, objFile, objRootDSE
Dim strDNSDomain, objTrans, strNetBIOSDomain
Dim strNTName, strUserDN, objUser
Const ForReading = 1
' Constants for NameTranslate.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
' Specify the text file of user names.
strFilePath = "C:\Documents and Settings\joh149\bulk.txt"
' Open the file for read access.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFilePath, ForReading)
' Determine DNS domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
' Use the NameTranslate object to find the NetBIOS domain name
' from the DNS domain name.
Set objTrans = CreateObject("NameTranslate")
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
' Remove trailing backslash.
strNetBIOSDomain = Left(strNetBIOSDomain, _
Len(strNetBIOSDomain) - 1)
' Read NT names from text file.
Do Until objFile.AtEndOfStream
strNTName = Trim(objFile.ReadLine)
' Skip blank lines.
If (strNTName <> "") Then
' Use Set method to specify NT name.
' Trap error if name not found.
On Error Resume Next
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strNTName
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "User " & strNTName _
& " not found in Active Directory"
End If
On Error GoTo 0
' Use Get method to retrieve Distinguished Name.
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
' Bind to user object in AD.
Set objUser = GetObject("LDAP://" & strUserDN)
' NT Name;Common Name;first name;last name;street;city;
wscript.Echo objUser.sAMAccountName _
& ";" & objUser.sn _
& ";" & objUser.givenName _
& ";" & objUser.l _
& ";" & objUser.c _
& ";" & objUser.title _
& ";" & objUser.company _
& ";" & objUser.department _
& ";" & objUser.physicaldeliveryofficename
End If
Loop
objFile.Close
--
jamiejambo
Since you already generate an output with the wscript.echo method, the
simplest way is to invoke your script like so:

cscript //nologo c:\jambo.vbs > c:\output.txt

Loading...