VBSCRIPT to enumerate all ip printers on remote computers

Posted on 2006-05-04
Last Modified: 2007-12-19

I need to create a script that will allow me to input a list of PC's remotely and then have the script enumerate all the IP Printers for each PC and output that to a text file!!

Question by:mikeeubank
    1 Comment
    LVL 2

    Accepted Solution

    Try this...
    Create and paste the following in a vbs file:


    Option Explicit

    Const ListOnlyIPPrinters = true

    Dim LogFileName
    LogFileName = WScript.CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\printer_log.txt"

    DeleteFile( LogFileName )

    Dim sList
    Dim aList
    Dim sPC

    sList = InputBox("Enter list of computers comma delimited:", "Printer List")

    aList = Split(sList, ",")

    For Each sPC in aList
          sPC = Trim(sPC)

          ListIPPrinters( sPC )

          Call LogToFile( LogFileName, "")

    msgbox "done"

    Sub ListIPPrinters( ComputerName )
          Dim objWMIService, colInstalledPrinters, objPrinter

          Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & ComputerName & "\root\cimv2")
          If ERR.NUMBER <> 0 Then
                Call LogToFile( LogFileName, "Problem connecting to " & ComputerName & ". " & Err.Description)
                Exit Sub
          End If
          ON ERROR GOTO 0

          Call LogToFile( LogFileName, "Connected to " & ComputerName)

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

          Call LogToFile( LogFileName, vbTab & "Found " & colInstalledPrinters.count & " printers.")

          For Each objPrinter in colInstalledPrinters
                If ListOnlyIPPrinters Then
                      If instr(1, objPrinter.PortName, "IP_", 1) = 1 Then
                            Call LogToFile( LogFileName, vbTab & objPrinter.Name & " - " & objPrinter.PortName)
                      End If
                      Call LogToFile( LogFileName, vbTab & objPrinter.Name & " - " & objPrinter.PortName)
                End If
    End Sub

    Sub LogToFile( sFileName, sText )
          Dim FSO, File
          Set FSO = CreateOBject("Scripting.FileSystemObject")
          Set File = FSO.OpenTextFile( sFileName, 8, True)
          File.WriteLine( sText )
          Set File = Nothing
          Set FSO = Nothing
    End Sub

    Sub DeleteFile( sFileName )
          Dim FSO
          Set FSO = CreateOBject("Scripting.FileSystemObject")
          If FSO.FileExists( sFileName ) Then
                FSO.DeleteFile( sFileName )
          End If
          Set FSO = Nothing
    End Sub


    Just double click on the file to run.
    It will prompt you for a list of computers comma delimited (pc1,pc2,etc)
    It will then try to connect to each computer using WMI and list all printers to a text file on your desktop and prompt you when it's done.

    Some if this code is from

    Let me know if this works for you.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    zeroFront challenge 7 58
    Fix45 challenge 15 66
    Excel object stays open 19 56
    How Complex Is This Java Course ? 9 52
    This article will show, step by step, how to integrate R code into a R Sweave document
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    733 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

    22 Experts available now in Live!

    Get 1:1 Help Now