Ok. I might have a solution. I tried looking into the /savecred
parameter of net use
but couldn't get it to work properly so here is another approach:
Your trouble was the delay with Explorer after the disconnect with the VPN.
So why not hide the drives from Explorer after the disconnect so it won't try to access them.
There is a registry key to hide drives from Explorer:
HKEY_xxx\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoDrives
and HKEY_xxx can both be HKEY_LOCAL_MACHINE
and HKEY_CURRENT_USER
.
Below this post there is a summary as to what value NoDrives
should hold.
(add the numbers for the drives).
I've created a small batchfile where you don't have to do the calculations yourself.
It is a 'toggle'-batchfile.
Run it once to connect to your "My VPN".
Run it again to disconnect and hide the drives I, J and K.
You can easily split this in two batchfiles but you get the point... You could also simplify this batchfile if you set the number of NoDrives
yourself instead of letting the batch do the calculation.
This needs to be run as administrator. (it is possible to automatically elevate but that's very advanced)
If you grant yourself permission on the key NoDrives
you don't have to run this as administrator. Just open up regedit.exe
, browse to this key and click "Edit > Permissions" and grant yourself "Full Control" over this key.
@echo off
:: ------------------------
set MyVPN="My VPN"
:: ------------------------
rasdial | findstr %MyVPN% 1>nul
If %ERRORLEVEL% == 0 goto disconnect
:connect
echo ============================================================
echo connecting to %MyVPN%
echo ============================================================
rasdial %MyVPN%
:: This is easy. We just set 0 in NoDrives
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v NoDrives /T REG_DWORD /d 0 /f
goto :end
:disconnect
echo ============================================================
echo disconnecting from %MyVPN%
echo ============================================================
rasdial %MyVPN% /disconnect
:: ------------------------------------------------------------------
:: This is harder. We need to determine what value NoDrives should be
:: ------------------------------------------------------------------
Setlocal EnableDelayedExpansion
:: ------------------------
:: Set here your VPN drives
:: ------------------------
set drives='I J K'
:: ------------------------
:: Calculation to determine the value
:: ------------------------
set drive_value=1
set NoDrives=0
for %%c in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
echo.%drives% | findstr "%%c" 1>nul
if !ERRORLEVEL! == 0 set /a NoDrives+=drive_value
set /a drive_value="drive_value<<1"
)
echo Seting NoDrives to %NoDrives%
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v NoDrives /T REG_DWORD /d %NoDrives% /f
endlocal
:end
pause
Note:
Windows Explorer needs to be closed and opened again before the drives are hidden (or visible again).
Settings for NoDrives
:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoDrives
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoDrives
Decimal Hex
A: 1 1
B: 2 2
C: 4 4
D: 8 8
E: 16 10
F: 32 20
G: 64 40
H: 128 80
I: 256 100
J: 512 200
K: 1024 400
L: 2048 800
M: 4096 1000
N: 8192 2000
O: 16384 4000
P: 32768 8000
Q: 65536 10000
R: 131072 20000
S: 262144 40000
T: 524288 80000
U: 1048576 100000
V: 2097152 200000
W: 4194304 400000
X: 8388608 800000
Y: 16777216 1000000
Z: 33554432 2000000
2Just out of curiosity, why do you think you need this? – Zoredache – 2013-10-29T18:47:05.313
2I connect to a VPN, use the K: drive, then disconnect the VPN. After that, my Explorer is often slow, until it finally marks K: as disconnected. This is a simplification, as I have more than 3 drive mappings needed by various scripts, and may connect/disconnect from the VPN multiple times a day. – Glen Little – 2013-10-29T20:12:07.477
1@GlenLittle Have you tried unchecking the "reconnect at logon" option when you map the drive? – Moses – 2013-10-30T02:09:49.817
No, I haven't. Most of the mappings are done with
net use
, but they are persistent. – Glen Little – 2013-10-31T23:03:42.5234
Why not make a script to connect and disconnect the drives (including removing the mapping). Like here You could even create a script which connect to the VPN automatically and a script to disconnect (including all the drive-mapping stuff)
– Rik – 2013-11-01T22:18:52.800Thanks Rik. Interesting script (sort of). But I wouldn't want to store all the passwords in the file. I may need to do something similar though... – Glen Little – 2013-11-02T03:04:03.610
Try disabling the network adapter associated with the VPN connection (once you're done with it) and check if it helps with the Explorer delay. – Kedar – 2013-11-03T10:20:51.817
@kedar I only have one adapter. The VPN "adapter" cannot be disabled, only disconnected. However, I did try disabling/enabling the main adapter and it did not seem to help. – Glen Little – 2013-11-04T16:46:13.043
Does adding the hostnames of servera, serverb etc with their ip jumbers in lmhosts help? That way the hosts don't need to be searched on the network and the ip times-out directly. (Not the
hosts
butlmhosts
) – Rik – 2013-11-04T18:01:39.800Unfortunately, adding to the lmhosts file doesn't seem to help. – Glen Little – 2013-11-05T20:31:02.180
4A
net use \\serverB\share2 /delete
should do the trick (it disconnects the drive but doesn't afflict the mapping) – Paolo Gibellini – 2013-11-07T07:28:22.087@PaoloGibellini have you tried this yourself? I get a
The network connection could not be found.
error with this (using my own drive-specs of course). If you meannet use J: /delete
that really deletes the mapping. – Rik – 2013-11-07T09:36:12.8371@Rik Thank you, I hadn't tried it and yes, if a unit is mapped to a drive letter you must use the letter:
net use J: /delete
is the correct way. You should use the full path when you are connected to a network share without mapping it. Anyhow, at the next restart your mapping will be restored (either if created through a batch file using /persistent:yes or using the windows user interface). – Paolo Gibellini – 2013-11-07T10:22:38.587You can not map it in the first place. Access the share without a drive letter mapping with net use \server\share\vol /user:domain\username password then just access the folder \server\share\vol – Back2Basics – 2013-11-07T22:01:43.733