Solved

Export results from a VB Script

Posted on 2015-02-05
4
83 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 51

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 51

Expert Comment

by:Bill Prew
ID: 40619964
Welcome.

~bp
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 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

12 Experts available now in Live!

Get 1:1 Help Now