Remapping network drives

I am in the process of implementing DFS Namespaces and I have clients that have network drives mapped to four different file servers.  I need some sort of script (powershell, vbs, etc.) that will remap the old file server name to the new DFS namespace with little or no impact to clients.
candlendsAsked:
Who is Participating?
 
Richard QuadlingConnect With a Mentor Senior Software DeveloperCommented:
Sorry for the delay.Try this manually on a few workstations first. See what you think.The lines which have ...ECHO MAP ....are the actual mapping commands to drop and reconnect.The script also handles not dropping and reconnecting the mapping once it is on the named DFS.Just edit the 5th line to reflect the new DFS name.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
 
REM Edit the line below.
SET DFS=Distributed_File_System_Name_Goes_Here
 
REM Iterate the NET USE output
FOR /F "tokens=1,2* delims= " %%A IN ('NET USE') DO (
 
	SET TestLocal=%%B
	SET TestRemote=%%C
 
REM Remove the "Microsoft Windows Network" part if it is present as part of the Remote.
	IF "!TestRemote:~-25!"=="Microsoft Windows Network" (
		SET TestRemote=!TestRemote:~0,-25!
	)
 
REM Trim any whitespace
	CALL :_Trim !TestRemote!
	SET TestRemote=!TRIM_Response!
 
REM Only process Local Drives (not printers)
	IF "!TestLocal:~-1!"==":" (
 
REM Only process Remotes which are not mapped to hidden shares (Remote ends in $)
		IF NOT "!TestRemote:~-1!"=="$" (
			CALL :_Pos "!TestRemote!" \ 3
			CALL :_Split "!TestRemote!" !POS_Response!
 
REM Only process shares which are NOT already mapped to the DFS
			IF /I NOT "!SPLIT_Left!"=="\\!DFS!" (
				ECHO Drive !TestLocal! is currently mapped to "!TestRemote!"
				ECHO Drive !TestLocal! will be mapped to "\\!DFS!\!SPLIT_Right!"
				ECHO.
 
				ECHO NET USE /D !TestLocal!
				ECHO NET USE !TestLocal! "\\!DFS!\!SPLIT_Right!" /PERSISTENT:YES
				ECHO.
			)
		)
	)
)
GOTO :EOF
 
:_Trim
SET TRIM_Response=%*
GOTO :EOF
 
:_Pos
SET POS_Response=%3
SET POS_Work=%1
SET POS_Find=%2
:_PosLoop
IF "!POS_Work:~%POS_Response%,1!"=="%POS_Find%" GOTO :EOF
SET /A POS_Response+=1
GOTO _PosLoop
 
:_Split
SET SPLIT_Work=%~1
SET SPLIT_Right=!SPLIT_Work:~%2,255!
SET /A SPLIT_Work-=1
SET SPLIT_Left=!SPLIT_Work:~0,%2!
GOTO :EOF

Open in new window

0
 
Lunda_ConstructionCommented:
Change it in the logon script, then have each user restart.
0
 
Richard QuadlingSenior Software DeveloperCommented:
The NET USE command in the login script should do what you want.

The syntax of this command is:


NET USE
[devicename | *] [\\computername\sharename[\volume] [password | *]]
        [/USER:[domainname\]username]
        [/USER:[dotted domain name\]username]
        [/USER:[username@dotted domain name]
        [/SMARTCARD]
        [/SAVECRED]
        [[/DELETE] | [/PERSISTENT:{YES | NO}]]

NET USE {devicename | *} [password | *] /HOME

NET USE [/PERSISTENT:{YES | NO}]
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
candlendsAuthor Commented:
The net Use command won't work because I have hundreds of clients each with unique network drive mappings that have to be remapped.  I need a script that will look at the existing drive mappings and substitute the old server name for the new DFS namespace name.
0
 
Richard QuadlingSenior Software DeveloperCommented:
Aha. So you want to parse the output of NET USE...

And I'm on a standalone laptop at home ... no network.

Can you help by going to one of the clients and typing ...

NET USE > %TEMP%\MappedDrives.log

and then attach it here?

If you need to edit anything, simple replace the letters, don't delete them (just in case spacing is important).

So ...


PRIVATE

becomes

P*****E

for example.

0
 
candlendsAuthor Commented:
I have attached the net use file.  The drive letters and network drive mappings are very diverse, practically unique for each user
0
 
Richard QuadlingSenior Software DeveloperCommented:
So what criteria are you using for remapping?
0
 
candlendsAuthor Commented:
I will be replacing the old server name (i.e. \\server1\networkpath, \\server2\networkpath, \\server3\networkpath, etc.) with the new DFS Namespace so the new drive mapping will \\dfsname\networkpath.  I hope that is what you mean by criteria?
0
 
Richard QuadlingSenior Software DeveloperCommented:
Yep.

File missing.

I have to go now.

If you can post the file, I can take a look at it tomorrow for you.
0
 
candlendsAuthor Commented:
File doesn't seem to attach, here is the info in the file:

New connections will be remembered.


Status       Local     Remote                    Network

-------------------------------------------------------------------------------
Disconnected I:        \\r*********1\its         Microsoft Windows Network
             K:        \\r******s.ads\netlogon\users
                                                 Microsoft Windows Network
Disconnected Z:        \\a****a\terminatedemployees$
                                                 Microsoft Windows Network
The command completed successfully.
0
 
Richard QuadlingSenior Software DeveloperCommented:
So,

i: => \\dfs\its
k: => \\dfs\netlogon\users
z: => \\dfs\terminatedemployees$

Or whatever mappings they have.

I'll create a test script tonight.

Now I'm at work, I have another example to work from (see snippet).

I think those ending in $ may be an issue (for me I'm connecting to the root of drives rather than hidden shared folders.

What do you want to happen for those mappings that cannot be performed?

Say there is no mapping available?

Also, if users have mapped to the root of the drives on the servers ...

\\server1\c$

for example, how do you want those handled?



New connections will be remembered.
 
 
Status       Local     Remote                    Network
 
-------------------------------------------------------------------------------
OK           I:        \\bv-cluster-file\department data
                                                 Microsoft Windows Network
OK           M:        \\bv-cluster-file\Data Management
                                                 Microsoft Windows Network
OK           N:        \\bv-cluster-file\network area
                                                 Microsoft Windows Network
Disconnected S:        \\bv-vm-svr-8\SageCCC     Microsoft Windows Network
Disconnected T:        \\bv-vm-svr-19\CCC2SQL    Microsoft Windows Network
OK           Z:        \\richardquadling\scratch$
                                                 Microsoft Windows Network
Disconnected LPT2      \\BV-CLUSTER-PRN\BV-DIR-PRN-1
                                                 Microsoft Windows Network
The command completed successfully.

Open in new window

0
 
candlendsAuthor Commented:
Thanks for the help by the way!  You can probably disregard the shares ending in $ or root driives as it is not that common and can be dealt with in a one off fashion.  For those mappings that cannot be mapped the script should fail gracefully or just move onto the next share.
0
 
Richard QuadlingSenior Software DeveloperCommented:
Ok.

Thanks for that. I'll see what I can get to you today.
0
 
Richard QuadlingSenior Software DeveloperCommented:
AngelIII, my script performs as I expect it to. It would be great to have some feedback from the OP to know if it is what they wanted.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.