Page 1 of 1 1
Topic Options
#110081 - 2003-12-10 11:34 PM Determine default printer (WinXP).
Dunit Offline
Fresh Scripter

Registered: 2003-12-10
Posts: 5
Hi there! I've searched around the archives (and Google, etc) to find the answer to this but can't quite find what I'm after.
We need to find a users default printer and then map it to LPT1 for some legacy DOS applications.
"This answer has been posted loads of times!" I hear you cry! Yep, I've seen several very good posts on this but our particular problem is that we have a Group Policy in Active Directory which disables registry editing for all users. Therefore on our old NT machines in our Resource domain the old (existing) code which looks like this:

Code:
  
;$prn = READVALUE( "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device" )
;$pos = INSTR( $prn, "," ) ; Look for the first comma
;IF $pos <> 0 ; If a comma was found then strip them out
; $prn = SUBSTR( $prn, 1, $pos-1 )
;ENDIF
;$pos = INSTR( $prn, "\" ) ; Look for the first \\
;IF $prn <> "" AND $pos <>0
; USE LPT1: $prn ; Map LPT1: to the default printer
; COLOR c+/n
; AT(20,22) $prn + " > lpt1:"
;ELSE
; COLOR r+/n
; AT(20,22) 'No printer installed'
;ENDIF



Works nicely, but when we move new XP machines into the AD domain and apply group policy, the logon script stops at that point with a dialog box stating (not surprisingly) "Registry Editing has been disabled by your administrator" [OK].
I know that there are ways around it like creating security groups (ie per department) and using INGROUP as a condition to map what printer to LPT1 but that's a lot of work and adds to group maintenance.

Does anyone have any ideas on how we can determine which printer is set to default and map it to LPT1 without it checking thru the registry?
I have a feeling this may be really easy for someone OR really difficult/impossible! Either way your help is appreciated and I've learnt a lot from reading thru the various posts on this board (thanks!).

Regards,

Andrew.

Top
#110082 - 2003-12-10 11:48 PM Re: Determine default printer (WinXP).
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
nope.
what you actually said here is a REAL suprise.
there is not a single bit in your code that edits registry by any means.
if this really is the bit that makes the notice come up, report a bug to MS.

anyway, I might think there is something else in your script that really does edit reg.
_________________________
!

download KiXnet

Top
#110083 - 2003-12-10 11:54 PM Re: Determine default printer (WinXP).
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I'd be surprised if ReadValue() can be blocked by a policy. You could always use WMI.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#110084 - 2003-12-11 12:05 AM Re: Determine default printer (WinXP).
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Hey Andrew...

I agree with Lonk that the script you supplied doesnt do any registry editing. It simply reads...And I dont think a PC would be usable if the users couldnt atleast Read from the registry. So either there is some other part of the script you did not post..Or its a bug in MS.

Anyways...There is a vbs script on WinXP Pro machines in the system32 driectory called prnmngr.vbs which will get the default printer for you. I didnt see if you have anything in the AD that isnt XP or not...But for the XP machines you can try...

Code:
If @ProductType = "Windows XP Professional"
$tempdeffile = "%temp%\defprinter.xxx"
$tempdefhandle = FreeFileHandle()
If Exist ($tempdeffile)
Del $tempdeffile
Endif
Shell '%comspec% /c cscript %windir%\system32\prnmngr.vbs -g > '+$tempdeffile
If Open ($tempdefhandle,$tempdeffile) = 0
While @Error = 0 and $defaultprinter = ""
$line = ReadLine ($tempdefhandle)
If InStr ($line, "The default printer is")
$defaultprinter = Split ($line,"The default printer is ")[1]
Endif
Loop
$null = Close ($tempdefhandle)
Endif
? "Default Printer --> "+$defaultprinter
?
? "Press Any Key To Exit"
get $
Endif


Top
#110085 - 2003-12-11 12:07 AM Re: Determine default printer (WinXP).
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Les...I believe the .vbs script above uses WMI. So if you know how to do it without all of the bloat of the MS script...It would probably be much easier.
Top
#110086 - 2003-12-11 12:43 AM Re: Determine default printer (WinXP).
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
This is not silver platter stuff, but does show how WMI can be used.
Code:
Break ON
$objWMIService = GetObject("winmgmts:\\" + @WKSTA + "\root\cimv2")
$colItems = $objWMIService.ExecQuery("Select * from Win32_Printer",,48)
For Each $objItem in $colItems
? "Default: " + $objItem.Default
? "Description: " + $objItem.Description
? "Local: " + $objItem.Local
? "Name: " + $objItem.Name
? "PortName: " + $objItem.PortName
Next

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#110087 - 2003-12-11 01:56 AM Re: Determine default printer (WinXP).
Dunit Offline
Fresh Scripter

Registered: 2003-12-10
Posts: 5

Hi guys,

Thanks VERY much for such quick replies. Thanks also for pointing me in the direction of my brain- we are reunited and functioning (relatively) well again!
As soon as I read your reply Lonkero, I just thought "Oh right, yeah!"
Looking thru the script again, what it's actually doing AFTER that point is calling another script which (you guessed it) is trying to write a value to the runonce key!
I just sort of skipped over the "CALL OHS.KIX" without putting my brain into gear and *thinking*.

Thanks also to Les and Citrixman for your replies. I'll be looking at the .vbs script anyway because it looks interesting.

Thanks again guys, I could have been chasing my tail over such a silly thing and when you are collapsing domains and upgrading all the clients etc there's a million things to do!

Regards,

Andrew.

Top
#110088 - 2003-12-11 02:13 AM Re: Determine default printer (WinXP).
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
You would have been able to diagnose this problem your self by putting error checks into the code, e.g.
Code:

? 'Error '+@ERROR+' - '+@SERROR


behind the READVALUE() and using either the /d flag or DEBUG ON in the beginning of your code. Stepping through your code you would have discovered that the script successfully passes the READVALUE() and chokes on the WRITEVALUE().
_________________________
There are two types of vessels, submarines and targets.

Top
#110089 - 2003-12-11 05:20 PM Re: Determine default printer (WinXP).
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Les...Im trying to strip down the MS vbs script into a small kix script just for my own personal amusement...Except I cant get the $oEnum = oService.ExecQuery("select DeviceID from Win32_Printer where default=true") line to work. It just displays the select DeviceID from Win32_Printer where default=true in a command box. I know you do a lot of WMI so do you see anything wrong with it?

Code:
 
If GetDefault() = 0
? "Default Printer - "+$oPrinter.DeviceID
Else
? "Could Not Obtain Default Printer"
Endif
Get $

FUNCTION GetDefault()
$oLocator = CreateObject ("WbemScripting.SWbemLocator")
If Not $oLocator
Exit 1
Endif
$oService = $oLocator.ConnectServer(@WkSta, "root\cimv2")
If Not $oService
Exit 1
Endif
$oService.Security_.impersonationlevel = 3
$oEnum = oService.ExecQuery("select DeviceID from Win32_Printer where default=true")
For Each $oPrinter in $oEnum
$DefaultPrinter = $oPrinter.DeviceID
Next
ENDFUNCTION


Top
#110090 - 2003-12-11 05:24 PM Re: Determine default printer (WinXP).
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Missing $
Code:

$oEnum = $oService.ExecQuery("select DeviceID from Win32_Printer where default=true")

_________________________
There are two types of vessels, submarines and targets.

Top
#110091 - 2003-12-11 05:27 PM Re: Determine default printer (WinXP).
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
ohhhh..Stupid. Thanks for pointing that out Jens. I should have seen that myself...Works great now.

Code:
If GetDefault() = 0
? "Default Printer - "+$DefaultPrinter
Else
? "Could Not Obtain Default Printer"
Endif
Get $

FUNCTION GetDefault()
$oLocator = CreateObject ("WbemScripting.SWbemLocator")
If Not $oLocator
Exit 1
Endif
$oService = $oLocator.ConnectServer(@WkSta, "root\cimv2")
If Not $oService
Exit 1
Endif
$oService.Security_.impersonationlevel = 3
$oEnum = $oService.ExecQuery("select DeviceID from Win32_Printer where default=true")
For Each $oPrinter in $oEnum
$DefaultPrinter = $oPrinter.DeviceID
Next
ENDFUNCTION


Top
#110092 - 2003-12-12 10:58 PM Re: Determine default printer (WinXP).
Rocco Capra Offline
Hey THIS is FUN
*****

Registered: 2002-04-01
Posts: 380
Loc: Mansfield Ohio
Question with the "USE LPT1:"...

Quote:


;IF $prn <> "" AND $pos <>0
; USE LPT1: $prn ; Map LPT1: to the default printer
; COLOR c+/n
; AT(20,22) $prn + " > lpt1:"
;ELSE
; COLOR r+/n
; AT(20,22) 'No printer installed'
;ENDIF




Don't you have to be an admin of the PC to add "local" printers?

Rocco
_________________________
I Love this board!! -------------------- My DEV PC is running KIX 4.22 WINXP Pro

Top
#110093 - 2003-12-12 11:08 PM Re: Determine default printer (WinXP).
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Only for Windows XP/2003 by default. See KB313644 - Non-Administrators Cannot Remap an LPT Port to a Network Printer
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


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

Who's Online
1 registered (Allen) and 363 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.103 seconds in which 0.065 seconds were spent on a total of 12 queries. Zlib compression enabled.

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