Link to home
Start Free TrialLog in
Avatar of chicabow
chicabow

asked on

Login script for printers

Can I add a script to log, perhaps a policy or something, that maps all the users who log in to the printers in the office. Instead of manually going to each computer and mapping the computers, it would be nice to have a policy that does this automatically. Can this be done, if so where in the policy is this controlled.
Thanks.

Avatar of Fatal_Exception
Fatal_Exception
Flag of United States of America image

So you want a script that maps the shared printer..??   Try the Net Use command in a batch file..

http://www.computerhope.com/nethlp.htm#01
Avatar of chicabow
chicabow

ASKER

yes I want the user to just log in the morning and all the available printers are listed for them, printer name change and are avialable at times and sometimes not...instead of editing every user, I want this controlled via server somehow.
servername is cheffy and printer is hplaser.
so whats the syntax?
net use LPT1: \\servername\printername /yes
will test this soon,, now will I be able to "wipe" out all the printer references the current users have, then reassign using this script mentioned above?
The reason for this is because I want to rename the printers currently in the network but don't want to cause any issues or hickups.
You should be okay with this...  TEST IT FIRST though..  :)
Its not working for me....I tried many different variations, with quote, no quotes....still nothing is working....is there a way to log what happens since we can't actually see the script run?
If you are using it in a batch file, test the batch file by itself first...  Add the line 'pause' and it will keep the window open..  
I tried that and I got this error
System error 85 has occured
The local device name is already in use.

But it's not in use....so i'm at a loss here...
try LPT2  

Let me know
Still no go.... :(
Here is a MS article on mapping printers with DOS commands..

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q314499
ASKER CERTIFIED SOLUTION
Avatar of JamesDS
JamesDS

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
James is right with his suggestion, I would definitely avoid using dos batch commands and mapping LPT ports.
If you use his commands in a VB script you can assign this script to a group policy for an OU.  Therefore say you had an OU for each department at your company you would have a different GP on each OU assigning different printers to the users.  This means that if a user moved departments it would be a simple matter of dragging them into the new OU and they would get a different set of printers next time they logged on.
Wow that works like a charm JamesDS, well I just created the file and double clicked and it worked....I have not assigned it to the GPOs yet. So need the exact steps of how to attach a vbs to a Group Policy, never used vbs before....
Would also like to enhance this script to remove all instances of printer currently listed then re-add the listed ones. Would like all clients to have a clean list of valid printers.
Please advise.
Yea..  those scripts are wonderful.. and I will also be copying it for future reference...  :)

Here is a link to logon scripts that may help you with your GPO's

http://www.tycho7.com/Software/Win2k/logon_scripts.htm
I don't see anything about associating with VBS files, I already have each user with a specific bat file, but now I need to add vbs files as well how can this be done using the GPO?
And the question about removing the printers..
The same way you do your batch files...  Scripts are placed in winnt\sysvol\domain\scripts.  And you use the logon script policy to point your GPO to that script...
ok....looking for direction on using (where and how) the logon script policy.
SOLUTION
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
ThankQ

FE
Setting a policy for printer mapping for an OU isn't always what you want to do though. Where I work, in a school, the users have roaming profiles and will log onto many different machines. They will always want to have the nearest printers available to them and other printers throughout the organisation invisible.

The obvious answer would be to assign the policy to an OU containing computers, but, if I put them there (in the Computer startup/shutdown policy) the scripts don't appear to run.
(Anyone any idea why?)

So, I copy the script to the local hard drive and stick a shortcut to it in the startup folder. To ensure that as users move around they don't accumulate printers all over the site the script does the following:
Enumerates all connected printers.
Deletes all connected printers.
Connects only those printers which are nearby.

Works for me, but I'd much rather be able to assign the script to OUs of workstations.
To give my two cents worth.... I use Kix32 in your environment for login script processing.
KIX is a free utility that can be used for many type of scenarios... printers included. Obviously I can't detail them all but here an example of a KIX script I use to automatically add and remove printers based on the user NT group.

This command addes printers if the use belongs to the DEPT_IS nt group

If ingroup("DEPT_IS")

            $ReturnCode = KeyExist("HKEY_CURRENT_USER\Printers\Connections\,,aumelsvr011,AUMELISD1DL")
            If $ReturnCode = 0
               ? "Printer doesn't exist....adding"
                  If addPrinterConnection ("\\aumelsvr011\AUMELISD1DL") = 0
                           ? "Added printer ISD Lexmark T622....AUMELISD1DL"
                  Endif
            Endif
