Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBS Script to collecting information to CSV

Posted on 2010-11-17
9
Medium Priority
?
832 Views
Last Modified: 2012-06-27
Hi,

I am trying to work on a script to collect a bit of information and drop it into a CSV file for a migration.

I was wondering if someone can help me with creating one that does that following:


host name, username, ip address, network drives
Input dialouge box "First name"
Input dialouge box "Last Name"
Input dialouge box "Email address"

This data to be saved to CSV files.

I already have the below for the network drives. (saying that network drives are not a piority).


Dim objNetwork, objDrives, intIndex, objFSO, objFile, objUser, strUsername
 
Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
strUsername = objUser.samAccountName
 
Set objFSO = CreateObject("Scripting.FileSystemobject")
'Change the file path on the next line'
Set objFile = objFSO.CreateTextFile("SERVERPATH\test.csv" & strUsername & ".csv")
 
Set objNetwork = CreateObject("WScript.Network")
Set objDrives = objNetwork.EnumNetworkDrives
For intIndex = 0 To objDrives.Length - 1 Step 2
    objFile.WriteLine  """" & strUsername & """,""" & objDrives(intIndex) & """,""" & objDrives(intIndex + 1) & """"
Next
objFile.Close
Set objFile = Nothing
Set objUser = Nothing
Set objFSO = Nothing
Set objDrives = Nothing
Set objNetwork = Nothing

Open in new window

