• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1063
  • Last Modified:

best way to reconnect map drive disconnected...

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
michelcabare
Asked:
michelcabare
  • 7
  • 6
  • 6
  • +2
1 Solution
 
artisticsoulCommented:
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
 
michelcabareAuthor Commented:
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
 
ZephyrTCCommented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
ZephyrTCCommented:
If you need to connect drives according to user, you can add a bit more to the code to accomplish this as well...
0
 
Steve KnightIT ConsultancyCommented:
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
 
michelcabareAuthor Commented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
michelcabareAuthor Commented:
thank's for help
i need time to try yours suggestions now....
0
 
Steve KnightIT ConsultancyCommented:
ok, no problem.  will try few other things when i nect  see a disconnected drive too.
0
 
michelcabareAuthor Commented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
michelcabareAuthor Commented:
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
 
fireline1082Commented:

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
 
Steve KnightIT ConsultancyCommented:
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
 
ZephyrTCCommented:
@ 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
 
Steve KnightIT ConsultancyCommented:
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
 
ZephyrTCCommented:
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
 
ZephyrTCCommented:
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
 
michelcabareAuthor Commented:
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
 
Steve KnightIT ConsultancyCommented:
hmm, didnt notice that was just 125 points :-( oh well, hope it works for you ok anyway!


Steve
0
 
ZephyrTCCommented:
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
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 7
  • 6
  • 6
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now