Mapping Network Drives During Logon

Home Forums Microsoft Networking and Management Services GPO Mapping Network Drives During Logon

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    swojup
    Member
    #110331

    Hi All!

    I wanted to share with you my work on this issue.
    I have a Windows 2000 domain controllers and Windows XP clients, and I wanted to give all the users in my domain a set of mapped network drives.

    I had a VBScript that mapped the drives for a very long time (few years actually), until recently it mapped incorrect share paths.
    Since the code was ugly I thought of re-designing the method for mapping instead of debugging it.

    The first thing that crossed my head is to write a GPO Template for the registry keys that “net use /persistent” use. I tested it out and it worked, but it had major disadvantages:
    1. If the server was down, it took along time till it moved to the next share
    2. The the share was inaccessible, it took a long time will it was figured out
    3. If the drive letter was already busy (mounted or mapped), there wasn’t any way of knowing it

    So I’ve decided to write a simple, yet sophisticated, batch script, that can deal with all of these issues.

    And here is what I came up with.
    There are two files, DriveMapper.cmd and MapDrive.cmd

    Have fun with it, and also please let me know what you thoughts about it.

    Yalla bye,
    Guy

    ========================================================


    @echo
    off
    cmdow @ /hid
    rem ************************
    rem Script: DriveMapper.cmd
    rem Creation Date: 19/02/2006
    rem Last Modified: 20/02/2006
    rem Author: Guy Rozendorn
    rem E-mail: [email protected]
    rem ************************
    rem Description: We had a problem with the previous map drive script,
    rem some of the maps were incorrect. So I’ve decided to write a new
    rem script
    rem Before actually mapping the drive, the script check if the host server
    rem is alive, and if the share is accessible
    rem Also, it checks that the drive letter is not mount or already mapped,
    rem and if so it tries to disconnect it
    rem ************************
    rem Files: The script read from %ScriptPath%Maps.txt
    rem and write to a log file %USERPROFILE%MapDrive.log
    rem It also calls to MapDrive.cmd
    rem All files should be located at %ScriptHome%
    rem ************************

    SET ScriptHome=\ServerDriveMapper
    SET LogFile=%USERPROFILE%DriveMapper.log

    ECHO **** DriveMapper started > “%LogFile%”
    ECHO **** Working on %ScriptHome%Maps.txt >> “%LogFile%”

    FOR /F “tokens=1-3 skip=6 ” %%A in (%ScriptHome%Maps.txt) DO (call %ScriptHome%MapDrive.cmd %%A %%B %%C)

    ========================================================


    @echo
    off
    rem ************************
    rem Script: DriveMapper.cmd
    rem Creation Date: 19/02/2006
    rem Last Modified: 20/02/2006
    rem Author: Guy Rozendorn
    rem E-mail: [email protected]
    rem ************************
    rem Description: We had a problem with the previous map drive script,
    rem some of the maps were incorrect. So I’ve decided to write a new
    rem script
    rem Before actually mapping the drive, the script check if the host server
    rem is alive, and if the share is accessible
    rem Also, it checks that the drive letter is not mount or already mapped,
    rem and if so it tries to disconnect it
    rem ************************
    rem Files: The script writes to a log file %USERPROFILE%MapDrive.log
    rem ************************

    ECHO **** Mapping %1: to %3 (%2) >> “%LogFile%”

    ECHO PING -w 100 -n 1 %2 >> “%LogFile%”
    PING -w 100 -n 1 %2 >> “%LogFile%”
    IF NOT %ERRORLEVEL%==0 GOTO SERVER_NOT_RESPONDING

    ECHO NET USE %3 >> “%LogFile%”
    ECHO %USERNAME% | NET USE %3 >> “%LogFile%”
    IF NOT %ERRORLEVEL%==0 GOTO CONNECTION_FAILED

    ECHO DIR %1: >> “%LogFile%”
    DIR %1: >> “%LogFile%”
    IF %ERRORLEVEL%==0 GOTO DRIVE_LETTER_IN_USE

    ECHO NET USE %1: %3 >> “%LogFile%”
    :MAP_DRIVE
    ECHO %USERNAME% | NET USE %1: %3 >> “%LogFile%”
    IF NOT %ERRORLEVEL%==0 GOTO MAP_FAILED

    ECHO **** Mapped %1: to %3 successfully >> “%LogFile%”
    GOTO PROCEED

    :DRIVE_LETTER_IN_USE
    ECHO ***** Drive Letter %1: is in use, trying to disconnect it >> “%LogFile%”
    ECHO NET USE %1: /D >> “%LogFile%”
    NET USE %1: /D >> “%LogFile%”
    IF NOT ERRORLEVEL==0 GOTO DRIVE_LETTER_DISCONNECT_FAILED
    ECHO DIR %1: >> “%LogFile%”
    DIR %1: >> “%LogFile%”
    IF %ERRORLEVEL%==0 GOTO DRIVE_LETTER_STILL_BUSY
    GOTO MAP_DRIVE

    :DRIVE_LETTER_DISCONNECT_FAILED
    ECHO ****** Failed to disconnect Drive Letter %1:, maybe a local disk is mounted to it >> “%LogFile%”
    GOTO PROCEED

    :DRIVE_LETTER_STILL_BUSY
    ECHO ****** The Drive Letter %1: is still accessible, even though it was disconnected successfully. weird shit >> “%LogFile%”
    GOTO PROCEED

    :SERVER_NOT_RESPONDING
    ECHO ****** The server %2 is not responding to ping >> “%LogFile%” >> “%LogFile%”
    GOTO PROCEED

    :CONNECTION_FAILED
    ECHO ****** Cannot connect to share %3, probably an invalid share path >> “%LogFile%”
    GOTO PROCEED

    :MAP_FAILED
    ECHO ****** Cannot map %1 to %3 (Don’t know why) >> “%LogFile%”
    GOTO PROCEED

    :PROCEED

    ========================================================

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.