简体   繁体   English

即使具有管理员权限,批处理脚本访问也被拒绝

[英]Batch script access denied even with admin privileges

I have a batch script in Windows7 to update the hosts file that fails.我在 Windows7 中有一个批处理脚本来更新失败的hosts文件。 I am logged as user with administrative rights.我以具有管理权限的用户身份登录。

Even if I run the script with "Run as administrator" option I get Access denied. 0 files copied即使我使用“以管理员身份运行”选项运行脚本,我也会Access denied. 0 files copied Access denied. 0 files copied when executing this part of the script:执行这部分脚本时Access denied. 0 files copied

for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b
set ip=%ip:~1%

REM create changing part of hosts file...   
if exist %temp%\temp.txt del %temp%\temp.txt
echo %ip% myproxy >> %temp%\temp.txt  

REM check this...
set hostpath=C:\WINDOWS\system32\drivers\etc

REM add static part of hosts file
type "%hostpath%\hosts-static" >> %temp%\temp.txt

REM replace hosts file
copy /y %temp%\temp.txt "%hostpath%\hosts"

ipconfig /flushdns
netsh interface ip delete arpcache
pause

I also tried to create a shortcut and set the "Advanced -> Run as Administrator" option but no luck.我还尝试创建一个快捷方式并设置“高级 - > 以管理员身份运行”选项,但没有成功。

If I open a cmd shell as Administrator and then run the script from there everything works fine, but no way of running it directly double-clicking on the file (or its link).如果我以管理员身份打开 cmd shell,然后从那里运行脚本,一切正常,但无法直接双击文件(或其链接)运行它。 Any idea?有什么想法吗?


EDIT:编辑:

  • added the whole script.添加了整个脚本。
  • I tried creating a shortcut for the following command to execute as Administrator我尝试为以下命令创建快捷方式以管理员身份执行

    C:\\Windows\\System32\\cmd.exe /c script.bat C:\\Windows\\System32\\cmd.exe /c script.bat

and it is also failing.它也失败了。 From the same shortcut (without arguments) I can open a window where I can execute the batch correctly.从相同的快捷方式(不带参数)我可以打开一个窗口,我可以在其中正确执行批处理。 I really cannot see why.我真的不明白为什么。

Try attrib -r -s -h -a "%hostpath%\\hosts" before your copy command.copy命令之前尝试attrib -r -s -h -a "%hostpath%\\hosts" If any file is attributed +r, +s, or +h, you'll get "Access is denied" if you try to overwrite it using copy .如果任何文件的属性为 +r、+s 或 +h,如果您尝试使用copy覆盖它,您将收到“访问被拒绝”。

Obviously a late response, but just solved this issue with a very straightforward solution so I thought I'd share:显然是一个迟到的回应,但只是用一个非常简单的解决方案解决了这个问题,所以我想我会分享:

Using ICACLS you can modify access control lists (ACLs) to bypass access denied errors.使用ICACLS,您可以修改访问控制列表 (ACL) 以绕过拒绝访问的错误。 Run the following command:运行以下命令:

ICACLS C:\path\to\batch\file\directory\* /C

the parameter /C tells the batch file to bypass access denied errors.参数 /C 告诉批处理文件绕过拒绝访问错误。 Cheers.干杯。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Robocopy-Windows权限(即使使用管理员也拒绝访问) - Robocopy - Windows Permission (Access Denied even with Admin) 尝试在 Windows PowerShell 中使用 Python 时权限被拒绝,即使具有管理员权限 - Permission denied when trying to use Python in Windows PowerShell, even with admin privileges 即使已经有管理员,如何检查是否需要管理员权限才能访问文件夹? - How to check if admin privileges are needed to access a folder even if already has admin? 试图删除ProgramData中的文件。 访问被拒绝,甚至以管理员身份 - Trying to delete file in ProgramData. Access is Denied, even as Admin 批处理-检查给定进程是否正在以管理员权限运行 - Batch - check if given process is running with admin privileges 如果呼叫与批处理脚本中的MOVE一起使用,则访问被拒绝 - Access denied if Call used along with MOVE in batch script 具有更高特权的WiX CustomAction SCHTASKS“访问被拒绝” - WiX CustomAction with elevated privileges SCHTASKS “Access Denied” Windows批处理脚本和在HomePath中工作的特权 - Windows batch script and privileges for working in HomePath 使用访问令牌共享进程管理员权限 - sharing process admin privileges using Access Tokens 即使应用程序以管理员身份运行,SetNamedSecurityInfo() 函数也会返回 ACCESS_IS_DENIED(Winerror.h 中的错误代码 5) - SetNamedSecurityInfo() function returning ACCESS_IS_DENIED (error code 5 in Winerror.h) even when the application is run as admin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM