Page 1 of 2 12>
Topic Options
#171499 - 2006-12-14 03:23 PM Is WMI Broke?
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
This script seams to work fine for the local computer but if I try to use a remote computer name it does not work. I have all windows firewalls disabled so that should not have anything to do with it. Could you test this script on your network then let me know how it works?

Code:
GetWMIPrinters("NetAdmin2")

Function GetWMIPrinters($Computer)
	$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $Computer + "\root\cimv2")
	$colInstalledPrinters = $objWMIService.ExecQuery("Select * from Win32_Printer")
	For Each $objPrinter in $colInstalledPrinters
		? "Name: " + $objPrinter.Name
		? "Location: " + $objPrinter.Location
		? "Default: " + $objPrinter.Default
	Next
EndFunction
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#171500 - 2006-12-14 03:30 PM Re: Is WMI Broke? [Re: Benny69]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
You need to include checks after the GetObject() and ExecQuery - the error condition will help identify the cause of the problem.
Top
#171501 - 2006-12-14 03:34 PM Re: Is WMI Broke? [Re: Richard H.]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
you mean something like:
Code:
Function GetWMIPrinters($Computer)
	$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $Computer + "\root\cimv2")
	If Not(@ERROR = 0)
		? @ERROR
	EndIf
	$colInstalledPrinters = $objWMIService.ExecQuery("Select * from Win32_Printer")
	If Not(@ERROR = 0)
		? @ERROR
	EndIf
	For Each $objPrinter in $colInstalledPrinters
		? "Name: " + $objPrinter.Name
		? "Location: " + $objPrinter.Location
		? "Default: " + $objPrinter.Default
	Next
EndFunction
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#171502 - 2006-12-14 03:48 PM Re: Is WMI Broke? [Re: Benny69]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
If that is what you mean, i receive no errors.
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#171503 - 2006-12-14 03:51 PM Re: Is WMI Broke? [Re: Benny69]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Basically yes, though personally I'd prefer something a bit more like this:
Code:
Dim $colInstalledPrinters,$objPrinter
 
$colInstalledPrinters=GetWMIPrinters(".")
If @ERROR "Could not retrieve printers: ["+@ERROR+"] "+@SERROR+@CRLF Exit @ERROR EndIf
 
For Each $objPrinter in $colInstalledPrinters
	"Name: " + $objPrinter.Name + @CRLF
	"Location: " + $objPrinter.Location + @CRLF
	"Default: " + $objPrinter.Default + @CRLF
Next

Function GetWMIPrinters($Computer)
	Dim $objWMIService
	$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $Computer + "\root\cimv2")
	If @ERROR Exit @ERROR EndIf
	If VarType($objWMIService)<>9 Exit 2 EndIf
	$GetWMIPrinters=$objWMIService.ExecQuery("Select * from Win32_Printer")
	Exit @ERROR
EndFunction


Edited by Richard H. (2006-12-14 03:53 PM)

Top
#171504 - 2006-12-14 03:56 PM Re: Is WMI Broke? [Re: Richard H.]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
ok, tried your script and as with mine it works localy but not remotely and i receive no errors.
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#171505 - 2006-12-14 04:07 PM Re: Is WMI Broke? [Re: Benny69]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Anything in the event log on the remote computer?

It's also worth checking the object properties, to see if there is an internal error state field.

Top
#171506 - 2006-12-14 04:14 PM Re: Is WMI Broke? [Re: Richard H.]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
nope, nothing. It works for you on your network? A few days ago I asked Doc to test it on his network and he said it did not work for him. He thought that there might have been some recent security change in microsoft.
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#171507 - 2006-12-14 04:15 PM Re: Is WMI Broke? [Re: Richard H.]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Maybe getting no printers is actually correct?

I've just used the script against some of my Citrix farm members.

Although these will have dozens of (network) printers connected for all the users, the only printer that the script actually reports is the "Microsoft Office Document Image Writer" which is created when Office is installed.

The same applies when run against co-worker's machine - only the Office printer appears.

Perhaps you need to run the command in a different context?

Top
#171508 - 2006-12-14 04:17 PM Re: Is WMI Broke? [Re: Richard H.]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Silly question...
I hope you are searching for local installed printers?

Top
#171509 - 2006-12-14 04:21 PM Re: Is WMI Broke? [Re: Richard H.]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
idk, if i go to the remote machine and run the script localy it returns all the printers on that machine, it just does not seam to want to work remotely. The strainge thing is, that this is the suggested script by microsoft to look at remote computers. I think they have changed something and they are not admiting to it.

I have also tried to look at the remote registry with the same results:
Code:
$SubKeys = GetPrinterList("NetAdmin2")
$SubKeys = Split($SubKeys,",")
For Each $Key in $SubKeys
	? $Key
