我正在编写一个使用批处理文件将某些文件复制到另一个位置的应用程序。我使用的是64位Windows 7。
我使用以下代码也要求管理员权限:
获取ADMIN权限的代码块:
@echo off
:: BatchGotAdmin (Run as Admin code starts)
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"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:: BatchGotAdmin (Run as Admin code ends)
:: Your codes should start from the following line
将文件复制到system32文件夹的代码:
copy /d /Y "D:\opt\optPath.txt" "C:\Windows\System32\"
复制操作没有错误,但文件被复制到" C:\ Windows \ SysWOW64 "位置自动。需要帮助。
答案 0 :(得分:1)
尝试使用:
@echo off
:: Batch-Admin API
net file>nul 2>&1&&if "%~1"=="64" (goto:GotAdmin) else (if exist "%windir%\Sysnative\" (call start %windir%\Sysnative\cmd /c "%~0" 64&exit) else (goto:GotAdmin))
echo Requesting administrative privileges...
(echo Set UAC = CreateObject^("Shell.Application"^)
echo UAC.ShellExecute "%~s0", "ELEV","", "runas", 0 ) > "%temp%\admin.vbs"
cscript /Nologo "%temp%\admin.vbs"&exit
:GotAdmin
:: Place ADMIN tasks below
copy /d /Y "D:\opt\optPath.txt" "C:\Windows\System32\"
pause
exit
我 modified 将您的脚本重写为:
net file && echo Admin || echo No-admin
VBScript
将所有内容启动为32-bit
(将C:\Windows\System32
重定向到C:\Windows\SysWOW64
)。 64位启动器:
call start %WinDir%\SysNative\cmd /c %0
(此窗口已隐藏)