0
Comment
Question by:Eirejp
[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
  • 5
  • 4
9 Comments
 
LVL 1

Author Comment

by:Eirejp
ID: 34161059
Also the file must append.

Thanks!
0
 
LVL 1

Author Comment

by:Eirejp
ID: 34161332
Just an update to my previous entry.

I just need to add in hostname and ip address now.

Dim kNameFirst
Dim kNameLast
Dim EmailAdd

kNameFirst = InputBox("Please Enter your first name in Kanji? ¿¿¿¿¿¿", "Auditors 1")
kNameLast = InputBox("Please Enter your last name in Kanji? ¿?", "Auditor v1")
EmailAdd = InputBox("Please Enter your email address?", "Auditor v1")


Dim objNetwork, objDrives, intIndex, objFSO, objFile, objUser, strUsername
Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
strUsername = objUser.samAccountName

Set objFSO = CreateObject("Scripting.FileSystemobject")
'Change the file path on the next line'
Set objFile = objFSO.CreateTextFile("C:\Documents and Settings\cliebich\My Documents\test.csv" & strUsername & ".csv")

Set objNetwork = CreateObject("WScript.Network")
Set objDrives = objNetwork.EnumNetworkDrives
For intIndex = 0 To objDrives.Length - 1 Step 2
    objFile.WriteLine  """" & strUsername & """,""" & kNameFirst & """,""" & kNameLast & """,""" & EmailAdd & """,""" & ObjNetInfo & """,""" & objDrives(intIndex) & """,""" & objDrives(intIndex + 1) & """"
Next
objFile.Close
Set objFile = Nothing
Set objUser = Nothing
Set objFSO = Nothing
Set objDrives = Nothing
Set objNetwork = Nothing
 

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34161650
Hi there, try this.

Regards,

Rob.
Dim kNameFirst
Dim kNameLast
Dim EmailAdd

kNameFirst = InputBox("Please Enter your first name in Kanji? ¿¿¿¿¿¿", "Auditors 1")
kNameLast = InputBox("Please Enter your last name in Kanji? ¿?", "Auditor v1")
EmailAdd = InputBox("Please Enter your email address?", "Auditor v1")

Dim objNetwork, objDrives, intIndex, objFSO, objFile, objUser, strUsername
Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
strUsername = objUser.samAccountName

Set objFSO = CreateObject("Scripting.FileSystemobject")
'Change the file path on the next line'
Set objFile = objFSO.CreateTextFile("C:\Documents and Settings\cliebich\My Documents\test.csv" & strUsername & ".csv")

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 

Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
Set colComputerIP = objWMIService.ExecQuery("Select Name from Win32_NetworkAdapterConfiguration")
For Each objComputer In colComputer
    strHostName = objComputer.Name
Next
For Each IPConfig In colComputerIP
	If Not IsNull(IPConfig.IPAddress) Then 
        For intIPCount = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
			If strIPAddress = "" Then
				strIPAddress = IPConfig.IPAddress(intIPCount)
			Else
				strIPAddress = strIPAddress & " | " & IPConfig.IPAddress(intIPCount)
			End If
		Next
	End If
Next

Set objNetwork = CreateObject("WScript.Network")
Set objDrives = objNetwork.EnumNetworkDrives
For intIndex = 0 To objDrives.Length - 1 Step 2
    'objFile.WriteLine  """" & strUsername & """,""" & kNameFirst & """,""" & kNameLast & """,""" & EmailAdd & """,""" & ObjNetInfo & """,""" & objDrives(intIndex) & """,""" & objDrives(intIndex + 1) & """"
    objFile.WriteLine  """" & strUsername & """,""" & kNameFirst & """,""" & kNameLast & """,""" & EmailAdd & """,""" & strHostName & """,""" & strIPAddress & """,""" & ObjNetInfo & """,""" & objDrives(intIndex) & """,""" & objDrives(intIndex + 1) & """"
Next
objFile.Close
Set objFile = Nothing
Set objUser = Nothing
Set objFSO = Nothing
Set objDrives = Nothing
Set objNetwork = Nothing

Open in new window

0
Independent Software Vendors: 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:Eirejp
ID: 34161693
Hi Rob,

Seems to error on line 25

For Each IPConfig In colComputerIP

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34161701
Oops, change these lines:

Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
Set colComputerIP = objWMIService.ExecQuery("Select Name from Win32_NetworkAdapterConfiguration")

to this

Set colComputer = objWMIService.ExecQuery("Select Name from Win32_ComputerSystem")
Set colComputerIP = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration")


Regards,

Rob.
0
 
LVL 1

Author Comment

by:Eirejp
ID: 34161769
thanks this works perfectly!

The only issue now is the csv is been overwritten.

How can I change this to append?

Dim kNameFirst
Dim kNameLast
Dim EmailAdd

kNameFirst = InputBox("Please Enter your first name in Kanji? ??????", "Auditors 1")
kNameLast = InputBox("Please Enter your last name in Kanji? ??", "Auditor v1")
EmailAdd = InputBox("Please Enter your email address?", "Auditor v1")

Dim objNetwork, objDrives, intIndex, objFSO, objFile, objUser, strUsername
Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
strUsername = objUser.samAccountName

Set objFSO = CreateObject("Scripting.FileSystemobject")
'Change the file path on the next line'
Set objFile = objFSO.CreateTextFile("PATH\results.csv")

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 

Set colComputer = objWMIService.ExecQuery("Select Name from Win32_ComputerSystem")
Set colComputerIP = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration")


For Each objComputer In colComputer
    strHostName = objComputer.Name
Next
For Each IPConfig In colComputerIP
	If Not IsNull(IPConfig.IPAddress) Then 
        For intIPCount = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
			If strIPAddress = "" Then
				strIPAddress = IPConfig.IPAddress(intIPCount)
			Else
				strIPAddress = strIPAddress & " | " & IPConfig.IPAddress(intIPCount)
			End If
		Next
	End If
Next

Set objNetwork = CreateObject("WScript.Network")
Set objDrives = objNetwork.EnumNetworkDrives
For intIndex = 0 To objDrives.Length - 1 Step 2
        objFile.WriteLine  """" & strUsername & """,""" & kNameFirst & """,""" & kNameLast & """,""" & EmailAdd & """,""" & strHostName & """,""" & strIPAddress & """,""" & objDrives(intIndex) & """,""" & objDrives(intIndex + 1) & """"
Next
objFile.Close
Set objFile = Nothing
Set objUser = Nothing
Set objFSO = Nothing
Set objDrives = Nothing
Set objNetwork = Nothing

Open in new window

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 34162284
All you need to do is change this line:
Set objFile = objFSO.CreateTextFile("PATH\results.csv")

to this
Set objFile = objFSO.OpenTextFile("PATH\results.csv", 8, True)


Regards,

Rob.
0
 
LVL 1

Author Comment

by:Eirejp
ID: 34170504
Works like a charm!

Cheers Rob your a legend
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34170508
No problem.  Thanks for the grade.

Regards,

Rob.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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