More on SBS login scripts

This post comes from an SBS user (thanks, TK!).


Because there have been various requests for login script help, I wanted to post something that may help those who aren’t so familiar with writing a batch file or login script.

DISCLAIMER: I’m not an instructor, I haven’t written any books, I don’t write code for a living, and there may be other ways to accomplish the same thing as I have written.  This is intended to help others so please keep your opinions to yourself unless you have something constructive to add that will benefit others.  Additionally, this example script is intended for tutorial purposes and will need ‘tweaked’ to suit your environment.  I take no responsibility for the use of this script in a production environment.

NOTES: There are tremendous amounts of information on the Internet and in books on how to write scripts, what good practices are, and what variables can be used in a login script (batch file).  This should be enough to get  the average person going in short order.  If anything is unclear, or if you have questions, please post them.  Remember, you will need to modify this script for your server name, network share names, drive letters you require, printer names, etc.

Okay, with BS out of the way, let’s get going. 


First, you can find your default login script by clicking Start – Run, type \\ServerName\netlogon, click OK.  This will open a folder with a batch file called SBS_LOGIN_SCRIPT.bat.  Using Notepad, WordPad, or your favorite text  editor open this file (note: you should be able to right-click the file and select edit from the context menu).  This file generally includes only one line used for SBS client setups.  Do not remove this line!  It can be moved around within the script, but leave it intact.  If you don’t already know it, this line will tell you the name of your server.

Now, following will be a clean version of my sample login script followed by a sample with notes.  My notes will start with the “Note:” to help you distinguish the difference.  If this helps even one person, it was worth the time.  If you have further questions, please post them for all to learn from. Enjoy!
-TK  M/T Box Computers

<CLEAN LOGIN SCRIPT>
@echo off
rem   ==================================================
rem
rem   Title: Login Script
rem   Author: Your Name
rem   Date: Self-explanatory
rem   Description: Network Login Script
rem
rem ==================================================

:SBS_SETUP
rem Default sbs2k3 client setup
\\ServerName\Clients\Setup\setup.exe /s ServerName

:MAPDRIVES
rem Connect network drives
if exist f:\*.* net use f: /d
net use f: \\ServerName\ShareName /persistent:no
if exist g:\*.* net use g: /d
net use g: \\ServerName\applications /persistent:no
if exist h:\*.* net use h: /d
net use h: \\ServerName\%username% /persistent:no

:PRINTERS
rem Connect network printers
net use lpt1: \\ServerName\Printer1ShareName /persistent:no
net use lpt2: \\ServerName\Printer2ShareName /persistent:no

:END
</CLEAN LOGIN SCRIPT>


<COMMENTED LOGIN SCRIPT>
Note: The word ‘REM’ is a way to add a remark to your batch file (login script).  It is a good idea to use remarks throughout your login script. This will help later when troubleshooting why the login script is written the way it is.  You should also document dates of changes and why.

Note: The command ‘ECHO’ can be used to turn display on and off.  ‘ECHO’ followed by words will display those words on the screen.  ‘ECHO OFF’ will suppress all display until ‘ECHO ON’ is issued.


Note: The @ sign in front of ‘ECHO OFF’ says to not display this line also.

@echo off
rem   ==================================================
rem
rem   Title: Login Script
rem   Author: Your Name
rem   Date: Self-explanatory
rem   Description: Network Login Script
rem
rem ==================================================

Note: The use of the colon ‘:’ followed immediately by a word designates the following lines as a section or routine within your script.  This allows you to move back and forth within the login script.  This isn’t normally necessary, but I found it helpful to form good script writing habits early on.  A batch file will normally flow from top to bottom 
executing every line it comes to that is not a remark line, and that is an actual command.  You can skip sections by using the ‘GOTO’ command.  Often times I will check for the existence of the drive mappings after they should have completed.  If they do not exist I will send the user to an error message section letting them know something failed, with instructions to reboot and/or contact their IT support.  If you would like an example of this also, let me know and I will post it.

:SBS_SETUP
rem Default sbs2k3 client setup
Note: Leave this line intact somewhere near the head of your script.  As 
you see here, this is the first actual line that executes in this script.
\\ServerName\Clients\Setup\setup.exe /s ServerName>

:MAPDRIVES
rem Connect network drives
Note: Tests for and deletes drive mapping if it exists (ensures drive letter isn’t erroneously mapped elsewhere).
if exist f:\*.* net use f: /d
> Note: Correctly maps drive letter to network share.
> Note: Be sure to modify for your correct server name.
net use f: \\ServerName\ShareName /persistent:no
if exist g:\*.* net use g: /d
net use g: \\ServerName\applications /persistent:no
if exist h:\*.* net use h: /d

Note: I have used the variable ‘UserName’ below.  The system will read this as the login name of the user.  If you setup a user share with each user’s login name, this line will correctly map a home drive for each user based on their login name.  Don’t forget to set security and permissions on the user shares you setup.
net use h: \\ServerName\%username% /persistent:no

:PRINTERS
rem Connect network printers
Note: This will map your network printers to LPT ports.  This is normally only necessary for older legacy (read: DOS) programs.
Note: Be sure to modify for your correct server name.
net use lpt1: \\ServerName\Printer1ShareName /persistent:no
net use lpt2: \\ServerName\Printer2ShareName /persistent:no

:END
</COMMENTED LOGIN SCRIPT>

7 thoughts on “More on SBS login scripts

  1. That great information thanks – trying to get my head round scripts.

    How would I deploy a folder to all users desktop?

    (this contain a couple of files that would need to be copied with the folder)

  2. Assuming the use of NT or higher, the following should work:

    xcopy <source> "%ALLUSERSPROFILE%\desktop\<foldername>" /s /v

    Example of <source>:

    \\servername\ITAdmin\AllUsersFolder\*.*

    Also, for and command line program, Use:

    xcopy /?

    for the command line switches. i.e use /s for all sub directories and /v for verify.

    Type

    set

    at the command line for basic environment variables. That’s where the ALLUSERSPROFILE environment variable came from.

    Paul.

  3. Help I Setup one server windows server 2003
    and I will like make login scripts for user
    But went i on the Start – Run, type \\ServerName\netlogon, click OK. This will open a folder with a batch file called SBS_LOGIN_SCRIPT.bat There is not folder
    called sbs_login_Script.bat

    Help Me My Email:h.nova@att.net

  4. Can you force a connection to a networked printer through login script that has an IP but you do not want to share off the server?

  5. I just deployed SBS2003 and am messing around with the logon scripts. It’s been several years since I’ve edited scripts, so bare with me as I regain my bearings. I’m wondering about the \\servername\clients\setup\setup.exe /s servername entry. This particular line is giving my Vista machines grief as it constantly pops a security warning. Can I do something like write a .txt file and use an ifexist statment to skip it if it’s been run already? Does it have to run everytime somebody logs in? Is there a switch I can use to have it run as administrator or something? Let me know, and thanks in advance.

    KM

  6. I follw the scripting but how does the script execute?? Where do you place it?? What if anything else do I have to do to get the script to run each time a user logs on??

Leave a Reply

Your email address will not be published. Required fields are marked *