[英]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:编辑:
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.