2

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!

Brad Conyers
  • 1,161
  • 4
  • 15
  • 24
  • Why not changing the path back to the batch file location? Ex `pushd "%~dp0"` – jeb Jul 17 '12 at 06:46
  • possible duplicate of [How can I auto-elevate my batch file, so that it requests from UAC admin rights if required?](http://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-admin-rights) – parvus Jul 29 '14 at 09:26

2 Answers2

0

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.

Community
  • 1
  • 1
Maximus
  • 10,751
  • 8
  • 47
  • 65
0

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.

James K
  • 4,005
  • 4
  • 20
  • 28