#136076 - 2005-03-22 08:11 PM
PrinterList() UDF only works on my machine.
|
technician
Fresh Scripter
Registered: 2005-03-22
Posts: 9
|
I'm writing a KiXtart script that will remove all local printers and re-install the printers I choose. I've been playing around with the PrinterList() UDF by Allen and got it to list the printers on my personal machine (admin permissions, WinXP SP2) both local and network. I cannot get the same results from other workstations on my network (admin permissions, WinXP SP1). The only difference in machines is service pack 2 installed on my machine.
Here's a copy of the code I've been tinkering with:
Code:
COLOR w+/b
break on
$array=printerlist()
for each $printer in $array
? $printer
next
;if delprinterconnection($printer) = 0 ; ? "Deleted printers!" ;else ; ? "Unable to remove printers!" ;endif
;if addprinterconnection("\\Calvin\Canon") = 0 ; ? "Added printer \\Calvin\Canon" ;else ; ? "ERROR: Unable to add printer \\Calvin\Canon!" ;endif
;if setdefaultprinter("\\Calvin\Canon") = 0 ; ? "Default printer is now set to \\Calvin\Canon" ;else ; ? "ERROR: Unable to set a default printer!" ;endif
EXIT
Function Printerlist(optional $remotepc,optional $displaymode)
dim $service,$printer,$printers,$printerdesc
if $remotepc=""
$remotepc='\\'+ @wksta
else
if not left($remotepc,2)="\\"
$remotepc='\\' + $remotepc
endif
endif
$Service=GetObject("winmgmts:{impersonationLevel=impersonate}!" + $remotepc + "\root\cimv2")
if not @error=0
exit @error
endif
$Printers=$service.execquery ('select * from Win32_Printer')
for each $printer in $printers
select
case $displaymode=1
if $printerdesc=""
$printerdesc=$printer.name + "," + $printer.portname
else
$printerdesc=$printerdesc + "|" + $printer.name + "," + $printer.portname
endif
case $displaymode=0
if $printerdesc=""
$printerdesc=$printer.name
else
$printerdesc=$printerdesc + "|" + $printer.name
endif
endselect
next
$printerlist=split($printerdesc,"|")
endfunction
Any ideas on how I could be slipping up?
|
Top
|
|
|
|
#136078 - 2005-03-22 08:26 PM
Re: PrinterList() UDF only works on my machine.
|
technician
Fresh Scripter
Registered: 2005-03-22
Posts: 9
|
I have kixtart running @ NETLOGON, so I login as admin and run \\CALVIN\NETLOGON\kix32.exe test.kix (test.kix resides in my network share which is mapped to z:\). I was planning on running it as a logon script, but have been calling it manually to make sure it works for now. I have verified that all the kix files have the correct permissions.
I am gunning for polling the local computer, since I have no way of knowing when these machines are on/off.
|
Top
|
|
|
|
#136080 - 2005-03-23 04:08 PM
Re: PrinterList() UDF only works on my machine.
|
technician
Fresh Scripter
Registered: 2005-03-22
Posts: 9
|
On another workstation I receive the following message after inputting your code after $array=printerlist():
Quote:
ERROR: -2147217467 Number of printers: -11
I have one installed printer on the workstation which was there before I tried running the script.
When I run your code on my personal machine I receive the following:
Quote:
Error: 0 Number of printers: 61 SnagIt 7 pdfFactory Pro Microsoft Office Document Image Writer HP LaserJet 2300 Series PCL 5e hp deskjet 990c \\Calvin\Canon \\CALVIN\PC Lab Laserjet
As you can see I don't have 61 printers installed, but maybe it's grabbing that from the number of drivers installed. It does display the correct printers installed on my machine though.
|
Top
|
|
|
|
#136081 - 2005-03-23 04:29 PM
Re: PrinterList() UDF only works on my machine.
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4562
Loc: USA
|
The first problem with the counts is actually an easy one.
Try this instead: ? "Number of Printers: " + (val(ubound($array)) + 1)
The second problem appears to be a com error: try replacing the @error line with code below to find out what the error is.
Code:
cerror @error " " @serror
Function Cerror() exit Iif(@error<0,VAL("&"+Right(DecToHex(@error),4)),@error) Endfunction
|
Top
|
|
|
|
#136082 - 2005-03-23 05:54 PM
Re: PrinterList() UDF only works on my machine.
|
technician
Fresh Scripter
Registered: 2005-03-22
Posts: 9
|
Okay, now I'm getting some interesting information.
Quote:
4097 Error (317 / 13D) while retrieveing error information for 1001Win32_Printer Error: 0
It's now outputting the correct # of printers
|
Top
|
|
|
|
#136084 - 2005-03-23 08:56 PM
Re: PrinterList() UDF only works on my machine.
|
technician
Fresh Scripter
Registered: 2005-03-22
Posts: 9
|
Here's the complete script I'm playing with:
Code:
COLOR w+/b
break on
$array=printerlist()
? "Error: " + @error ;? "Number of printers: " + ubound($array) + 1 ? "Number of Printers: " + (val(ubound($array)) + 1)
for each $printer in $array
? $printer
next
;if delprinterconnection($printer) = 0 ; ? "Deleted printers!" ;else ; ? "Unable to remove printers!" ;endif
;if addprinterconnection("\\Calvin\Canon") = 0 ; ? "Added printer \\Calvin\Canon" ;else ; ? "ERROR: Unable to add printer \\Calvin\Canon!" ;endif
;if setdefaultprinter("\\Calvin\Canon") = 0 ; ? "Default printer is now set to \\Calvin\Canon" ;else ; ? "ERROR: Unable to set a default printer!" ;endif
EXIT
Function Printerlist(optional $remotepc,optional $displaymode)
dim $service,$printer,$printers,$printerdesc
if $remotepc=""
$remotepc='\\'+ @wksta
else
if not left($remotepc,2)="\\"
$remotepc='\\' + $remotepc
endif
endif
$Service=GetObject("winmgmts:{impersonationLevel=impersonate}!" + $remotepc +
"\root\cimv2")
; if not @error=0
; exit @error
; endif
cerror @error " " @serror
$Printers=$service.execquery ('select * from Win32_Printer')
for each $printer in $printers
select
case $displaymode=1
if $printerdesc=""
$printerdesc=$printer.name + "," + $printer.portname
else
$printerdesc=$printerdesc + "|" + $printer.name + "," + $printer.portname
endif
case $displaymode=0
if $printerdesc=""
$printerdesc=$printer.name
else
$printerdesc=$printerdesc + "|" + $printer.name
endif
endselect
next
$printerlist=split($printerdesc,"|")
endfunction
Function Cerror() exit Iif(@error<0,VAL("&"+Right(DecToHex(@error),4)),@error) Endfunction
|
Top
|
|
|
|
#136087 - 2005-03-23 09:30 PM
Re: PrinterList() UDF only works on my machine.
|
technician
Fresh Scripter
Registered: 2005-03-22
Posts: 9
|
Kix version is the latest stable off the website.
The code I posted here is what I ran on both my personal workstation and a random workstation out in my library, no changes (that's why it's stumping me that it would work one place and not another). When I mean running it I mean physically sitting down at the workstation and typing in \\CALVIN\NETLOGON\kix32.exe test.kix, not running any scripts over the network to poll a remote machine I am not sitting at.
I'll change the cerror deal as you mentioned. Will the output change enough to post it here?
|
Top
|
|
|
|
#136090 - 2005-03-25 05:43 PM
Re: PrinterList() UDF only works on my machine.
|
technician
Fresh Scripter
Registered: 2005-03-22
Posts: 9
|
Quote:
Thanks for explaining... and that is odd. Are you logging in as the same user on the different PCs? If not you might try it to see if its a permissions issue.
When I wrote this UDF, I'm sure it was done on Windows 2000 SP4 or SP3. So I feel pretty confident XP would work as well, regardless of it SP.
Yes, I'm logging in as the exact same administrator account on both machines. Since the script is kept on a account share over the network, I even tried copying it to the local machine but it didn't make a difference.
This is very strange, has me completely stumped.
|
Top
|
|
|
|
#136092 - 2005-03-25 08:47 PM
Re: PrinterList() UDF only works on my machine.
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4562
Loc: USA
|
I have no idea why its not working. I just tested it on my XP SP2 box with no problems.
Output: PDF995 Intuit Internal Printer HP LaserJet 1100 (MS) Fax Auto HP LaserJet 1100 (MS) on HOMEPC
Jooel...man you are anti-XP...
|
Top
|
|
|
|
#136093 - 2005-03-25 08:55 PM
Re: PrinterList() UDF only works on my machine.
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4562
Loc: USA
|
Hey Tech... you might try running Kixomatic on the machines you are having trouble with, and see if it has anything to do with WMI.
|
Top
|
|
|
|
#136094 - 2005-03-28 05:02 PM
Re: PrinterList() UDF only works on my machine.
|
technician
Fresh Scripter
Registered: 2005-03-22
Posts: 9
|
Just a little update, I went into one of my computer labs and ran the script on a machine in there (settings are identical as far as I'm aware) and it [the script] worked perfectly. Now I'm pulling my hair trying to figure out what in the world is different between these PCs. Is WMI a package I can download and run from Microsoft?
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1574 anonymous users online.
|
|
|