From command line on win7, how do I launch a proce

2019-07-21 16:50发布

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. The problem is that if I right-click and choose "run as administrator" it changes my current directory so ./src no longer works. If I disable UAC on my machine then everything runs fine. Thank you!

2条回答
放我归山
2楼-- · 2019-07-21 17:24

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.

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.

Save the following as as .js file, like 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:

wscript StartAdmin.js

To launch from a particular directory and launch a batch file, change line 2 in StartAdmin.js to something like:

WshShell.SendKeys("^{esc}cmd /C "cd %userprofile% & batchfile.bat"^+{ENTER}");

/C switch tells it to run the commands, then close the command-line window.
/K would leave the command window open after it exited the batch file.
To help you understand the SendKeys commands:

+=[Shift Key]
^=[Control Key]
{esc}=[Escape Key]
{enter}=[Enter Key]

To learn more about using CMD.EXE, type 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.

查看更多
放我归山
3楼-- · 2019-07-21 17:37

Look here: 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.

查看更多
登录 后发表回答