Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript to export printer info and TCP/IP info to text file

Status
Not open for further replies.
Feb 12, 2009
3
US
I have a working vbscript that currently exports printer information to a text file as shown below. I need to be able to view the printer port's IP address into this script. Since this falls under the class w32.tcpipprinterport and not under w32.printer which my script is utilizing...i need help in "combining" the two classes. My working script is below:

Option Explicit
On Error Resume Next
Dim fso, src, dest, ts, output, temp, groups, user,WshShell,objWMIService,colInstalledPrinters,objPrinter,strPrinterStatus
Set fso = CreateObject("Scripting.FileSystemObject")
src = "computers.txt"
dest = "printers.csv"

If Not fso.FileExists(src) Then
WScript.Echo "File: """& src & " cannot be found."
Else
Set ts = fso_OpenTextFile(src,1)
Set output = fso.CreateTextFile(dest,True)
Do Until ts.AtEndOfStream
temp = ts.ReadLine

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

output.WriteLine string(0,vbtab) & objPrinter.ServerName
output.WriteLine ("STATUS") & vbTab & vbTab & ("PRINTER NAME") & vbTab & vbTab & ("CURRENT ACTIVE PORT")
output.writeline

For Each objPrinter in colInstalledPrinters
Select Case objPrinter.PrinterState
Case "0"
strPrinterStatus = "Ready"
Case "131072"
strPrinterStatus = "Toner Low"
Case "1024"
strPrinterStatus = "Printing"
Case "2"
strPrinterStatus = "Error"
Case "128"
strPrinterStatus = "Offline"
End Select

output.WriteLine string(0,vbTab) & strPrinterStatus & string(2,vbTab) & objPrinter.name & string(2,vbtab) & objPrinter.PortName
Next

Loop
End If


------------------------------------------

Thanks to anyone that can provide me a solution.

Ada
 
Unfortunately, the IP address you seek doesn't appear to show up in the Win32_TCPIPPrinterPort class in WMI. The class shows the field as existing, but it doesn't actually populate it. (I checked on several machines.)

ByteCount:
Caption:
CreationClassName: Win32_TCPIPPrinterPort
Description:
HostAddress:

Name: IP_172.25.114.70
PortNumber:
Protocol:
Queue:
SNMPCommunity:
SNMPDevIndex:
SNMPEnabled:
Status:
SystemCreationClassName: Win32_ComputerSystem
SystemName:
Type:

Notice that the Name field is the same as the PortName from Win32_Printer, but that HostAddress is never populated. Odd...

Take a look at prnadmin.dll, which is included with the Resource kit. Once you register the DLL, you can access all the properties of the printer.

The resource kit includes documentation and sample scripts.


PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Thanks for replying. I have the following script that I use occasionally that does export the port name and host address, and then writes to a text file. As you see, i am looking to merge the two classes (w32.printer and w32.tcpipprinterport together, for lack of better terms. Here is my working script to export port name with host address to a text file.
--------------------------------------------------------
Option Explicit
On Error Resume Next
Dim fso, src, dest, ts, output, temp, groups, user,WshShell,objWMIService,colInstalledPorts,objport
Set fso = CreateObject("Scripting.FileSystemObject")
src = "computers.txt"
dest = "ports.txt"

If Not fso.FileExists(src) Then
WScript.Echo "File: """& src & " cannot be found."
Else
Set ts = fso_OpenTextFile(src,1)
Set output = fso.CreateTextFile(dest,True)
Do Until ts.AtEndOfStream
temp = ts.ReadLine

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

output.WriteLine ("PRINTER NAME") & vbTab & vbTab & ("CURRENT ACTIVE PORT")
output.writeline

For Each objport in colInstalledPorts
output.WriteLine string(0,vbTab) & objport.Name & string(2,vbTab) & objPort.HostAddress

Next

Loop
End If
--------------------------------------------------------
 
Alright... Here's the deal. You can make a direct call to the WMI instance you are looking for by putting the following in your For...Next method:

Code:
Dim strHostAddr
strHostAddr =	GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
				& temp & "\root\cimv2:Win32_TCPIPPrinterPort=""" & _
				objPrinter.PortName & """").HostAddress


Since this is working for you, I can only suggest that you don't go this way. I've tried working with printers with WMI and had very poor results in general. As an example, I've just tried your script against several different print servers on different domains and environments, they all yield results like the following...

Script Output said:
PRINTER NAME CURRENT ACTIVE PORT

IP_192.168.220.107
IP_192.168.220.108
IP_192.168.220.112
IP_192.168.220.54
IP_192.168.220.61
IP_192.168.220.64
IP_192.168.220.67
IP_192.168.220.69
IP_192.168.220.70
IP_192.168.220.71
IP_192.168.220.73
IP_192.168.220.74
IP_192.168.220.76
IP_192.168.220.78
IP_192.168.220.79
IP_192.168.220.94
IP_192.168.220.97
IP_192.168.220.98


Notice that the Current Active Port field is blank for all instances?

Consider the prnadmin.dll. Here's a sample:

Code:
Option Explicit

Dim oMaster, oPrinter

Const sPServer = "\\yourservernamehere"

Set oMaster	= CreateObject("PrintMaster.PrintMaster.1")

' *** Get Configuration for every printer on sPServer ***
For Each oPrinter In oMaster.Printers(sPServer)
	wscript.echo "Printer Name.....: " & oPrinter.PrinterName
	wscript.echo "Share Name.......: " & oPrinter.ShareName
	wscript.echo "Port Name........: " & oPrinter.PortName
	wscript.echo "Driver Name......: " & oPrinter.DriverName
 	wscript.echo "Comment..........: " & oPrinter.Comment
	wscript.echo "Location.........: " & oPrinter.Location
	wscript.echo "Separator File...: " & oPrinter.Sepfile
	wscript.echo "Print Processor..: " & oPrinter.PrintProcessor
 	wscript.echo "Data type........: " & oPrinter.Datatype
 	wscript.echo "Parameters.......: " & oPrinter.Parameters
 	wscript.echo "Attributes.......: " & CStr(oPrinter.Attributes)
 	wscript.echo "Priority.........: " & CStr(oPrinter.Priority)
 	wscript.echo "Default Priority.: " & CStr(oPrinter.DefaultPriority)
 	wscript.echo "Start Time.......: " & CStr(oPrinter.StartTime)
 	wscript.echo "Until Time.......: " & CStr(oPrinter.UntilTime)
 	wscript.echo "Status...........: " & CStr(oPrinter.Status)
 	wscript.echo "Job count........: " & CStr(oPrinter.Jobs)
 	wscript.echo "Average PPM......: " & CStr(oPrinter.AveragePPM)
 	WScript.Echo
Next

You should be able to consistently get the IP address of the port from the "parameters" field.

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Thank you! I added the below to my code and it works perfect!
Dim strHostAddr
strHostAddr = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& temp & "\root\cimv2:Win32_TCPIPPrinterPort=""" & _
objPrinter.PortName & """").HostAddress
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top