Next

Function GetPrinterList($Computer)
	If @INWIN = 1
		;$GetPrinterList = GetSubKeyValues("\\"+$sComputer+"\HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices")
		$GetPrinterList = GetSubKey("\\"+$Computer+"\HKCU\Printers\Connections")
	Else
		
	EndIf
EndFunction

Function GetSubKey($SubKey)
	Dim $Index
	
	$Index = 0
	$Key = EnumKey($Subkey,$Index)
	While @ERROR = 0
		If $GetSubKey = ""
			$GetSubKey = $Key
		Else
			$GetSubKey = $GetSubKey + "," + $Key
		EndIf
		$Index = $Index + 1
		$Key = EnumKey($Subkey,$Index)
	Loop
EndFunction
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#171510 - 2006-12-14 04:29 PM Re: Is WMI Broke? [Re: Benny69]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
IDK
Means
I Don't know?

I can imagine that if you query a remote computer, you only get the local printers. I presume you would have to know who the current user is to get the network printers for that user.

Top
#171511 - 2006-12-14 04:30 PM Re: Is WMI Broke? [Re: Benny69]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
I guess that is your problem - there is no remote access to "HKCU".

Only locally installed printers (HKLM) are going to show up.

Top
#171512 - 2006-12-14 04:33 PM Re: Is WMI Broke? [Re: Richard H.]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Maybe if you first can catch the @SID of the user(s) that is (are) logged on...
Top
#171513 - 2006-12-14 04:35 PM Re: Is WMI Broke? [Re: Richard H.]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
Ok, that would explain why it does not work when I try to look at the remote registry. I am still confused as to why the WMI does not work.
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#171514 - 2006-12-14 04:39 PM Re: Is WMI Broke? [Re: Benny69]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Same reason I guess - unless you can use the context of the logged in user.

Take my example of our Citrix servers. Which one of the 60 logged in users is the one who's printer list you'd return?

Top
#171515 - 2006-12-14 04:41 PM Re: Is WMI Broke? [Re: Witto]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Originally Posted By: Witto
Maybe if you first can catch the @SID of the user(s) that is (are) logged on...


...or just enumerate all the users in HKEY_USERS.

Top
#171516 - 2006-12-14 04:59 PM Re: Is WMI Broke? [Re: Richard H.]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
What you guys are saying makes some sence, but in the microsoft script repository the description for this script says, 'Lists all the printer connections on a computer.' It does not elude to anything that would be constricted by whatever user is logged on.

Quote:

List Printer Connections

Description

Lists all the printer connections on a computer.

Supported Platforms

Windows Server 2003
Yes

Windows XP
Yes

Windows 2000
No

Windows NT 4.0
No

Windows 98
No


Script Code
Code:
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

_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#171520 - 2006-12-14 06:46 PM Re: Is WMI Broke? [Re: Benny69]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
I know my UDF worked in the past and its doing pretty much the same wmi code... try it and see what kind of results you get:
PrinterList()

Top
#171521 - 2006-12-14 06:59 PM Re: Is WMI Broke? [Re: Allen]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
Allen,
Your code as i used it:
Code:
Break on
$RC=SetOption("WrapAtEOL","on") 

$array=PrinterList("NetAdmin2",0)
For Each $printer in $array  
  ? $printer  
Next

Get $a

Function PrinterList(optional $remotepc, optional $displaymode)
	Dim $service,$printer,$printers,$printerdesc[0],$counter,$portname,$printername
	If $remotepc=""
		$remotepc="."
	EndIf
	$Service = GetObject("winmgmts:\\" + $remotepc + "\root\cimv2")
	If @error
		Exit @error
	EndIf
	$Printers=$service.execquery ('select * from Win32_Printer')
	For Each $printer in $printers
		ReDim preserve $printerdesc[$counter]
		If $displaymode & 1
			$portname = "," + $printer.portname
		EndIf
		Select
			Case $displaymode & 4 ;remote printers
				If Left($printer.portname,2)="\\" Or Left($printer.name,2)=="\\"
					$printername=$printer.name
				EndIf
			Case $displaymode & 2 ;local printers
				If Left($printer.portname,2)<>"\\" And Left($printer.name,2)<>"\\"
					$printername=$printer.name
				EndIf
			Case 1 ; all printers
				$printername=$printer.name
		EndSelect
		If $printername<>""
			$printerdesc[$counter]=$printername + $portname
			$counter=$counter + 1
			$printername=""
		EndIf
	Next
	$PrinterList=$printerdesc
EndFunction

I get the same results, it works localy but not remotely.
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
Page 1 of 2 12>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
2 registered (morganw, mole) and 414 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.072 seconds in which 0.023 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org