dtadin
(Fresh Scripter)
2006-03-24 07:09 PM
Need help detecting USB or LPT1 printers

I need to add something to my login script that checks to see if the user currently has a LPT1 or USB printer as their default. If not I need to map the user to a network printer.
Now I know I can easly see if they have a LPT1 printer using IF EXIST('LPT1:')
any idea what I can do with a USB printer? DOT4_001 is the port showing in windows and doesnt seem to want to work if I use that in place of LPT1:

Anyhelp?

Thanks
Dennis Tadin


NTDOCAdministrator
(KiX Master)
2006-03-24 07:16 PM
Re: Need help detecting USB or LPT1 printers

Hello and Welcome to the board


You could read the registry and do an IfInStr after enuming the keys

HKEY_CURRENT_USER\Printers\Connections

If you need further assistance assembling the code to do this please let us know.


dtadin
(Fresh Scripter)
2006-03-24 08:27 PM
Re: Need help detecting USB or LPT1 printers

ntdoc I havent used kixtart to check the registry yet.
So yes im probley going to need some help with that part.
what am I looking for inside connections? I have users with a dozen or so diffrent types of printers some lpt some usb. I really dont want to check form every diffrent type of printer (I see in connections it lists them by names and not by port)


NTDOCAdministrator
(KiX Master)
2006-03-24 08:40 PM
Re: Need help detecting USB or LPT1 printers

Not sure... if I get time I'll look and see if one of the many UDFs can help or how to detect that alone.

I don't have any USB printer installed at the moment so can't really check, but I may have access to one that has a USB printer installed.


AllenAdministrator
(KiX Supporter)
2006-03-24 08:51 PM
Re: Need help detecting USB or LPT1 printers

PrinterList() will help you get the ports, but you will still need some code to figure out what printer is the default.

AllenAdministrator
(KiX Supporter)
2006-03-24 08:59 PM
Re: Need help detecting USB or LPT1 printers

Try this...
Code:
 
break on

$objWMIService = GetObject("winmgmts:\\.\root\cimv2")
$colPrinters = $objWMIService.ExecQuery("Select * From Win32_Printer Where Default = TRUE")
For Each $objPrinter in $colPrinters
? $objPrinter.Name
? $objPrinter.portname
Next



NTDOCAdministrator
(KiX Master)
2006-03-24 09:00 PM
Re: Need help detecting USB or LPT1 printers

Well there is a UDF to get that too

AllenAdministrator
(KiX Supporter)
2006-03-24 09:01 PM
Re: Need help detecting USB or LPT1 printers

I figured there was... I just couldn't put my hands on it.

NTDOCAdministrator
(KiX Master)
2006-03-25 12:17 AM
Re: Need help detecting USB or LPT1 printers

GetDefaultPrinter() - Returns default printer
http://www.kixtart.org/ubbthreads/showflat.php?Cat=0&Number=117544


AllenAdministrator
(KiX Supporter)
2006-03-25 12:58 AM
Re: Need help detecting USB or LPT1 printers

Just having a little fun with this...

Microsoft.com - Managing Printer Operations

Code:
break on 


$dp=GetDefaultPrinterProperties
? "Capabilities: " + join($dp.Capabilities,",")
? "CapabilityDescriptions: " + join($dp.CapabilityDescriptions,",")
? "Description: " + $dp.Description
? "DeviceID: " + $dp.DeviceID
? "DriverName: " + $dp.DriverName
? "Location: " + $dp.Location
? "Name: " + $dp.Name
? "PortName: " + $dp.PortName
? "Separtor File: " + $dp.SeparatorFile
? "ServerName: " + $dp.ServerName
? "ShareName: " + $dp.ShareName
? "StartTime: " + $dp.StartTime
? "TimeofLastReset: " + $dp.TimeOfLastReset
? "UntilTime: " + $dp.UntilTime


Function GetDefaultPrinterProperties()
dim $colprinters,$printer
$colprinters=GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * From Win32_Printer Where Default = TRUE")
if @error
exit @error
endif
for each $printer in $colprinters
$GetDefaultPrinterProperties=$printer
next
endfunction



