Announcement

Collapse
No announcement yet.

Mapping Network Drives During Logon

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mapping Network Drives During Logon

    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=\\Server\DriveMapper
    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


    RIVE_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


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

    RIVE_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

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

  • #2
    Re: Mapping Network Drives During Logon

    Guy,

    Thank you for sharing. I'm sure some of us may be able to utilize your nifty batch file.

    Thanks,
    Andrew

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

    Comment


    • #3
      Re: Mapping Network Drives During Logon

      Hey all,
      I've just updated DriveMapper, lots of changes are done.

      Here are the important ones:
      * Bug fixes
      * support for long share paths (i.e. \\server\share\path to folder)
      * driver maps changed to non persistent
      * one main script for mapping (drivemapper.cmd) for logon
      * one main script for disconnecting drives (drivedisconnector.cmd) for logoff

      Bug reports are always welcome (-:
      Files at the bottom

      Thanks,
      Guy


      ---- DriveDisconnector.cmd ----
      @echo off

      SET ScriptHome= \\Server\Share\DriveMapper
      SET LogFile=%USERPROFILE%\DriveMapper.log
      %ScriptHome%\cmdow.exe @ /hid

      rem ************************
      rem Script: DisconnectMapper.cmd
      rem Creation Date: 30/05/2006
      rem Last Modified: 30/05/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 checksthat 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 ************************

      ECHO **** DisconnectMapper started >> "%LogFile%"
      ECHO **** Working on %ScriptHome%\Maps.txt >> "%LogFile%"
      FOR /F "tokens=1-3 skip=6 delims=," %%A in (%ScriptHome%\Maps.txt) DO (call %ScriptHome%\UnmapDrive.cmd %%A)


      ---- DriveMapper.cmd ----
      @echo off

      SET ScriptHome=\\SWoJup\DriveMapper
      SET LogFile=%USERPROFILE%\DriveMapper.log
      %ScriptHome%\cmdow.exe @ /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 ************************

      ECHO **** DriveMapper started > "%LogFile%"
      ECHO **** Working on %ScriptHome%\Maps.txt >> "%LogFile%"

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


      ---- MapDrive.cmd ----
      @echo off
      rem ************************
      rem Script: MapDrive.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.exe 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 reads from %ScriptPath%\Maps.txt
      rem and writes to a log file %USERPROFILE%\MapDrive.log
      rem Allfiles should be located at %ScriptHome%
      rem ************************

      ECHO **** Mapping %1: to %3 (%2) >> "%LogFile%"
      ECHO %SystemRoot%\System32\ping.exe -w 100 -n 1 %2 >> "%LogFile%"
      %SystemRoot%\System32\ping.exe -w 100 -n 1 %2 >> "%LogFile%"
      IF NOT %ERRORLEVEL%==0 GOTO SERVER_NOT _RESPONDING
      ECHO %SystemRoot%\System32\net.exe use %3 /persistent:no >> "%LogFile%"
      %SystemRoot%\System32\net.exe use %3 /persistent:no >> "%LogFile%"
      IF NOT %ERRORLEVEL% == 0 GOTO CONNECTION_FAILED
      ECHO DIR %1:\ >> "%LogFile%"
      DIR %1:\ >> "%LogFile%"
      IF %ERRORLEVEL%==0 GOTO DRIVE_LETTER_IN_USE

      :MAP _DRIVE
      ECHO %SystemRoot%\System32\net.exe use %1: %3 /persistent:no >> "%LogFile%"
      %SystemRoot%\System32\net.exe use %1: %3 /persistent:no >> "%LogFile%"
      IF NOT %ERRORLEVEL%==0 GOTO MAP_FAILED
      ECHO **** Mapped %1: to %3 successfully>> "%LogFile%"
      GOTO PROCEED

      RIVE_LETTER_IN_USE
      ECHO ***** Drive Letter %1:\ is in use, trying to disconnect it >> "%LogFile%"
      ECHO %SystemRoot%\System32\net.exe use %1: /D >> "%LogFile%"
      %SystemRoot%\System32\net.exe 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

      RIVE_LETTER_DISCONNECT_FAILED
      ECHO ****** Failed to disconnect DriveLetter %1:, maybe a local disk is mounted to it >> "%LogFile%"
      GOTO PROCEED

      RIVE_LETTER_SllLL_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%"
      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 s (Don't know why) >> "%LogFile%"
      GOTO PROCEED

      :PROCEED


      ---- Maps.txt ----
      # DriveMapper List of maps
      # The script skips the first 6 lines of this file (the 6th line is empty)
      #
      # Exmaple
      # DriveLetter,ServerName,SharePath

      X,Server,\\Server\c$
      Y,Server,\\Server\c$\Program Files


      ---- UnMapDrive.cmd ----
      @echo off
      rem ************************
      rem Script: UnmapDrive.cmd
      rem Creation Date: 30/05/2006
      rem Last Modified: 30/05/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.exe the drive, the script check if the host server
      rem is alive, and if the share is accessible
      rem Also, it checksthat the drive letter is not mount or already mapped,
      rem and if so it tries to disconnect it
      rem ************************
      rem Files: The script reads from %ScriptPath%\Maps.txt
      rem and writes to a log file %USERPROFILE%\MapDrive.log
      rem All files should be located at %ScriptHome%
      rem ************************
      ECHO **** Removing %1: from the registry >> "%LogFile%"
      ECHO %SystemRoot%\System32\reg.exe delete HKCU\Network\%1 /va /f >> "%LogFile%"
      %SystemRoot%\System32\reg.exe delete HKCU\Network\%1 /va /f

      ECHO **** Unmapping %1 >> "%LogFile%"
      ECHO %SystemRoot%\System32\net.exe use %1: /D >> ""%LogFile%"
      %SystemRoot%\System32\net.exe use %1: /D
      IF NOT %ERRORLEVEL%==0 GOTO UNMAP_FAILED
      GOTO PROCEED

      RIVE_LETTER_IN_USE
      ECHO ***** DriveLetter %1:\ is still in use, weird >> "%LogFile%"

      :UNMAP_FAILED
      ECHO ***** Cannot unmap %1 to %3 (Don't know why) >> "%LogFile%"
      GOTO PROCEED

      :PROCEED

      Comment

      Working...
      X