[英]From command line on win7, how do I launch a process as administrator with elevated UAC
I have a program which requires Administrative privileges that I want to run from a batch file. 我有一个程序,该程序需要从批处理文件运行的管理特权。 What command can I run from command line will run my program with administrative privileges?
我可以从命令行运行什么命令,将以管理特权运行程序? I'm okay with the pop-up window asking for permission.
我可以在弹出窗口中请求许可。 Additionally, the file needs to be able to run from anywhere on a computer so additional files are run from ./src.
此外,该文件需要能够在计算机上的任何位置运行,以便其他文件可以从./src运行。 The problem is that if I right-click and choose "run as administrator" it changes my current directory so ./src no longer works.
问题是,如果我右键单击并选择“以管理员身份运行”,它将更改我的当前目录,因此./src不再起作用。 If I disable UAC on my machine then everything runs fine.
如果我在计算机上禁用了UAC,则一切运行正常。 Thank you!
谢谢!
Look here: https://superuser.com/a/269750/139371 在这里看: https : //superuser.com/a/269750/139371
elevate seems to be working, calling 提升似乎正在工作,打电话
C:\Utils\bin.x86-64\elevate.exe -k dir
executes dir
in the "current directory" where elevate
was called. 执行
dir
中的“当前目录”,其中elevate
被调用。
This is tough, Microsoft provides no utility to do this (mostly because giving a batch file that ability breaks security), except for RunAs, and that requires that the Administrator account be activated. 这很困难,Microsoft不提供实用程序来执行此操作(主要是因为提供了破坏安全性的批处理文件),但RunAs除外,并且这要求激活Administrator帐户。
There IS a JScript program that can do something similar, by using SendKeys to open the Start menu and type cmd[CTL]+[SHIFT]+[ENTER] which will launch a Command-Line shell. 有一个JScript程序可以执行类似的操作,方法是使用SendKeys打开“开始”菜单,然后键入cmd [CTL] + [SHIFT] + [ENTER],这将启动命令行外壳程序。
Save the following as as .js file, like StartAdmin.js: 将以下内容另存为.js文件,例如StartAdmin.js:
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys("^{esc}cmd^+{ENTER}"); The equivilent of [CTRL]+[ESC] cmd [CTRL]+[SHIFT]+[ENTER]
To run StartAdmin.js from a batch file, you will need the following line: 要从批处理文件运行StartAdmin.js,您将需要以下行:
wscript StartAdmin.js
To launch from a particular directory and launch a batch file, change line 2 in StartAdmin.js to something like: 要从特定目录启动并启动批处理文件,请将StartAdmin.js中的第2行更改为以下内容:
WshShell.SendKeys("^{esc}cmd /C "cd %userprofile% & batchfile.bat"^+{ENTER}");
/C switch tells it to run the commands, then close the command-line window. / C开关告诉它运行命令,然后关闭命令行窗口。
/K would leave the command window open after it exited the batch file. / K退出批处理文件后,将使命令窗口保持打开状态。
To help you understand the SendKeys commands: 为了帮助您理解SendKeys命令:
+=[Shift Key]
^=[Control Key]
{esc}=[Escape Key]
{enter}=[Enter Key]
To learn more about using CMD.EXE, type CMD /? 要了解有关使用CMD.EXE的更多信息,请键入CMD /? at the command prompt.
在命令提示符下。
This is a very untidy and ugly way to do it, but it's the only way I know how using only the tools that come with Windows. 这是一种非常不整洁和丑陋的方法,但这是我知道如何仅使用Windows附带的工具的唯一方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.