Endif


This command deletes printers if the reg key for the specific printer is found.

$ReturnCode = KeyExist("HKEY_CURRENT_USER\Printers\Connections\,,aumelsvr011,AUMELBOG1DP")
            If $ReturnCode
               ? "Printer doesn't exist....adding"
                  If delPrinterConnection ("\\aumelsvr011\AUMELBOG1DP") = 0
                           ? "Removed printer Business Operations Hewlett Packard 4050....AUMELBOG1DP"
                  Endif
            Endif

I hope this is helpful for some. KIX can be found at www.kixtart.org

Regards

HoPPeR
Additionally this code sets an existing printer as the default:

Option Explicit
Dim objPrinter
Set objPrinter = CreateObject("WScript.Network")
objPrinter.SetDefaultPrinter "\\server\printer"
' End of Script


Useful to set the default printer for different groups...
I also work in a school. The way I handled the login script problem was to name the computers in an organized fashion that would allow my login script to assign printers based on the name of the computer that the user logs on to. Therefore, if a student logs into computer LAB10, his default printer will be "Printer4". I created a separate script for the teachers, because I don't want students accessing the faulty printers.

-------------------------------------------------------------------------
Dim objNetwork
Dim objPrinters
Dim Printer1, Printer2, Printer3, Printer4

on error resume next

Set objNetwork = CreateObject("WScript.Network")
Set objPrinters = objNetwork.EnumPrinterConnections

objNetwork.AddWindowsPrinterConnection Printer1
objNetwork.AddWindowsPrinterConnection Printer2
objNetwork.AddWindowsPrinterConnection Printer3
objNetwork.AddWindowsPrinterConnection Printer4

Select Case left(objNetwork.ComputerName,3)

      Case "100"
            objNetwork.SetDefaultPrinter Printer1

      Case "200"
            objNetwork.SetDefaultPrinter Printer2

      Case "300"
            objNetwork.SetDefaultPrinter Printer3

      Case "LAB"
            objNetwork.SetDefaultPrinter Printer4      

      Case Else

End Select

-------------------------------------------------------------------

You can also add code to check for a local printer and make that the default in lieu of the closest workgroup printer. This code is much more complicated and I had to account for a terminal server login as well. I hope this helps.

--------------------------------------------------------------------

Set objPrinters = WshNetwork.EnumPrinterConnections

for i=0 to objPrinters.Count-1 Step 2
            
      if Left(objPrinters(i),4)="LPT1" OR Left(objPrinters(i),4)="USB1" Then

            if left(objPrinters(i + 1),3)="Acr" then
                  'don't set acrobat printers as default
            else
                  WshNetwork.SetDefaultPrinter objPrinters(i + 1)
                  defaultPrinter = objPrinters(i+1)
                  defaultPrinterAssigned = true
                  localPrinter=true
            end if

      elseif Left(objPrinters(i),2)="TS" AND DefaultPrinterAssigned = false then
                  PrinterDesc = split(objPrinters(i+1)," ")

                  count = 0
                  for each token in PrinterDesc
                        if token="session" then      
                              sessionnumber = PrinterDesc(count + 1)
                        end if
                        count = count + 1                              
                  next
                  if TermSessionID = sessionnumber then
                        WshNetwork.SetDefaultPrinter objPrinters(i + 1)
                        defaultPrinter = objPrinters(i+1)
                        defaultPrinterAssigned = true
                        TerminalSessionPrinter = true
                  else
                        defaultPrinterAssigned = false
                  end if
      End If

Next


This works for me in MANY environments.

rundll32 printui.dll,PrintUIEntry /in /n \\%servername%\%printername%

Run this in the run line to get all variables.

rundll32 printui.dll,PrintUIEntry /?

Can you dig?

-chumpmasterg

How about connecting to a printer server i.e. jetdirect via an IP port or FQDN? Any Ideas?
I've got a 300 User network that we are configuring from the ground up and during the first few sites that we made live we noticed that we are going to have to script the printer installs this is going to be far too much work for us to add them to every machine and then readd them when a second user logged into the machine.

I have the same question as Brianpmoris, what about TCP/IP Printer Ports on the clients? Our Client wants 2 mappings for each printer. One for using the windows 2003 print server in the AD (\\server\prt-01) and another map to a local TCP printer port (IP_10.0.0.22).

The reason is if they lost access to the domain / print server they could still print off of the local static mapped ones. First off if anyone has any suggestions on a better way to do this I'm all ears...

