how to easily reconnect to "unavailable" network shares

Solution 1:

Try this one:

for /f "TOKENS=3,4" %a in ('net use') do net use %a %b

C:\>n:
The System cannot find the drive specified

C:\>for /f "TOKENS=3,4" %a in ('net use') do net use %a %b
... [Ignore the output] ...

C:\>n:

N:\>

Solution 2:

As described in this Technet article this is a side effect of UAC. It can be avoided by adding an entry to the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System with name EnableLinkedConnections, type DWORD and value 1. This will make network connections created under regular user credentials available to processes run with elevated privileges via RunAs, too.

Solution 3:

Problem: After windows has started it is not possible to access e. g. drive n:
To solve this problem I have written a batch file which simply activates the connection.

Solution:

C:\>n:
The System cannot find the drive specified

C:\>ActivateMappedNetworkDrive.bat n:

C:\>n:

N:\>

ActivateMappedNetworkDrive.bat

@echo off
REM Problem: Mapped network drives are not "connected" after restart of windows
REM          If you open the windows explorer and click on such a network drive it works.
REM          But if you first try to access it by any script/program it won't work.
REM Solution: Open the desired network drive in a minimized explorer instance and close it shortly afterwards
REM Michael Hutter / August 2019

if "%1"=="" (
  echo Syntax: %0 DriveToConnect:
  echo Example: %0 n:
  goto End
)
set PROCESSNAME=explorer.exe
set PREFIX=start /min
set SUFFIX=%1

::First save current pids with the wanted process name
setlocal EnableExtensions EnableDelayedExpansion
set "RETPIDS="
set "OLDPIDS=p"
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='%PROCESSNAME%'" get ProcessID ^| findstr [0-9]') do (set "OLDPIDS=!OLDPIDS!%%ap")

::Spawn new process(es)
%PREFIX% %PROCESSNAME% %SUFFIX%

::Wait for a second (may be optional)
choice /c x /d x /t 1 > nul

::Check and find processes missing in the old pid list
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='%PROCESSNAME%'" get ProcessID ^| findstr [0-9]') do (
if "!OLDPIDS:p%%ap=zz!"=="%OLDPIDS%" (set "RETPIDS=/PID %%a !RETPIDS!")
)

::Kill the new threads (but no other)
taskkill %RETPIDS% /T > NUL 2>&1
endlocal

:End