Solved

VB Script to query printers and write to a .csv file on the network

Posted on 2012-03-15
3
178 Views
Last Modified: 2014-08-01
OK. here is the issue. I need a script to query 12k computers for a certain printer server. Then, if you have the certain print server exsists (\\PRINTSVRCC01) then I want it to write the information to a .csv file located on a network share. \\myserver\share\printresults.csv. The information should include the printer server name, print queue and their host name.

Below is a script that I am starting but could use some help. I am new to vb and not sure how to resolve this.



strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")

For Each objPrinter in colInstalledPrinters
    Wscript.Echo "Name: " & objPrinter.Name
    Wscript.Echo "Location: " & objPrinter.Location
    Wscript.Echo "Default: " & objPrinter.Default
   Next

Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("\\myserver\share\printresults.csv", ForAppending, True)

Set objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery("Select * from Win32_Service")

For Each objService in colServices    
    objTextFile.WriteLine(objService.DisplayName & vbTab & _
        objService.State)
Next
objTextFile.Close
0
Comment
Question by:Willing2Learn_More
[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
3 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 37727744
Hi, see how this code goes.

Regards,

Rob.

Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
Set objPrinters = objNetwork.EnumPrinterConnections

strPrintServer = "\\YourPrintServer\"

For i = 0 To objPrinters.Count - 1 Step 2
	If LCase(Left(objPrinters(i + 1), Len(strPrintServer))) = LCase(strPrintServer) Then
		Set objTextFile = objFSO.OpenTextFile("\\myserver\share\printresults_" & objNetwork.ComputerName & ".csv", ForAppending, True)
		objTextFile.WriteLine """" & objNetwork.ComputerName & """,""" & strPrintServer & """,""" & Replace(objPrinters(i + 1), strPrintServer, "", 1, -1, vbTextCompare) & """"
		objTextFile.Close
	End If
Next

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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