Second off I need a script to be able to make this happen. I've read about 10 different ways to map a printer via a script or command line. I really would like to know what the best and easiest way to make this happen is. All of my servers are Server 2003 and all of the workstations are Windows XP Pro.






dacomb...  you need to ask your question in a new thread if you really want to attract attention to your question...  You might even want to place it in the scripting section too, along with in the W2K3 section..

FE
Check out this site: http://www.robvanderwoude.com/index.html - search for command line printer control

I wrote the following batch file based on info found at this site and others to do exactly what you want to do.

There are commands to map to a particular port -either local or by IP - see above site

@ECHO OFF
rem VER | FIND "Windows 2000" >NUL
rem IF ERRORLEVEL 1 GOTO Not2000

SETLOCAL

SET Printer=%1


:: Check printer specified
IF NOT DEFINED Printer GOTO NoPrinter
IF NOT "%Printer:~0,2%"=="\\" GOTO MissingBS
FOR /F "tokens=1* delims=\" %%A IN ('ECHO.%Printer%') DO (
      SET PrintServer=%%A
      Set PrintShare=%%B
)

rem echo "Line 12"

IF NOT DEFINED PrintShare GOTO NotDefinedShare
PING %PrintServer% -n 1 -w 1000 2>&1 | FIND "TTL=" >NUL
IF ERRORLEVEL 1 GOTO Syntax
NET VIEW \\%PrintServer% 2>&1 | FIND /I "%PrintShare%" >NUL
IF ERRORLEVEL 1 GOTO Syntax

rem echo "Line 30"


rem echo "Line 40"

:: Install the printer on the specified computer
echo
ECHO About to INstall the Printer: %Printer%
echo
rem Most of this was taken from http://www.robvanderwoude.com/index.html - Command Line Printer Control
rem This line give the printers property
RUNDLL32 PRINTUI.DLL,PrintUIEntry /p /n\\machine\printer

rem
rem If ths property of the printer is available then it is installed already so don't re-add
rem This line deletes printers - gd
rem RUNDLL32.EXE PRINTUI.DLL, PrintUIEntry /gd /n%Printer% %RemotePC%
rem
rem This line ADDs printers - ga
IF NOT ERRORLEVEL 1 RUNDLL32.EXE PRINTUI.DLL, PrintUIEntry /ga /n%Printer% %RemotePC%


net stop spooler
net start spooler
IF NOT ERRORLEVEL 1 ECHO Printer: %printer% installed

ENDLOCAL
GOTO:EOF

:NOPrinter
echo
echo Error - Printer not defined: %Printer%
echo
goto Syntax


:MissingBS
echo
echo Error - Missing BackSlashes : %Printer%
echo
goto Syntax

:NotDefinedShare
echo
echo Error - NotDefinedShare : %Printer%
echo
goto Syntax

:Not2000
echo
echo Error - Not 2000 or XP
echo VER | FIND "Windows 2000" >NUL
echo
goto Syntax

:Syntax
ECHO.
ECHO AddPrint.bat,  Version 1.00 for Windows 2000
ECHO Install a network printer on any PC on the network
ECHO.
ECHO Usage:  ADDPRINT  \\print_server\printer_share
ECHO.
ECHO Where:              "print_server"   is the print server name or IP address
ECHO                     "printer_share"  is the share name for the printer

ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com

IF "%OS%"=="Windows_NT" ENDLOCAL
I use VBscripting for mapping printers.
I have the same problem as you have, the users needs a list of printers to select from.
VBScript lacks forms, this can be fixed by using wshDialog (GNU general public license, version 2).
First the list is shown for the selection of a default printer and the aging for selection for additional printers.
You need to change the domain name for the SQL statement (The domain is “domain.local” right know). All printer need to be listed in the AD.
The script follows:

Option Explicit

Const vbModal = 1
Const ADS_SCOPE_SUBTREE = 2

Dim oDlg, oReplicatorForm, oPrinterForm, sBtn, objConnection, objCommand, objRecordSet, x, i, oNet, oShell, oPrinters

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "SELECT printerName,serverName,location,UncName FROM " _    
    & " 'LDAP://DC=domain,DC=local'  WHERE objectClass='printQueue' ORDER BY printerName"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = True
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

x=1
i=0
Set oNet = CreateObject("WScript.Network")
Set oShell = WScript.CreateObject("WScript.Shell")

