Solved

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

Posted on 2012-03-15
3
177 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
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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