如何轻松重新连接到“不可用”的网络共享

如何轻松重新连接到“不可用”的网络共享

有时候,我会以管理员用户身份运行 cmd。为了方便起见,我设置了一个快捷方式...

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

有时候,我想运行指向普通用户可用的映射驱动器的脚本。

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.

我特别想让我的 L: drive 进入地图。我认为类似这样的方法应该可行...

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.

如果我重新映射驱动器,一切都会顺利,但这是一项苦差事(实际上“路径”要长得多)。

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

C:\Windows\system32>l:

L:\>

答案1

尝试一下这个:

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

C:\>n:
系统找不到指定的驱动器

C:\>for /f "TOKENS=3,4" %a in ('net use') do net use %a %b
... [忽略输出] ...

C:\>n:

N:\>

答案2

正如所述这篇 Technet 文章这是 UAC 的副作用。可以通过在注册表项中添加HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System名称EnableLinkedConnections、类型DWORD和值的条目来避免这种1情况。这将使在常规用户凭据下创建的网络连接也可用于通过 RunAs 以提升的权限运行的进程。

答案3

问题:Windows 启动后,无法访问例如驱动器 n:
为了解决这个问题,我编写了一个批处理文件,它只是激活连接。

解决方案:

C:\>n:
系统找不到指定的驱动器

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

下面是一个批处理脚本

  • 遍历所有网络连接并
  • 询问每个不可用的共享该做什么(断开连接、刷新、忽略)

修复工具

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
    )
)

相关内容