Set oDlg = Wscript.CreateObject("WshDialog.Kit")
Set oReplicatorForm = BuildReplicatorForm(objRecordSet)
objRecordSet.MoveFirst
Set oPrinterForm = BuildPrinterForm(objRecordSet)
objRecordSet.MoveFirst

oReplicatorForm.Show vbModal

Select Case Ucase(oDlg.Clicked)
Case "1"
    sBtn = oReplicatorForm.GetOptionButton("FRM1")

      If oReplicatorForm.Ctl("Remove").Value Then
            Set oPrinters = oNet.EnumPrinterConnections
            For i = 1 to oPrinters.Count - 1 Step 2
                     If Left(oPrinters.Item(i),2)="\\" Then
                           oNet.RemovePrinterConnection oPrinters.Item(i)
                     End If
            Next
      End If
      
      oNet.AddWindowsPrinterConnection sBtn
      oNet.SetDefaultPrinter sBtn
      
      oPrinterForm.Show vbModal
      
      Select Case Ucase(oDlg.Clicked)
      Case "3"
          Do while x=<objRecordSet.RecordCount
                If oPrinterForm.Ctl("Box"&x).Value Then
                      oNet.AddWindowsPrinterConnection objRecordSet.Fields("UncName").Value
                      objRecordSet.MoveNext
                End If
                x=x+1
          Loop
          MsgBox "Printer installationen er ferdigt"
      Case Else
          MsgBox "Du har kun installeret en default printer"
      End Select

Case Else
    MsgBox "Du har annulleret instalationen du har ikke adgang til printerene"
End Select

oReplicatorForm.Clear
oPrinterForm.Clear
Wscript.DisconnectObject oDlg
Set oDlg = Nothing


Function BuildReplicatorForm(objRecordSet1)

    Dim oFrm1, BtnPlaceHigh, BtnPlaceLeft, Rows, oCtl, x

    Set oFrm1 = oDlg.NewForm
      BtnPlaceHigh=300
      BtnPlaceLeft=150
      Rows=(objRecordSet1.RecordCount-(objRecordSet1.RecordCount mod 15))/15+1
      x=0


    oFrm1.NewButton "1", 450, 7600, 750, 375, "&Next"
    oFrm1.NewButton "Cancel", 1400, 7600, 750, 375, "&Cancel"
    oFrm1.Ctl("Cancel").Cancel = True

      Set oCtl = oFrm1.NewCheckBox("Remove", 450, 7100, 1800, 375, "Remove old printers",1, "FRM1")
      oCtl.ToolTipText = "Fjern gamle printere"

    oFrm1.NewImage "IMG1", 250, 200 , 0, 0, "IconQuestion"
    oFrm1.NewLabel "LBL1", 1150, 300, 3000, 375, "Printer valg"
    oFrm1.NewFrame "FRM1", 400, 700, Rows*4500, 6300, " Vælg Default Printer "
      Set oCtl = oFrm1.NewLabel("LBL2", (Rows*4500)-1450, 7500, 1800, 200, "By Sune Stevnsborg")
      oCtl.ForeColor = RGB(66, 66, 66)
      Set oCtl = oFrm1.NewLabel("LBL3", (Rows*4500)-1450, 7710, 1800, 200, "For Leif Hansen Gruppen")
    oCtl.ForeColor = RGB(66,66, 66)
    Do Until objRecordSet1.EOF
      Set oCtl = oFrm1.NewOptionButton(objRecordSet1.Fields("UncName").Value, BtnPlaceLeft, BtnPlaceHigh, 4300, 375, objRecordSet1.Fields("printerName").Value,False, "FRM1")
      oCtl.ToolTipText = "Vælg Default Printer"
    objRecordSet1.MoveNext
      BtnPlaceHigh=BtnPlaceHigh+400
      x=x+1
      If x=15      Then
            BtnPlaceLeft=BtnPlaceLeft+4350
            BtnPlaceHigh=300
            X=0
      End If
    Loop
    oFrm1.Caption = "Printer Installation"
    oFrm1.TaskBar = False
    oFrm1.AutoSize
    Set BuildReplicatorForm = oFrm1
End Function


