Solved

Export results from a VB Script

Posted on 2015-02-05
4
88 Views
Last Modified: 2015-02-19
Can someone modify this script to export the results to an excel CSV file? Also is there a way to modify this to work on multiple computers?

strComputer = "atl-ps-01"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
     
For Each objPrinter in colPrinters
    If objPrinter.Attributes And 64 Then 
        strPrinterType = "Local"
    Else
        strPrinterType = "Network"
    End If
    Wscript.Echo objPrinter.Name & " -- " & strPrinterType
Next

Open in new window

0
Comment
Question by:Edward Pamias
[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
  • 2
  • 2
4 Comments
 
LVL 55

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40592737
Give this a try.  It takes in a text file of the names of the computers to look at, and outputs a CSV with the results.  Adjust the file names near the top of these two files.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Define input and output file names
Const Infile = "B:\EE\EE28611267\filein.txt"
Const Outfile = "B:\EE\EE28611267\fileout.csv"

' Create file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Open input and output files
Set objInfile = objFSO.OpenTextFile(Infile, ForReading, False, TriStateUseDefault)
Set objOutfile = objFSO.OpenTextFile(Outfile, ForWriting, True)

' Write out header line for CSV
objOutfile.WriteLine "ComputerName,PrinterName,PrinterType"

' Read input file line by line
Do While Not objInfile.AtEndOfStream
   ' Read the next line
   strComputer = objInfile.ReadLine

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

   Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
        
   For Each objPrinter in colPrinters
       If objPrinter.Attributes And 64 Then
           strPrinterType = "Local"
       Else
           strPrinterType = "Network"
       End If
       objOutfile.WriteLine strComputer & "," & Quote(objPrinter.Name) & "," & strPrinterType
   Next

   Set colPrinters = Nothing
   Set objWMIService = Nothing

Loop

' Cleanup and end
objInfile.Close
objOutfile.Close
Set objInfile = Nothing
Set objOutfile = Nothing
Set objFSO = Nothing

' Add surrounding double quotes to a string
Function Quote(s)
   Quote = Chr(34) & s & Chr(34)
End Function

Open in new window

~bp
0
 
LVL 18

Author Comment

by:Edward Pamias
ID: 40619639
This worked great! Thanks!
0
 
LVL 18

Author Closing Comment

by:Edward Pamias
ID: 40619643
This script worked great! Thanks!
0
 
LVL 55

Expert Comment

by:Bill Prew
ID: 40619964
Welcome.

~bp
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

688 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