如何为 AutoHotkey 脚本添加管理员权限? [英] How to add administrator privileges to AutoHotkey script?
本文介绍了如何为 AutoHotkey 脚本添加管理员权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将其编译为可执行文件,但要打开它,我必须右键单击并按以管理员身份运行".我希望它每次运行时都请求管理员权限,但是怎么做?
I compiled it to an executable, but to open it I have to right-click and press "Run as administrator". I want it to request admin privileges each time I run it, but how to do it?
我不能这样做:
因为当我将它复制到第二台计算机时它不起作用.
Because then it doesn't work when I copy it to a second computer.
推荐答案
尝试将其添加到自动执行部分(脚本顶部):
Try adding this to the auto-execute section (top of the script):
; If the script is not elevated, relaunch as administrator and kill current instance:
full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!S)"))
{
try ; leads to having the script re-launching itself as administrator
{
if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" /restart
else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
}
ExitApp
}
并重新编译脚本.
有关详细信息,请阅读 https://autohotkey.com/docs/commands/Run.htm#RunAs.
For more details read https://autohotkey.com/docs/commands/Run.htm#RunAs.
这篇关于如何为 AutoHotkey 脚本添加管理员权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文