Link to home
Start Free TrialLog in
Avatar of advmicro
advmicro

asked on

Mapping a printer by way of a subnet or ipaddress

I  have two servers. One on 192.168.1.x and 192.168.2.x. Each server is in a different location. I need to create a login script that will map the appropriate printer for the ip address. For example, a client logs in at one location and gets a  printer, currently, and then, if they go to the other location that printer is still there. I need to make it so it adds the printer for that location ONLY so they wont be printing to the wrong location. In addition, I need to make that the printer default.
Here is the script that another person here helped me out with.
However, it is still not working.

I continue to get a UNC path error if I try running it from using explorer to the server.
I get no error, if I run it from a shortcut on the desktop, but it still will not work.



@echo off

for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find "IP Address"') do set IP ADDR=%%a
for /f "tokens=1,2,3 delims=." %%a in ("%ipaddr%") do set SUBNET=%%a.%%b.%%c

if "%subnet%"=="192.168.1" goto subnet1
if "%subnet%"=="192.168.2" goto subnet2

echo On another subnet (%subnet%)
goto next

:subnet1
  echo Running on subnet 1 (%subnet%) from %ipaddr%
  rundll32 printui.dll,PrintUIEntry /dn /n \\saco\HP4200
  rundll32 printui.dll,PrintUIEntry /in /n /y \\lewiston-server\HP4200

goto next

:subnet2
  echo Running on subnet 2 (%subnet%) from %ipaddr%
  rundll32 printui.dll,PrintUIEntry /dn /n \\lewiston-server\HP4200
  rundll32 printui.dll,PrintUIEntry /in /n /y \\saco\HP4200

goto next

:next
:end


btw
 the "lewiston-server"  and "Saco" are  Windows 2000 Servers
and the logon script is on a shared folder of a Windows2003 server
Avatar of tymes
tymes
Flag of Canada image

Well, the UNC error probably means nothing... it may zoom by quickly but it may say...
  CMD.EXE was started with the above path as the current directory.
  UNC paths are not supported.  Defaulting to Windows directory.

So it is just a warning message saying its probably running from c:\windows and it should affect or impair the script.


