简体   繁体   中英

Batch: run as administrator from mapped network drive

So, I have that common problem that everyone seems to have of trying to run a batch file stored on a network drive as an administrator. I have scoured the internet and there are some 'workarounds' but I am not sure how to apply them if I can at all.

Actual task: batch file (lets call this batch #1) to copy a file "mms.cfg" from a network drive (same dir as batch) to system32 and syswow64. Of course, these folders are protected thus the need to run as admin. But when you run as admin, the mapped network drive doesn't exist for the "admin" account and the batch craters.

In this particular instance batch #1 runs a silent install of flash. After that it needs to copy a config file to the aforementioned protected folders to disable its auto-update feature since our standard users can't run the updates which flash would otherwise nag them about (why can't adobe just add a switch to their installer to turn off this feature?).

What I've tried: I have seen the work around to set EnableLinkedConnections=1 in the registry, but I need to deploy this batch file to numerous PCs on a network and cannot change that registry key on each one.

I've also tried to re-map the directory after I run as admin. Again, the batch craters since it is being ran from a mapped network drive and "administrator" doesn't know this drive exists, thus the crash before I get the chance to map the drive (via pushd or whatever).

I cannot simply copy the batch to the local PC to run it each time as: 1) it does other tasks (starts two installers), 2) the batch file itself is being ran from another batch (denote as batch #2) that is going down a list of software updates and applying them silently, 3) this is to apply a series of updates where I (domain admin) would log into each PC and run batch #2 to update a series of programs (domain auto-maps directory to updates, thus I navigate to I:\\updates and run it).

End result I'm hoping for: Essentially this boils down to how do I run a batch stored on a mapped network drive with administrative rights? Is there any way to do this on any given networked machine or am I reduced to copying the config file manually to system32 and syswow64 for each PC that I want to update?

TLDR: Run batch stored on a mapped network drive as admin without it cratering.

If I was unclear or need to provide more information I will do my best to check back. Thank you in advance for any help!


UPDATE - So, I was able to get EnableLinkedConnections=1 set across our computers in the domain via GPO. However, I have noticed another interesting issue: when you right-click & run as admin, it runs fine. However, including the below code to auto-request admin rights now fails when you just run the script. Maybe I'm just missing something simple?

Again, thanks in advance for any help!

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------

If anyone else is running into this problem (like me) my workaround was to go to the UNC path and create a shortcut to the batch file. Then when you run the shortcut as administrator from the mapped drive, it will start the batch file as UNC path.

I use this kind of batch file to run commands or other batch files from network mapped drive.

@echo off
SETLOCAL EnableDelayedExpansion

set FULLPATH=%CD%
set DRIVE=%FULLPATH:~0,2%
set DIRECTORY=%FULLPATH:~2%


:: This script will run first time without admin rights and solve UNC path from the mapped drive path ot regular path if mapped drive is not used.
:: The script will pass the path as an argument %~2 for it self when the script is runned with admin rights.


for /f "tokens=* delims= " %%A in ('net use %DRIVE% 2^> nul ^| find /i "\\"') do set line=%%A


    if "%line%" == "" (
        :: If temp is empty, script is not on network mapped drive and a folder of the bat file should be used.
        set UNCPATH=localfolder

    ) else (
        set UNCNAME=%line:*\\=\\%
        :: eg \\192.168.1.20\sharename
        set "UNCPATH=!UNCNAME!!DIRECTORY!"

    )


:: This will run the script second time and ask adming rights
if not "%1"=="am_admin" (powershell start -verb runas '%0' 'am_admin "!UNCPATH!"' & exit /b)

if "%~2" == "localfolder" (
    pushd %~dp0
) else (
    pushd %~2
)


::------------------------------------------------------------------------------

echo Place your code here^!
dir
echo CD: %CD%
echo Bat file DIR: %~dp0

::------------------------------------------------------------------------------

popd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM