windows .bat 脚本复制到 System32 导致拒绝访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3843783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:21:05  来源:igfitidea点击:

.bat script copy into System32 gives Access Denied

windowspermissionsbatch-filecopy

提问by Berming

I'm writing a .bat script where I need to copy a file to System32. I change to the folder then attempt to copy the file from a storage folder to the System32 folder.

我正在编写一个 .bat 脚本,我需要将文件复制到 System32。我更改到文件夹,然后尝试将文件从存储文件夹复制到 System32 文件夹。

cd C:\Windows\System32
copy %~dp0file.txt file.txt

I get an error Access Denied, 0 files copied.

我收到一个错误Access Denied, 0 files copied

I see why this is a problem, because if I try to copy a file to System32 using the non-programmatic GUI interface, I get a prompt asking me to confirm. So with the script, how do I bypass this Windows permissions or set it correctly, or some other solution.

我明白为什么这是一个问题,因为如果我尝试使用非编程 GUI 界面将文件复制到 System32,我会收到一个提示,要求我确认。因此,使用脚本,如何绕过此 Windows 权限或正确设置它,或其他一些解决方案。

Edit: The hint I got from the answer below is that it's possible to trigger Windows to show its GUI prompt for the user to give permissions. This idea will do. Hopefully someone knows exactly how to do this.

编辑:我从下面的答案中得到的提示是,可以触发 Windows 显示其 GUI 提示以供用户授予权限。这个想法会做。希望有人确切地知道如何做到这一点。

回答by Ben Voigt

Belongs on superuser (voting to move).

属于超级用户(投票移动)。

Answer is: use an elevated command prompt (or if you launch your batch file from a shortcut, select "Run as Admin" in the shortcut properties)

答案是:使用提升的命令提示符(或者如果您从快捷方式启动批处理文件,请在快捷方式属性中选择“以管理员身份运行”)

EDIT: Now that you explain you're looking for a programmatic way to trigger elevation, you should have a look at this other question(not necessarily the accepted answer, but all the other answers). I'd still vote to close, but as a dupe instead of move to superuser. From batch you might want to look at the "runas" command, but it will still need user confirmation.

编辑:既然您解释了您正在寻找一种触发提升的编程方式,您应该看看另一个问题(不一定是已接受的答案,而是所有其他答案)。我仍然会投票关闭,但作为一个骗子而不是转向超级用户。从批处理中,您可能想查看“runas”命令,但它仍需要用户确认。

回答by Ruel

You can use RunAscommand to copy the file as a local administrator. http://ss64.com/nt/runas.html

您可以使用RunAs命令以本地管理员身份复制文件。http://ss64.com/nt/runas.html

回答by mythofechelon

I use this in all of my batch files to check admin permissions at the beginning of the batch file?

我在所有批处理文件中都使用它来检查批处理文件开头的管理员权限吗?

@echo off
goto permissionCheck

:permissionCheck
    echo Checking permissions...

    SET adminRights=0
    FOR /F %%i IN ('WHOAMI /PRIV /NH') DO (
        IF "%%i"=="SeTakeOwnershipPrivilege" SET adminRights=1
    )

    IF %adminRights% == 1 (
        echo Elevated permissions confirmed.

        pause >nul
    ) ELSE (
        echo Elevated permissions absent.
        echo.
        echo This file needs to be run as an Administrator.
        echo.
        echo Close this window, right-click on the file and click "Run as Administrator".
        echo   OR
        echo Log on to an Administrator account and run this file normally.

        pause >nul
    )

I'm aware that this solution doesn't enable you to copy to system32without admin rights, but seeing as you need admin rights and there's no way around that I thought I would offer this solution.

我知道这个解决方案不能让你在system32没有管理员权限的情况下复制到,但是看到你需要管理员权限并且没有办法,我想我会提供这个解决方案。