Login Scripts
Location Detection

If you have a single domain spread over many sites it can be useful to be able to detect where the machine is and then connect it to the local printer or network shares. This is especially useful if you have roaming users who move between offices.

If you have a static environment and can control the naming convention then there is an alternative solution which may work better for you. More Information.

One method that works it to use the default gateway of the machine to detect where the machine is located. You must have different IP address ranges in each site for this to work correctly. Below is an example script with notes so that you can adjust how it for your own environment.

Note: Numbers in the notes correspond with the numbers down the side of the example script.

  1. For reference, the two subnet addresses being searched are contained at the top of the script. Useful when updating the original.
  2. The script carries out the command "ipconfig" and dumps the result in a text file in the user's profile directory. This also has the side effect of verifying the last time the login script was run correctly by the create date on the file.
  3. Short echo so the user knows something is going on.
  4. Searching for the first gateway address - the UK. If found it will be sent to the UK section further along the script. If it fails, it moves along to the next gateway. The "findstr" command is native file on Windows 2000 and XP.
  5. Now searching for the US gateway address. If this one fails, it goes to another place in the script, but it could easily search a forth gateway address.
  6. The sections called earlier. You could in here call separate scripts instead of commands, thus reducing the size of this initial script. 
    Remember to put in a "goto" command to skip the rest of the location based information. Otherwise the script will just continue moving down and will execute the next set of commands as well.
1. rem Subnets for reference
rem UK subnet: 192.168.22.254
rem US subnet: 192.168.11.254
2. rem Retrieve IP address and dump to a file
ipconfig >"%userprofile%\ipconfig.txt"
3. echo Checking for your location
4. rem Seaching for default gateway in IP dump above
findstr "192.168.22.254" "%userprofile%\ipconfig.txt"
if not errorlevel 1 goto UK
echo Not UK
goto next
5. :next
findstr "192.168.11.254" "%userprofile%\ipconfig.txt"
if not errorlevel 1 goto US
echo not US
goto other
6. :UK
rem UK settings
goto :next

:US
rem US settings
goto :next

:other
rem Other settings (for VPN etc)
goto :next

:next

rem more commands

:end


Complete Sample - Ready to Copy and Paste.

Remember to turn off word wrap when working in notepad


Top of the Page - Home Page
Last Page Update: 28/03/2015

Copyright Sembee Ltd. 1998 - 2024.

Reproduction of any content on this web site is prohibited without express written consent. Use of this web site is subject to our terms and conditions.

All trademarks and registered trademarks are property of their respective owners. This site is not endorsed or recommended by any company or organisation mentioned within and is to provide guidance only and as such we cannot be held responsible for any consequences of following the advice given.

Sembee Ltd. is registered in England and Wales at 1 Carnegie Road, Newbury, Berkshire, RG14 5DJ.
Registered company number: 4704428. VAT Number GB 904 5603 43.

Server 1