Solved

best way to reconnect map drive disconnected...

Posted on 2011-09-12
21
983 Views
Last Modified: 2012-08-13
on windows, it seem's a normal behaviour the a map drive for example \\PC\\share appear in the GUI with a red cross when the system disconnect it...
i know that in the GUI, a simple click on it will reconnect the drive
BUT if a batch try to use this map when is disconnected, an error occur...
i'm searching a way to reconnected a network map in a prompt command, to be shure that my script will run without problem...
ps: sorry for my bad english, and grammar, i'm a french user...
0
Comment
Question by:michelcabare
  • 7
  • 6
  • 6
  • +2
21 Comments
 
LVL 2

Expert Comment

by:artisticsoul
ID: 36527277
If you know the mapped drive is H:\ you could disconnect the drive in the beginning of the script and then remap it.

//Delete the map
net use /d h:

//Then remap
et use h: \\servername\share
0
 

Author Comment

by:michelcabare
ID: 36527335
yes, it's a way, but the drive is not alway the same...

i'ml searching something like 'clic' on GUI but in a batch environnement,
like "reconnect all drives"
or reconnect drives you know... by "net use"...

i don't kwno if it's possible

0
 
LVL 4

Expert Comment

by:ZephyrTC
ID: 36527362
If you go with an updated script, something like this would work (VBS)
Dim WSHNetwork, objNetwork

On Error Resume Next

'Disconnect ALL mapped drives
	Set clDrives = WshNetwork.EnumNetworkDrives
		For i = 0 to clDrives.Count -1 Step 2
	WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
     Next

'Maps drive with logged in credentials
Call DriveMapper("Q:", "\\YourServer\YourShare", "", "")

'Maps drive with supplied credentials
Call DriveMapper("Q:", "\\YourServer\YourShare", "User", "Password")
Function DriveMapper(DLetter, DPath, User, Password)

 Set objNetwork = CreateObject("WScript.Network")
 Credentials = True
   If User <> "" Then Credentials = False 
     strUser = User
     strPass = Password
   'WScript.Echo "Mapping " & Dletter & " to " & Dpath
	 If Credentials <> False Then
     objNetwork.MapNetworkDrive DLetter, Dpath
	  Else 
     objNetwork.MapNetworkDrive DLetter, Dpath, Credentials, User, Password
	 End If
End Function

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:ZephyrTC
ID: 36527368
If you need to connect drives according to user, you can add a bit more to the code to accomplish this as well...
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36528155
Hmm, for me:

C:\Documents and Settings\Administrator>net use
New connections will be remembered.


Status       Local     Remote
-----------------------------------------------
Disconnected Z:        \\server2\install2
                       \\TSCLIENT\C
                       \\TSCLIENT\D
The command completed successfully.


C:\Documents and Settings\Administrator>z:

Z:\>

And it works.  i.e. add

z:
cd \whatever
to your script.

Another option if that doesn't work may be to use pushd / popd to map a connection to the same share:
pushd \\server\share
REM Now has temporary z: drive to that share.  Tends to trigger other connection back to life too.
popd
Or if explorer works and above doesn't:

explorer h:\
taskkill /IM "H:\"
(or something like that).

Steve
0
 

Author Comment

by:michelcabare
ID: 36534005
hello dragon-it
i'm surprised or maybe don't understand what you explain,

because in my case, on XP or Seven, if network map are in the profile, they will be mounted when the user log in and activate his profile...
(for example he create himself a network map...) and if the NAS or whatever is down at this moment, the map is show disconnected...
when the nas is available.. the map network is still disconnected... and a batch script will fail... the user MUST reactivate the map with the GUI (by click on it) and after a script will works...

i'm searching something for reactivate map when they are available....

hope my description is not too confuse....

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36534056
i completely understand.  this was xp for me too.... In this case to disconnected permanent mapped drive because server not available when (in this case) workstation booted.

Next time i have a 7 machine booted (later today probably) will try one too.

Have you tried either of above on one of yours then already - i.e. If changing drive in batch (not just accessing it as part of a copy command say) or using pushd \\server\share that it refers too and popd afterwards or does it show if you type explorer h:\ from Run / searrch button in start menu.  if so the last listed may work.

If not it is certainly possible with vbs or batch to read the disconnected drive shares and try to remake them, will try that against some disconnected ones next time I can make some...

Steve

Steve
0
 

Author Comment

by:michelcabare
ID: 36534108
thank's for help
i need time to try yours suggestions now....
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36534153
ok, no problem.  will try few other things when i nect  see a disconnected drive too.
0
 

Author Comment

by:michelcabare
ID: 36564746
ok, i've try few test, and here are the results...
for me on seven computer when a net use show a disconnect map, i can't simply "use", i mean that in this situation,

Status       Local     Remote
-----------------------------------------------
Disconnected Z:        \\server2\install2

a command like Z: failed...

If i (like artisticsoul suggest) remove and rebuild this map it work's...  
ie a
net use Z: /delete
and following
net use z: \\server2\install2

But this solution seem for me not very practice, it mean's that to be shure, we have to rebuils each map before script, Map by map...

the other solution (as dragon-it suggested) with
pushd \\server\share
finishing by popd
work's fine... so i'will probably use it....


sorry for the solution of zephirTC with VBS, i'm not familiar with this scripting language.... so i didn't test his sugestion.

Shure that i 'll prefered a batch who is able to read disconnect drive shares (net use...) and try to remake them with same letter...


0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36565036
ok then as pushd works will make you little script to remap disconnected once on pc.

Something like this:

@echo off
for /f "tokens=2 delims=:" %%a in ('net use | find /i "Disconnected"') do (
  pushd "%%~a"
  popd
)

Give it a try, haen't got any disconnected drives (and PC not on yet).

