如何在管理员模式下执行批处理文件 [英] How do I execute a batch file in administrator mode
问题描述
我正在尝试编写一个批处理文件,以使用net share
在我的网络上共享文件夹,但是它需要管理员权限.我不知道如何在管理员模式下执行此命令.
I am trying to write a batch file to share a folder across my network using net share
but it needs administrator privileges. I dont know how to execute this command in admin mode.
在Windows搜索中右键单击cmd.exe并选择以管理员身份运行"时,它不会询问我密码,但是当我使用runas
命令编写批处理代码时,它会要求输入密码.为什么会这样?
When you right click cmd.exe in Windows search and select "Run as administrator" it doesn't ask me the password, but when I write a batch code using runas
command, it ask for a password. Why is it so?
我使用了以下命令
runas.exe /profile /user:administrator "cmd.exe"
start /wait cmd.exe /k "net share Inputs=Folder_Path /GRANT:Everyone,FULL"
没有工作.
我正在使用Win 7 32位
I am using Win 7 32-bit
此外,在某个地方,我看到共享文件夹后还将执行以下操作
Also, somewhere I saw that the following will also to be executed after the folder is shared
Icacls Folder_Path /grant Everyone:F /inheritance:e /T
这是必需的吗?
请帮助
推荐答案
If you want your batch script to ask for elevation if needed, this page might help you. I adapted it to use in a batch script I serve from the web like this:
REM --> Check for permissions
"%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system">nul 2>NUL
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
if exist "%temp%\getadmin.vbs" (
del "%temp%\getadmin.vbs"
echo Failed to acquire elevated privilege. Try saving this script and running it from your Desktop.
echo;
echo Press any key to exit.
pause>NUL
goto :EOF
)
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "%*", "", "runas", 1 >> "%temp%\getadmin.vbs"
cscript /nologo "%temp%\getadmin.vbs"
goto :EOF
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
:--------------------------------------
:: The rest of the script goes here...
关于尝试保存此脚本"消息,如果用户尝试直接从Web浏览器打开批处理脚本,则浏览器安全性可能会阻止提升.我必须添加它,以提醒我的用户先保存脚本,然后在Web浏览器之外独立运行它.
Regarding the "Try saving this script" message, if the user attempts to open the batch script directly from the web browser, browser security might block the elevation. I had to include that to remind my users to save the script first, then run it independently outside the web browser.
无论如何,加盐调味.
这篇关于如何在管理员模式下执行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!