Now, for the rest of it, I tried this myself and it didn't work because I'm using vista which doesn't say "IP Address" but uses "IPv4 Address".   So I've just rewrote it quickly and actually condensed the two lines into 1 that should work on more platforms.... (I've tried on 2000, 2003, XP and Vista).

First I should add that in what you posted here here there is an extraneous space in "IP ADDR=" which may just be a copy/paste or web formatting problem.

Next, my code has me replacing the two FOR lines with this one line... actually for reading I'll split it into three lines... (there is a PERIOD and SPACE after delims= before the quote)

for /f "tokens=9,10,11 delims=. " %%a in ('route print 0.0.0.0 ^|find "0.0.0.0"') do (
   set SUBNET=%%a.%%b.%%c
)


So unless it was that extra space in IPADDR which you should have noticed since it would always say
On another subnet ()
It would work for me as you had it.


You might try a variant and want to replace the delete and add commands with the vbs equivilants...
gee... I do have to detect XP/VISTA/2003 for this so add some lines at the top to detect VISTA

for /f "tokens=3 delims=.]" %%a in ('ver') do set WINVERS=%%a
IF "%WINVERS%" equ "6000" (
  set x_PrnDEL_cmd=cscript %windir%\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -d -p
  set x_PrnADD_cmd=cscript %windir%\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -ac -p
) else (
  rem 2003/WINXP  (not 2000 which doesn't have prnmngr.vbs by default -- copy it)
  set x_PrnDEL_cmd=cscript %windir%\System32\prnmngr.vbs -d -p
  set x_PrnADD_cmd=cscript %windir%\System32\prnmngr.vbs -ac -p
)

%WINVERS% will now contain 2195 for 2000, 2600 for WXP, 3790 for 2003, 6000 for Vista and we did some scripting based on the OS version.

Anyways,  now rather than
  rundll32 printui.dll,PrintUIEntry /dn /n \\saco\HP4200
  rundll32 printui.dll,PrintUIEntry /in /n /y \\lewiston-server\HP4200
You can have
 %x_PrnDEL_cmd%   \\saco\HP4200
 %x_PrnADD_cmd%  \\lewiston-server\HP4200
which use the VBS versions which may be more robust and work better.  the Variable themselves could even expand to your old commands using rundll32 and the printui.dll interface and you just can configure these as "constants" at the top of the script and they will expand later like using subroutines using which of the 2 methods you find more effective.

In my old scripts I actually notice that I mix them and configure
 set x_PrnDEL_cmd=rundll32 printui.dll,PrintUIEntry /q /dn /n
 set x_PrnADD_cmd=cscript %winsys%\prnmngr.vbs -ac -p
So when I wrote some of my own scripts years ago I found found this combination of commands to be most effective.

Ok using VBS I did get an error message 3018 if the printer had never been added before (if the driver was unrecognized), but after manually adding the driver once it seems to work well.

Wrapping things up I'm actually noticing the default printer isn't being set... I'd add a third command to SET the default printer..  here is my version of the entire script with two additional IF statements so I could feed it parameters to quickly use one network or another and to use different computer profiles that I was using to debug this...  You can work through it and see what other options might help you.

cscript %windir%\System32\prnmngr.vbs -?
rundll32 printui.dll,PrintUIEntry -?

will also help.  Other than that, I'd move onto logging.



@echo off

for /f "tokens=3 delims=.]" %%a in ('ver') do set WINVERS=%%a
if "%2" neq "" set WINVERS=%2
IF "%WINVERS%" equ "5600" set WINVERS=6000
IF "%WINVERS%" equ "6000" (
  rem VISTA
  set x_PrnDEL_cmd=cscript %windir%\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -d -p
  set x_PrnADD_cmd=cscript %windir%\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -ac -p
  set x_PrnSET_cmd=cscript %windir%\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -t -p
) else (
  rem 2003/WINXP
  set x_PrnDEL_cmd=cscript %windir%\System32\prnmngr.vbs -d -p
  set x_PrnADD_cmd=cscript %windir%\System32\prnmngr.vbs -ac -p
  set x_PrnSET_cmd=cscript %windir%\System32\prnmngr.vbs -t -p
)
if "%WINVERS%" equ "2000" (
  rem win2000 doesn't normally have prnmng.vbs (unless you copy it)
  set x_PrnDEL_cmd=rundll32 printui.dll,PrintUIEntry /q /dn /n
  set x_PrnADD_cmd=rundll32 printui.dll,PrintUIEntry /q /in /u /y /n
  set x_PrnSET_cmd=rundll32 printui.dll,PrintUIEntry /q /y /n
)

set WINOS=Unknown OS
if "%WINVERS%" equ "2195" set WINOS=Windows 2000
if "%WINVERS%" equ "3790" set WINOS=Windows 2003
if "%WINVERS%" equ "2600" set WINOS=Windows XP
if "%WINVERS%" equ "6000" set WINOS=Windows Vista
echo OS: %WINOS%


for /f "tokens=9,10,11 delims=. " %%a in ('route print 0.0.0.0 ^|find "0.0.0.0"') do (
set SUBNET=%%a.%%b.%%c
)

if "%1" neq "" goto subnet%1

if "%subnet%"=="192.168.10" goto subnet1
if "%subnet%"=="10.2.1" goto subnet2

echo On another subnet (%subnet%)
goto next

:subnet1
 echo Running on subnet 1 (%subnet%)
 %x_PrnDEL_cmd% \\adserver\pSCopier.PS
 %x_PrnADD_cmd% \\adserver\pHP4250.PS
 %x_PrnSET_cmd% \\adserver\pHP4250.PS
goto next

:subnet2
 echo Running on subnet 2 (%subnet%)
 %x_PrnDEL_cmd% \\adserver\pHP4250.PS
 %x_PrnADD_cmd% \\adserver\pSCopier.PS
 %x_PrnSET_cmd% \\adserver\pSCopier.PS
goto next

:next
:end
ASKER CERTIFIED SOLUTION
Avatar of tymes
tymes
Flag of Canada image

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
Avatar of advmicro
advmicro

ASKER

As for the prnmngr.vbs  do I need to program anything myself.

btw, Thank you so much!
nevermind, I just noticed that it is in the system32 dir
Again, thank you. Now I just need to figure out how to add it as default.
yup, prnmngr.vbs is on xp 2003 and vista.  You can even copy it to 2000 machines and use it there.
There are other vbs admin scripts for printers so I would use prndrvr.vbs to install the driver on vista machines or other machines to avoid the problem the first time if the driver didn't exist, but again this is easy to avoid by adding the manually printer once.

either
  cscript %windir%\System32\prnmngr.vbs -t -p \\server\printer
  rundll32 printui.dll,PrintUIEntry /q /y /n \\servername\printer
should make em default, I found using three different commands works while trying to add and make it default doesn't with the same command doesn't... although I think it might/should, but I didn't want to spend too much time on this so just used 3 separate del add set commands.
For some reason it wont delete the printer. Nor will it add it as defualt.

i am getting the error
Unable to delete printer <connection> \\lewiston-server\HP4200 Error 0x80041002
Not Found
Operation GetObject
Provider CIMWin32

here is my current script


@echo off

for /f "tokens=3 delims=.]" %%a in ('ver') do set WINVERS=%%a
if "%2" neq "" set WINVERS=%2
IF "%WINVERS%" equ "5600" set WINVERS=6000
IF "%WINVERS%" equ "6000" (
  rem VISTA
  set x_PrnDEL_cmd=cscript %windir%\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -d -p
  set x_PrnADD_cmd=cscript %windir%\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -ac -p
  set x_PrnSET_cmd=cscript %windir%\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -t -p
) else (
  rem 2003/WINXP
  set x_PrnDEL_cmd=cscript %windir%\System32\prnmngr.vbs -d -p
  set x_PrnADD_cmd=cscript %windir%\System32\prnmngr.vbs -ac -p
  set x_PrnSET_cmd=cscript %windir%\System32\prnmngr.vbs -t -p
  set x_Prndef_cmd=cscript %windir%\System32\prnmngr.vbs -t -p \\lewiston-server\HP4200
  set x_Prndefsaco_cmd=cscript %windir%\System32\prnmngr.vbs -t -p \\saco\HP4200
)
if "%WINVERS%" equ "2000" (
  rem win2000 doesn't normally have prnmng.vbs (unless you copy it)
  set x_PrnDEL_cmd=rundll32 printui.dll,PrintUIEntry /q /dn /n
  set x_PrnADD_cmd=rundll32 printui.dll,PrintUIEntry /q /in /u /y /n
  set x_PrnSET_cmd=rundll32 printui.dll,PrintUIEntry /q /y /n
  set x_Prndef_cmd=rundll32 printui.dll,PrintUIEntry /q /y /n \\lewiston-server\HP4200
  set x_Prndefsaco_cmd=rundll32 printui.dll,PrintUIEntry /q /y /n \\saco\HP4200
)

set WINOS=Unknown OS
if "%WINVERS%" equ "2195" set WINOS=Windows 2000
if "%WINVERS%" equ "3790" set WINOS=Windows 2003
if "%WINVERS%" equ "2600" set WINOS=Windows XP
if "%WINVERS%" equ "6000" set WINOS=Windows Vista
echo OS: %WINOS%


for /f "tokens=9,10,11 delims=. " %%a in ('route print 0.0.0.0 ^|find "0.0.0.0"') do (
set SUBNET=%%a.%%b.%%c
)

if "%1" neq "" goto subnet%1

if "%subnet%"=="192.168.1" goto subnet1
if "%subnet%"=="192.168.2" goto subnet2

echo On another subnet (%subnet%)
goto next

:subnet1
 echo Running on subnet 1 (%subnet%)
 %x_PrnDEL_cmd% \\saco\HP4200
 %x_PrnADD_cmd% \\lewiston-server\HP4200
 %x_PrnSET_cmd% \\lewiston-server\HP4200
 %x_Prndef_cmd% \\lewiston-server\HP4200
goto next

:subnet2
 echo Running on subnet 2 (%subnet%)
 %x_PrnDEL_cmd% \\lewiston-server\HP4200
 %x_PrnADD_cmd% \\saco\HP4200
 %x_PrnSET_cmd% \\saco\HP4200
 %x_Prndefsaco_cmd% \\saco\HP4200
goto next

:next
:end

Thanks so much...
Im at the location right now working on this, so if you see this anytime soon that would be awesome.
I also got the error:
c:\WINDOWS\system32\prnmngr.vbs<704, 9) SWbemServicesEx: Not Found
nvm.. I finally figured it all out. Thanks for all of the help!!!!
The deleting printer error is normal if the printer isn't there (like if you're logging into the same subnet as the previous time).   You can hide the error if it freaks you or someone else out with this...
 %x_PrnDEL_cmd% \\saco\HP4200                             2>null:
as you may get the error most of the time.

The commands
  set x_PrnSET_cmd=
  set x_Prndef_cmd=
  set x_Prndefsaco_cmd=
all do the same thing you can reduce them to one... you may have just been doing this for debug purposes.  you should also remove the servername\printername references in the definitions as they are specified later as parameters... Actually!  this is probably what you were doing wrong -- you ended up specifing the names twice in the commands which would result in an error.

'TM