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

Val*_* DG 5 windows batch-file windows-7-x64

我在Windows7中有一个批处理脚本来更新hosts失败的文件。我以具有管理权限的用户身份登录。

即使我使用“以管理员身份运行”选项运行脚本,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
Run Code Online (Sandbox Code Playgroud)

我还尝试创建快捷方式并设置“高级 -> 以管理员身份运行”选项,但没有成功。

如果我以管理员身份打开 cmd shell,然后从那里运行脚本,一切正常,但无法直接双击文件(或其链接)来运行它。任何想法?


编辑:

  • 添加了整个脚本。

  • 我尝试创建以下命令的快捷方式以管理员身份执行

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

但它也正在失败。通过相同的快捷方式(不带参数),我可以打开一个窗口,在其中可以正确执行批处理。我真的不明白为什么。

roj*_*ojo 0

attrib -r -s -h -a "%hostpath%\hosts"在你的命令之前尝试一下copy。如果任何文件的属性为 +r、+s 或 +h,则当您尝试使用 覆盖它时,您将收到“访问被拒绝”的消息copy