Function BuildPrinterForm(objRecordSet2)

    Dim oFrm2, BtnNr, BtnPlaceHigh, BtnPlaceLeft, Rows, oCtl, x

    Set oFrm2 = oDlg.NewForm("2")
      BtnNr=1
      BtnPlaceHigh=300
      BtnPlaceLeft=150
      Rows=(objRecordSet2.RecordCount-(objRecordSet2.RecordCount mod 15))/15+1
      x=0

    oFrm2.NewButton "3", 450, 7100, 750, 375, "&Done"
    oFrm2.NewButton "Cancel", 1400, 7100, 750, 375, "&Cancel"
    oFrm2.Ctl("Cancel").Cancel = True

    oFrm2.NewImage "IMG1", 250, 200 , 0, 0, "IconQuestion"
    oFrm2.NewLabel "LBL1", 1150, 300, 3000, 375, "Printer valg"
    oFrm2.NewFrame "FRM2", 400, 700, Rows*4500, 6300, " Vælg yderlgere printere "
      Set oCtl = oFrm2.NewLabel("LBL2", (Rows*4500)-1450, 7100, 1800, 200, "By Sune Stevnsborg")
      oCtl.ForeColor = RGB(66, 66, 66)
      Set oCtl = oFrm2.NewLabel("LBL3", (Rows*4500)-1450, 7310, 1800, 200, "For Leif Hansen Gruppen")
    oCtl.ForeColor = RGB(66,66, 66)
   

    Do Until objRecordSet2.EOF
      Set oCtl = oFrm2.NewCheckBox("Box"&BtnNr, BtnPlaceLeft, BtnPlaceHigh, 4300, 375, objRecordSet2.Fields("printerName").Value,, "FRM2")
      oCtl.ToolTipText = "Vælg yderlgere printere"
         objRecordSet2.MoveNext
         BtnNr=BtnNr+1
      BtnPlaceHigh=BtnPlaceHigh+400
      x=x+1
      If x=15      Then
            BtnPlaceLeft=BtnPlaceLeft+4350
            BtnPlaceHigh=300
            X=0
      End If
    Loop

    oFrm2.Caption = "Printer Installation"
    oFrm2.TaskBar = False
    oFrm2.AutoSize
    Set BuildPrinterForm = oFrm2
End Function
I am no script kiddy so please don’t flame me for my appalling script. On windows XP there are built in vbs scripts that will allow you to create IP printer ports and manage printer objects.

cscript c:\windows\system32\prnport.vbs -a -o raw -r IP_192.168.0.10 -h 192.168.0.10
cscript c:\windows\system32\prnmngr.vbs -a -p "HPLJ2300" -m "HP Laserjet 2300 Series PCL 5e" -r "IP_192.168.0.10"

I placed both of the above lines in a bacth file and it did the job just fine. I am sure a better script can be created as my coding skills are very lame.
Here is a simpler solution I found on the web recently

I created a script and launched it from the login script (login.bat)

Dim net
Set net = CreateObject("WScript.Network")
net.AddWindowsPrinterConnection "\\dbsinhouse3\Canon3100FAX"
net.AddWindowsPrinterConnection "\\dbsinhouse3\Canon3100PCL"
net.AddWindowsPrinterConnection "\\dbsinhouse3\CanoniRC"
net.AddWindowsPrinterConnection "\\dbsinhouse3\LexmarkO"
net.AddWindowsPrinterConnection "\\dbsinhouse3\Tek850"

Work wonderful and is so simple.

That looks good, is there one for Network Places as well?
I have a bunch of network places I want added is there a script for it?
Can you explain network places? I don't understand the question you presented.
Thanks
I use con2prt, it is easy to use, but i need to connect printers to computers.

eks. all computers in room A shoud have printer A as default printer, i have uset logon script, it does not work.

This connect all printers, but Xerox will always be default.
\\Bilbo\NETLOGON\con2prt /c \\bilbo\Xerox
\\Bilbo\NETLOGON\con2prt /c \\bilbo\EDB1
\\Bilbo\NETLOGON\con2prt /c \\bilbo\Lille-M
\\Bilbo\NETLOGON\con2prt /c \\bilbo\SOL
\\Bilbo\NETLOGON\con2prt /c \\bilbo\Mediatek

Any ide?
Avatar of Ron Malmstead
I prefer the IFMEMBER method...once setup properly...it's easy to manage through AD which printers a user is assigned.
http://www.microsoft.com/technet/archive/winntas/tips/winntmag/grlogon.mspx?mfr=true
First make all of your printers shared on a print server...

Example logon script.....too easy.

ifmember "accounting users"
if not errorlevel 1 goto EXIT
cscript prnmngr.vbs -x
cscript prnmngr.vbs -ac -p "\\servname\printersharename"
:EXIT

i just deleted all printers and added \servername\printer provided the user is a member of "accounting users".