Steve
0
 

Author Comment

by:michelcabare
ID: 36578516
ok, i try and i heve the following message
| (pipe) unnattended...
anywa, if i try to understand your response, (and if you help me for this "pipe"...
i will begin my script by the sequence :

for /f "tokens=2 delims=:" %%a in ('net use | find /i "Disconnected"') do (
  pushd "%%~a"
  popd
)

and after the map should be all reconnected...
in this case definitly this is a good solution for my problem...

thank's in advance
michel
0
 
LVL 3

Expert Comment

by:fireline1082
ID: 36578544

I guess the professional way to do this is by using scripting as suggested above.

Or you can also disconnect all drives by running command:

net use * /delete

and then remap the drives like net use M: \\share\data /Persistent:yes and so one for other drives

Just suggestion
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 125 total points
ID: 36578997
sorry missed of an escape character for the pipe symbol - when used in a for command you need to add a circumflex before it :

^|

that was interesting to find on mobile!

steve
0
 
LVL 4

Expert Comment

by:ZephyrTC
ID: 36581315
@ michelcabare

Have you tried my script above (very first post)?
It removes all network drives on the machine and then maps new drives according to the call command to the function.  Example:
To map a Q: drive to \\Server\ShareName, using the currently logged in credentials, the call command would look like this:
Call DriveMapper("Q:", "\\Server\ShareName", "", "")

Open in new window

To map a Z: drive to \\YourServer\YourShare, using the username "User" and the password "Password", the call command would look like this:
Call DriveMapper("Z:", "\\YourServer\YourShare", "User", "Password")

Open in new window


Simply edit each line that starts with "Call DriveMapper" to suit your needs below, and the problem will be solved.  Is there a reason you're trying to do this through .bat scripting and the command line?

'Save this file as a .VBS and run!
'FULL SCRIPT
Dim WSHNetwork, objNetwork

On Error Resume Next

'Disconnect ALL mapped drives
	Set clDrives = WshNetwork.EnumNetworkDrives
		For i = 0 to clDrives.Count -1 Step 2
	WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
     Next

'Maps drive with logged in credentials
Call DriveMapper("Q:", "\\YourServer\YourShare", "", "")

'Maps drive with supplied credentials
Call DriveMapper("Q:", "\\YourServer\YourShare", "User", "Password")


Function DriveMapper(DLetter, DPath, User, Password)

 Set objNetwork = CreateObject("WScript.Network")
 Credentials = True
   If User <> "" Then Credentials = False 
     strUser = User
     strPass = Password
   'WScript.Echo "Mapping " & Dletter & " to " & Dpath
	 If Credentials <> False Then
     objNetwork.MapNetworkDrive DLetter, Dpath
	  Else 
     objNetwork.MapNetworkDrive DLetter, Dpath, Credentials, User, Password
	 End If
End Function

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36581440
the point of mine was to read whatever was disconnected and reconnect the same without having to trace what it was first.... Of course we could do as you say, or just delete all with net use and remake too.

+or login scripts I do use vbscript where poss. If not batch but either do the same in this context and batch is shorter!

Was thinking of doing same in vbs, read current shares, disconnect and recoonect but dont know if you can tell they are disconnected?
0
 
LVL 4

Expert Comment

by:ZephyrTC
ID: 36581533
I hadn't realized it was a requirement to target a single drive.  IN that case, this would work:

'Save this file as a .VBS and run!
'FULL SCRIPT
Dim WSHNetwork, objNetwork

 Set objNetwork = CreateObject("WScript.Network")
On Error Resume Next

'Disconnect Specific Mapped drives
'Removes Z Drive
objNetwork.RemoveNetworkDrive "Z:"
'Removes Q Drive
objNetwork.RemoveNetworkDrive "Q:"


'Maps drive with logged in credentials
Call DriveMapper("Q:", "\\YourServer\YourShare", "", "")

'Maps drive with supplied credentials
Call DriveMapper("Q:", "\\YourServer\YourShare", "User", "Password")


Function DriveMapper(DLetter, DPath, User, Password)

 Credentials = True
   If User <> "" Then Credentials = False 
     strUser = User
     strPass = Password
   'WScript.Echo "Mapping " & Dletter & " to " & Dpath
	 If Credentials <> False Then
     objNetwork.MapNetworkDrive DLetter, Dpath
	  Else 
     objNetwork.MapNetworkDrive DLetter, Dpath, Credentials, User, Password
	 End If

Open in new window


Keep in mind that, if files are open on the drive, the disconnect will fail.  You can comment out the "on error resume next" line with a ' for troubleshooting purposes.
0
 
LVL 4

Expert Comment

by:ZephyrTC
ID: 36581574
Edit: Not sure if there is an attirbute we can parse from the registry or WMI that would let us identify disconnected drives only and parse their drive letter.  This would be useful.  I will look into it.
0
 

Author Closing Comment

by:michelcabare
ID: 36581600
ok, this solution is the most suitable for me, because i'm use script batchn and not familmiar at all with vbs...
so thank's for every one whoe give hint, and sure i'll be back with another problems soon...
than'ks again
michel
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36581652
hmm, didnt notice that was just 125 points :-( oh well, hope it works for you ok anyway!


Steve
0
 
LVL 4

Expert Comment

by:ZephyrTC
ID: 36581711
This will detect the connection state of the drives.  Soon will post something that will apply the information in the way we want it to:

Set objCol = GetObject("WinMgmts:").InstancesOf("Win32_NetworkConnection")
For Each Drive In objCol
       
       WScript.Echo drive.ConnectionState

Next

Open in new window

0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HP laptop drivers 12 58
Is Fedora an appropriate distro for the environment. 7 86
WMIC Output Formatting 1 43
Windows 7 - BSOD associated with video driver 34 48
New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question