Solved

Export results from a VB Script

Posted on 2015-02-05
4
84 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 52

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 14

Author Comment

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

Author Closing Comment

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

Expert Comment

by:Bill Prew
ID: 40619964
Welcome.

~bp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now