Produces:
Code:
 
Capabilities: 4,3,5
CapabilityDescriptions: Copies,Duplex,Collate
Description:
DeviceID: hp LaserJet 1012
DriverName: hp LaserJet 1012
Location:
Name: hp LaserJet 1012
PortName: IP_192.168.0.2
Separtor File:
ServerName:
ShareName:
StartTime:
TimeofLastReset:
UntilTime:



NTDOCAdministrator
(KiX Master)
2006-03-25 03:40 AM
Re: Need help detecting USB or LPT1 printers

Allen can you confirm this works for you against a remote computer?

On every system I've tried so far it comes back blank.
It does work locally though.


AllenAdministrator
(KiX Supporter)
2006-03-25 04:32 AM
Re: Need help detecting USB or LPT1 printers

I did not do a whole lot of testing on it... and I bet your finding are right... I'll change it. Thanks for looking at it.

AllenAdministrator
(KiX Supporter)
2006-03-26 12:26 AM
Re: Need help detecting USB or LPT1 printers

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_printer.asp

Just reading through this article and noticed that the Default Property only works on XP and above.


NTDOCAdministrator
(KiX Master)
2006-03-26 06:28 AM
Re: Need help detecting USB or LPT1 printers

Yeah I know, but I've run it against other XP and 2003 systems and they come back blank.

Not sure if I'm having some type of networking issue or what so was curious if it worked remotely for you.


dtadin
(Fresh Scripter)
2006-03-27 09:25 PM
Re: Need help detecting USB or LPT1 printers

Ok guys taking your code I thought I had a solution. (This is just a test to see if it works) But it doesnt seem to want to work... Any ideas? Help this n00b kixtart user ;p

Code:
break on 


$dp=GetDefaultPrinterProperties
$printerport= "$dp.PortName"


? "Printer PortName: " + $dp.PortName


Function GetDefaultPrinterProperties()
dim $colprinters,$printer
$colprinters=GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * From Win32_Printer Where Default = TRUE")
if @error
exit @error
endif
for each $printer in $colprinters
$GetDefaultPrinterProperties=$printer
next
endfunction

if $printerport = ("IP_192.168.4.39")
? "IP Port Exists"
else
? "IP Port Does not Exist"
endif

if $printerport = "LPT1:"
? "LPT1 Port Exists"
else
? "LPT1 Port Does not Exist"
endif

if $printerport = "DOT4_001"
? "USB Port Exists"
else
? "USB Port Does not Exist"
endif



Witto
(MM club member)
2006-03-27 11:28 PM
Re: Need help detecting USB or LPT1 printers

I ran your script
Code:
$printerport= "$dp.PortName"

I would not use quotes because $dp is a variable
Code:
? "Printer PortName:            " + $dp.PortName

This returns the (IP) port name of my (default) home printer
Your script seems OK to me...


dtadin
(Fresh Scripter)
2006-03-27 11:39 PM
Re: Need help detecting USB or LPT1 printers

Um yeah witto, but the part thats not working is the
if $printerport = ("IP_192.168.4.39")
? "IP Port Exists"
else
? "IP Port Does not Exist"
endif

Change that ip to the ip of your home printer and see if it says IP Port exists... It doesnt detect it right. Not sure what I am doing wrong.


Witto
(MM club member)
2006-03-27 11:46 PM
Re: Need help detecting USB or LPT1 printers

Well, I added
Code:
If $printerport = "IP_192.168.222.1"
? "IP Port Exists"
Else
? "IP Port Does not Exist"
EndIf


and removed the other If/EndIf statements.
The output on the screen is:
Code:
Printer PortName:            IP_192.168.222.1
IP Port Exists



dtadin
(Fresh Scripter)
2006-03-27 11:56 PM
Re: Need help detecting USB or LPT1 printers

It was those damn quotes, I fixed that and your right its working now.

Thanks Witto!
Dtadin