8

Every once in a while, I run cmd as my admin user. I set up a shortcut for convenience sake...

C:\Windows\System32\runas.exe /u:admin.user@domain cmd 

At some point, I've wanted to run scripts that point at mapped drives available to my regular user.

C:\Windows\system32>net use
Status       Local     Remote                    Network

-------------------------------------------------------------------------------
Unavailable  L:        \\fileserver\shared\path
                                                Microsoft Windows Network
Unavailable  X:        \\fileserver\shared\temp         Microsoft Windows Network
The command completed successfully.

I'm specifically interested in getting my L: drive to map. I think something like this should work...

C:\Windows\system32>net use l:
Local name        l:
Remote name       \\fileserver\shared\path
Resource type     Disk
The command completed successfully.

C:\Windows\system32>l:
The system cannot find the drive specified.

If I re-map the drive, everything is happy, but its a chore ("path" a lot longer in reality).

C:\Windows\system32>net use l: \\fileserver\shared\path
The command completed successfully.

C:\Windows\system32>l:

L:\>
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:\>

2
  • 'net use' produces multiple lines of output, which are not suitable for parsing in this way
    – Pete
    Nov 27 '19 at 17:57
  • 2
    This sent me down the right path, I have a .bat with this, seems to be working for Windows 7 and 10: FOR /F "TOKENS=1,2,3" %%A IN ('NET USE') DO IF "%%A"=="Unavailable" NET USE %%B %%C
    – Pete
    Nov 27 '19 at 18:15
5

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.

2
  • I actually have UAC disabled for the moment and it looks like enablelinkedconnections enabled. I don't want the same network connections available as the currently running user. I want the network connections available that were previously created with my runas user.
    – Pete
    Aug 4 '15 at 14:59
  • didn't help disabled UAC as well as added this parameter on my server 2008 R2
    – SeanClt
    May 10 '18 at 19:16
2

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
4
  • Thanks for the suggestion, but this solution would not work for my scenario. I'm trying to reconnect drives in the context of a cmd prompt launched as another user, using the runas command.
    – Pete
    Aug 20 '19 at 16:53
  • Thank you for this script. Dod you consider implementing the loop to reactivate all the unconnected mapped drives?
    – Arek
    Apr 28 '20 at 5:57
  • @Arek You have to call this script for each drive letter separately. Apr 29 '20 at 10:19
  • nice approach. I haven't seen that way of opening and closing a duplicate process before.
    – pbarney
    Jan 5 at 17:41
1

Here is a solution based on Michael Hutter's answer and comments found here. So, nothing new really, just combined all in one file that can be run to remount all network shares. All the credits go to people who posted previous answers.

@echo off

REM Script that remounts all network shares defined in windows explorer

echo "Remounting network shares ..."
FOR /F "TOKENS=1,2,3" %%A IN ('NET USE') DO IF "%%A"=="Unavailable" NET USE %%B %%C
FOR /F "TOKENS=1,2,3" %%A IN ('NET USE') DO IF "%%A"=="Disconnected" call :ActivateMappedNetworkDrive %%B
goto End

:ActivateMappedNetworkDrive
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)
REM choice /c x /d x /t 1 > nul
timeout /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
exit /b 0

:End
echo on
0

The problem occurs because of the way administrator accounts work in Vista and Windows 7. When you logon with an administrator account the account gets two tokens, a standard user token and an administrator token. When you originally mapped the drive the standard user token was used. In an elevated command prompt the administrator token is used. The mapped drive is outside of the context of the administrator token. I hope that made sense. The best work around is to use UNC drive paths or remap the drive in the elevated command prompt. Here is another workaround. Note that it is not recommended. I have not tested it in Windows 7. The article is about Vista.

http://support.microsoft.com/kb/937624

1
  • if I delete the connections (using net use l: /delete), and then recreate them in a administrator cmd window running as my domain admin user I still get this behavior. My question is actually about remapping the drives in an elevated command prompt, as you are suggesting. Unless I am mistaken, I should be able to reconnect a disconnected share by simply typing "net use L:" and not specifying the path.
    – Pete
    Oct 28 '16 at 18:49
0

the generell solving is, just wait windows for network - it slows down the boot for awhile but will prevent having disconnected network drives (in 99% of cases).

This policy setting (GPO) is located in Computer Configuration\Administrative Templates\System\Logon\ Always wait for the network at computer startup and logon

regardles, if you already run a Batchfile, opening the explorer makes more overhead as just disconnect and connect the drive.

Currently as years net use L: is and will be broken as it awaits that the path is applied. you may run either the disconnect or try only to remap the drive as you already suggested.

2
  • Thanks for the suggestion? Or maybe this was a response to Michael Hutter's solution? This doesn't really apply to my question. I'm launching a command prompt under a different user way after logon has occurred. You can assume the network is available.
    – Pete
    Aug 20 '19 at 17:01
  • @Pete the Problem is that the network drive are on disconnected state because Windows is not waiting for network during the Boot time and my suggestion is a generell solving
    – djdomi
    Aug 20 '19 at 21:05
0

Here comes a batch script which

  • iterates through all network connections and
  • asks for every unavailable share what to do (Disconnect, Refresh, Ignore)

NetUseRepair.bat

setlocal enableDelayedExpansion

rem for /f "TOKENS=1,2,3,4" %%a in ('net use') do @set MyVar=%%~c & if "!MyVar:~1,1!"==":" (echo net use %%c %%d) else echo a=%%a b=%%b c=%%c d=%%d
rem for /f "TOKENS=1,2,3,4" %%a in ('net use') do set MyTempVar=%%~c & set MyTempVar2=%%~a & if "!MyTempVar:~1,1!"==":" (echo net use %%c %%d) else if %%a==OK (echo OK: %%b - %%c) else if "!MyTempVar2:~1,1!"==":" (echo ? %%a - %%b) else rem echo a=%%a b=%%b c=%%c d=%%d
for /f "TOKENS=1,2,3,4" %%a in ('net use') do (
    set MyTempVar=%%~c & set MyTempVar2=%%~a
    if "!MyTempVar:~1,1!"==":" (
        echo Not available: %%c - %%d
        echo [D]isconnect / [R]efresh / [I]gnore
        choice /C DRI /N
        if errorlevel 3 (
            echo Ignore
            REM Ignore
        ) else if errorlevel 2 (
            REM Refresh
            echo net use %%c %%d
            echo on
            net use %%c %%d
            @echo off
            pause
        ) else if errorlevel 1 (
            REM Disconnect
            echo net use /delete %%c
            echo on
            net use /delete %%c
            @echo off
            pause
        )
    ) else if %%a==OK (
        echo Working/OK:  %%b - %%c
    ) else if "!MyTempVar2:~1,1!"==":" (
        echo Should work: %%a - %%b
    ) else (
        rem echo a=%%a b=%%b c=%%c d=%%d
    )
)
-1

I'm using a simple version:

explorer x:

where x is the drive letter. This works fine but opens obviously a new explorer window.

1
  • I've used this method before to reconnect a network drive after a server has gone down and come back up. I does not work when running from a cmd prompt launched as different user.
    – Pete
    Mar 1 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.