Solved

Export results from a VB Script

Posted on 2015-02-05
4
85 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
  • 2
  • 2
4 Comments
 
LVL 53

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 15

Author Comment

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

Author Closing Comment

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

Expert Comment

by:Bill Prew
ID: 40619964
Welcome.

~bp
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Programatically extract date from website 8 77
Added a column screws up code 5 57
Capture logon name 13 73
Delphi: barcode reading on android platform 1 30
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 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