Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie question of how to write output to a csv file

Status
Not open for further replies.
Oct 12, 2012
1
New to VBscript simply trying to figure out how to have the output written to a CSV

On Error Resume Next

strComputer = "."

Set objSysInfo = CreateObject("ADSystemInfo")
strComputername = objSysInfo.Computername

Set objcomputer = GetObject("LDAP://" & strComputerName)
strOUName = objcomputer.Parent

Set objOU = GetObject(strOUName)
Wscript.Echo objOU.distinguishedName

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
WScript.Echo "Name: " & objItem.Name
WScript.Echo "Domain: " & objItem.Domain
WScript.Echo "Manufacturer: " & objItem.Manufacturer
WScript.Echo "Model: " & objItem.Model
WScript.Echo "Total Physical Memory: " & objItem.TotalPhysicalMemory
WScript.Echo "UserName: " & objItem.UserName
Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
WScript.Echo "OS: " & objItem.Caption
WScript.Echo "Service Pack: " & objItem.CSDVersion
WScript.Echo "Build Date: " & WMIDateStringToDate(objItem.InstallDate)
Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled = 'True'", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
WScript.Echo "Description: " & objItem.Description
strIPAddress = Join(objItem.IPAddress, ",")
WScript.Echo "IP Address: " & strIPAddress
Next

WScript.Echo ("Done")

Function WMIDateStringToDate(dtmDate)
WScript.Echo dtm:
WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))
End Function
 
A CSV is nothing more that a text file with comma's separating the data. Make sure the data is encapsulated by double quotes (character 34) to ensure comma containing data does not alter the formatting.

Code:
'open new text file called output.csv
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile("output.csv", 2, true, 0)

'write header (optional)
objFile.WriteLine chr(34) & "Attribute" & chr(34) & "," & chr(34) & "Value" & chr(34)

'write new row
objFile.WriteLine chr(34) & "Name" & chr(34) & "," & chr(34) & objItem.Name & chr(34)

objFile.Close

-Geates

"I do not offer answers, only considerations."
- Geates's Disclaimer

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top