?
Solved

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

Posted on 2012-03-15
3
Medium Priority
?
180 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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

741 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