#110081 - 2003-12-10 11:34 PM
Determine default printer (WinXP).
|
Dunit
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
|
|
|
|
#110084 - 2003-12-11 12:05 AM
Re: Determine default printer (WinXP).
|
ShaneEP
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
|
|
|
|
#110087 - 2003-12-11 01:56 AM
Re: Determine default printer (WinXP).
|
Dunit
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
|
|
|
|
#110089 - 2003-12-11 05:20 PM
Re: Determine default printer (WinXP).
|
ShaneEP
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
|
|
|
|
#110091 - 2003-12-11 05:27 PM
Re: Determine default printer (WinXP).
|
ShaneEP
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 507 anonymous users online.
|
|
|