Solved

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

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
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…

831 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