• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 611
  • Last Modified:

Multiple wmi queries but giving the same ip address problem

Hi guys,
One of you kind gurus helped me with the following script.
What im trying to work out is why this script is returning the same ip address for each network printer, instead of the correct ip address.
Im using 2 select statements that query different win32 classes in order to retrieve different information about the printers. The problem is is that when the function is called, it seems to not return the correct ip address, and I'd like to get this sorted.
I dont want to use just the one class, and would like to learn how to query different classes in the one vbscript.
Any help on this would be much appreciated.
I have attached the code below. Thanks everyone.
strComputer = "serverA"
 
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
 
Set colItems = objWMIService.ExecQuery _
("SELECT * FROM Win32_Printer")
 
 
 
For Each objItem In colItems
 
WScript.Echo objItem.DeviceID & vbTab & objItem.DriverName & vbTab & GetPrinterIP(strComputer)
 
Next
 
 
 
Function GetPrinterIP(strComputer)
 
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colPorts =  objWMIService.ExecQuery _
    ("Select * from Win32_TCPIPPrinterPort")
 
For Each objPort in colPorts
 
    GetPrinterIP = objPort.HostAddress
 
Next
 
End Function

Open in new window

0
Simon336697
Asked:
Simon336697
  • 2
  • 2
  • 2
2 Solutions
 
käµfm³d 👽Commented:
If you try the following, does it correct the issue?
strComputer = "serverA"
 
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
 
Set colItems = objWMIService.ExecQuery _
("SELECT * FROM Win32_Printer")
 
 
 
For Each objItem In colItems
 
WScript.Echo objItem.DeviceID & vbTab & objItem.DriverName & vbTab & GetPrinterIP(strComputer)
 
Next
 
 
 
Function GetPrinterIP(strComputer)
 
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colPorts =  objWMIService.ExecQuery _
    ("Select * from Win32_TCPIPPrinterPort")
 
For Each objPort in colPorts
 
    GetPrinterIP = GetPrinterIP & " || " & objPort.HostAddress
 
Next
 
End Function

Open in new window

0
 
TakedaTCommented:
If you want to query two classes about the same printer, you need to find something in common.  I looked at both, and it looks like the Portname element of the win32_printer class matches the Name element of the win32_tcpipprinterport class.  So, when I pass the common element title (portname) to the function that checks the other class, I tell the function to set the IP only if the Name matches PortName.

I also noticed that you were passing the strComputer string to the function.  Since you had the strComputer string defined globally, the function would already know about it and it would be unneccessary.  So, I just replaced it with the portname.

I know this may be a bit confusing, but let me know if I can explain further.
strComputer = "serverA"
 
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colItems = objWMIService.ExecQuery _
("SELECT * FROM Win32_Printer")
 
For Each objItem In colItems
	WScript.Echo objItem.DeviceID & vbTab & objItem.DriverName & vbTab & GetPrinterIP(objItem.PortName)
Next
 
Function GetPrinterIP(strPortName)
 	Set objWMIService = GetObject("winmgmts:" _
   		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colPorts =  objWMIService.ExecQuery _
    	("Select * from Win32_TCPIPPrinterPort")
	For Each objPort in colPorts
   		If objPort.Name = strPortName then GetPrinterIP = objPort.HostAddress
	Next
End Function

Open in new window

0
 
käµfm³d 👽Commented:
For reference, line 30 was altered.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Simon336697Author Commented:
Hi Takeda,
That works perfectly mate thank you so much.
Is this the way you gurus normally do it when you wish to query multiple classes and have the output as I do here?

kaufmed thanks so much for your help as well.
The output from your code gave me every single ip address it seems for each printer, so it had a list of every ip address returned for each printer.
0
 
TakedaTCommented:
Everyone has their own methods, its whatever works for you.  It also depends on what the situation calls for.  And there are other scripters that are far beyond my skill who would do things differently Im sure.

You can be more specific in the wmi queries like in this.  I modified the function at the bottom to only pull more specific elements.  I also formatted the output to have carriage returns so its easier to read.

strComputer = "serverA"
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
	("SELECT * FROM Win32_Printer")
 
For Each objItem In colItems
	WScript.Echo "Printer:	"&objItem.DeviceID & vbCrLf & "Driver:	"&objItem.DriverName &_
		vbCrLf & "IP:	"&GetPrinterIP(objItem.PortName)
Next
 
Function GetPrinterIP(strPortName)
	Set colPorts =  objWMIService.ExecQuery _
    	("Select HostAddress from Win32_TCPIPPrinterPort where Name = '"&strPortName&"'")
	For Each objPort in colPorts
   		GetPrinterIP = objPort.HostAddress
	Next
	If GetPrinterIP = "" then GetPrinterIP = "No IP"
End Function

Open in new window

0
 
Simon336697Author Commented:
Champion champion thank you so much mate :>)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now