No announcement yet.

Proper Way to Map Drives?

  • Filter
  • Time
  • Show
Clear All
new posts

  • Proper Way to Map Drives?

    Hey, I ran into a small problem a few weeks back while moving user folders to a new server. I made the edit to their script for the new server. However, their old cached mapping took presidence over the new mapped script and all hell broke loose, when no one was able to access their folders. I had to edit the script to include the "net use x: /delete" command and have users log in again. This corrected the problem. I mapped the drives by the "net use X: \\server\share" comamnd.

    My question is this....should I include the "net use x: /delete" followed by the re-mapping of the drive on all of my scripts? Also, is there other edits I might consider adding to ensure smooth operation?
    MCITP:SA, MCSA 2003, MCP, CCNA, A+, Net+, Security+

  • #2
    Re: Proper Way to Map Drives?

    That's how we do it, the script deletes the old map, then sets up a new one.
    ** Remember to give credit where credit is due and leave reputation points where appropriate **


    • #3
      Re: Proper Way to Map Drives?

      We map a \\DFS-path instead of a \\server\share to a drive letter. In that case the scripts does not have to be modified, just create a new link for this dfs target.

      For your case;
      Consider using /persistent:no in your net use commands,to avoid the same issues in the future -

      Start/w Net.exe use X: /Delete /Yes
      Net.exe use X: \\newserver\share /Persistent:No
      You can delete all drive mappings with just one command/script
      start /w net use * /d /y >nul
      (the /Yes switch in combination with the /Delete switch is undocumented, it can be used to force the disconnection when drive is in use)

      <...deleted... explanation about the use of the /w switch here is not relevant>
      ((( - EDIT - read my next reply )))

      Last edited by Rems; 5th June 2008, 20:22.

      This posting is provided "AS IS" with no warranties, and confers no rights.


      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts


      • #4
        Re: Proper Way to Map Drives?

        We use a "net use x: /delete" in all of our logon scripts for drive mappings. This ensures that the drive mapping is current and not cached on the client.


        • #5
          Re: Proper Way to Map Drives?

          Review! !!

          My self I use a vbscript to map drives. For the deletion of a drive mapping I use something like;
          object.RemoveNetworkDrive(strDriveLetter1, [bForce], [bUpdateProfile])
          Where bForce and bUpdateProfile are optional Boolean values for "Persistent" and "Force" <- True or False.

          For a batch, you can delete the mapped drive with the switch "/D" - and you can force this action with an extra slash "/Y" [or "/Yes"].
          And you can control whether the new mapping must me Persistent (saved to the user's registry) or not.
          Like I explained in my previous reply.

          What is missing in the batch, is someting that will update the user's visible drive namespace for the drive.
          I know a there is this command-line:
          RunDLL32.exe user32.dll, UpdatePerUserSystemParameters (last parameter is case Sensitive!)

          I just tested the batch in my previous example and I discovered that the drive does unmap succesfully and remap the correct new path to the driveletter -=BUT=- in Windows explorer you still see the old name for the new mapped drive though.
          - I could not solve this by using the start/w command like I thought it would.
          - And neighter could it been solved by using the RunDLL32.exe user32.dll, UpdatePerUserSystemParameters command.

          Although the command Net Use shows you the new path mapped to the driveletter, and a subkey in user's MountPoints2 registrykey is correctly updated, the visible drive namespaces didn't follow and are not updated. The only thing that did solve this was a short break, needed between the unmapping and doing the remapping. (Or a re-logon of the user, but that would be stupid).

          Here is a new batch that has the little pause;
          @echo off
          :: v a r i a b l e s
          (Set $Drive=x:)
          (Set $UNC=\\newserver\share)
          (Set $wait=ping -w 1 -n 2 localhost ^>Nul)
          IF Exist "%$Drive%" (
            FOR /f "tokens=*" %%f IN (
            'net use ^|find /i "%$Drive%" ^|find /i "%$UNC%"') Do (
            Goto Mapped)
          ) ELSE Goto Map
          Net use %$Drive% /Delete /Yes >Nul &(
                   %$wait% )
          Net use %$Drive% "%$UNC%" /Persistent:No
          :Mapped -------------------------------------------Done
          :: c h e c k
          title check & COLOR 9E
          Dir %$Drive% 
          This batch is written in a way that it will pause only when it is necesseray, that is why I used the (combined commands).
          The Loop that is doing the checking for this, is slowing down the batch, but this is still faster then when doing; 1st unmap - 2nd pause - 3rd map at every logon.
          But when the 'Name' of the drive does not have to change, and you don't use persistent mappings, then you can skip the little pause, in that case check the previous replies.

          -=EDIT=- forget the batch above, here you can find a faster running variant:

          There are other ways to create a pause in a batch, but for this case, and for most cases, the command;
          " ping -w 1 -n 2 localhost >Nul " will do fine to provide a short break. You can experiment with the -n value.

          Ping.exe is native to Windows and it appears to have a very light footprint.
          But when a better controlled pause is needed,
          then you could do something like 'echoing' from within the batch two vbscript lines to a new tempfile; And call that file every time a managed pause is needed in the batch, see example;
          @echo off
          (Set WaitMiliSeconds=cscript.exe //NoLogo "%temp%.\pauseBatch.vbs")&&(
             echo.On Error Resume Next
             echo.Wscript.sleep wScript.Arguments^(0^)
          ::sample, pausing for about 5 seconds
          %WaitMiliSeconds% 5000
          ::optional  (added to the end of the batch) 
          DEL "%Temp%.\pauseBatch.vbs"

          Last edited by Rems; 5th June 2008, 20:36.

          This posting is provided "AS IS" with no warranties, and confers no rights.


          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts