Link to home
Start Free TrialLog in
Avatar of IT CAMPER
IT CAMPERFlag for United States of America

asked on

Need to keep local printer default after running con2prt batch file to install network printers.

I am running a bacth file with con2prt that installs several network printers to the local user when they logon.  The batch deletes current printers and reinstalls the list each time the user logs on.  I have left all of the con2prt commands as /c not having any of them install as default, knowing that the first one in the list gets tagged as default, by default.  The problem is that when a user with a local printer logs on, their local printer gets thrown out as default and the first network printers takes that position as default.  The user always needs to keep their local printer as default, if they have a local printer.  Otherwise, the batch works in setting the first network printer as default which is okay.  How do I keep the local printer default during this process.
Avatar of tim_mcgue
tim_mcgue

Have you thought about approaching the way you are deleting printers?  You can only delete network printers leaving the local printers (and ports) present with the following command:

RUNDLL32 PRINTUI.DLL,PrintUIEntry /dn <printer name>

To get the list of printers assigned to the user in your logon script enumerate the values (starting with "\\") in the following registry key:

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts

Also, you can use the same utility to set a printer as a default.  For instance:

rundll32 printui.dll,PrintUIEntry /y /n "printer"

You would have to search for a local printer on the machine in your script and then set it to default with the last command above.
Avatar of IT CAMPER

ASKER

I am having the script delete the installed network printers each time the user logs on so that any changes in printer configurations or if the user deletes a printer or has trouble with the driver, the whole setup will be re setup on each logon.  Also, if a printer goes down, that printer will not show up for the user to use as I will disable it in the script while it is down.  This cuts down on my support calls.  I am a little new at using the rundll32 command.  Can you give me more details on how I can use it to search for the local LPT1 or USB printer installed and set it as the deault once all of the script is finsihed installing the network printers?  Just to make sure you understand my position, the script delates and installs ONLY the network printers, but in this process the default is taken away from the local printer and palced on the first network printer installed even though I do not the con2prt /CD command for any of the network printers.  I just use the /C command to make the connection.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It looks like oBda nailed it.  Give oBda the points (as long as you agree).

Tim McGue
oBda....when I run your addition to my script it tells me the registry key cannot be found.  I browsed the registry and found the key, but maybe I am entering it wrong.  Here are a few questions.  Is HKCU the correct way to reference that key and is it able to reference Windows NT or does it need Windows_NT, WindowsNT?  Any other helpful hints for me?
If you use copy and paste to transfer the line into your script, you should be fine.
"HKCU" can (well, "should", to be more precise) be used with reg.exe.
Does it work when you start it from the command line?
Excuse my lask of experience.  I tried running reg query "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v Device at the command prompt and it returns unable to find the registry value
Now you have me flabbergasted. I just tested exactly your line (copy and paste) on three different machines: reg.exe v2.0 on W2k, reg.exe v1.05 (with a slightly modified command line due to a different argument syntax) and v2.0 on NT4, and reg.exe v2.0 and v3.0 on Windows XP. It worked in all cases.
Which version of reg.exe ("reg /?") are you using, and what is your OS (I'm assuming W2k)?
You were correct.  It does run great on an administrator account, but says it is not able to find registry key when I run it on a standard user account.  Is there some way to have the scripts run in administrator mode, kind of like the run-as command on programs, so that it can access the registry to perfrom this task.  The concept is perfect and does exactly what I need it to do except that it only runs on admin accounts.
This is a HKEY_CURRENT_USER registry access, so it's not a rights issue. Have you checked if the user you used to run the command has a default printer installed at all?
If so, have you checked with regedit if the specified key exists for this user's registry?
If it's just because the user doesn't have a default printer (in which case it can't be restored anyway), simply add "2^>NUL" to the reg-line to suppress the error output:

====8<----[logon.cmd]----
@echo off
setlocal
:: *** Save the current standard printer:
for /f "skip=3 tokens=2*" %%a in ('reg query "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v Device 2^>NUL') do set StdPrt=%%b
::*** Put your printer mappings here:
con2prt.exe /f


:: *** Restore the standard printer if necessary:
if not "%StdPrt%"=="" reg add "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v Device /d "%StdPrt%" /f >NUL
====8<----[logon.cmd]----
Hi oBdA,

 Any chance I can get you to comment on my post:
https://www.experts-exchange.com/questions/21183412/Roaming-Profiles-Local-Networked-Printers.html

  It's only 250 pts but I would much appreciate your expertise here..

Thanks,
-Mike-