Page 1 of 2

Run my exe as admin without prompt?

Posted: Wed May 23, 2018 8:49 am
by Dude
On Microsoft Windows, how can a user run my exe as an admin, when logged in as a standard user, without getting the UAC prompt every time? And when the PC boots up with my app, the UAC prompt appears for it, which is stupid because the user will be expecting my app to be running after they've made their morning coffee.

I know it can be done with a batch file (I do it that way for "Recuva" by Piriform), but is that the right way to do this? Or do I make a "helper exe" that launches the main exe with admin rights?

Thanks for any advice.

Re: Run my exe as admin without prompt?

Posted: Wed May 23, 2018 9:16 am
by RSBasic
It is always with prompt.
But you can register your application as a Windows service, then you always have administrators and system rights without prompt.
UAC is only necessary once when creating the Windows service.

Re: Run my exe as admin without prompt?

Posted: Wed May 23, 2018 9:36 am
by forumuser
I know it can be done with a batch file
If you mean that you create a task via schtasks from that .bat file?
Yeah, that's the correct way to do it

Re: Run my exe as admin without prompt?

Posted: Wed May 23, 2018 10:18 am
by Dude
@Forumuser: Yes, the batch file looks like this:

Code: Select all

runas.exe /user:Limited-PC\Administrator /savecred Recuva.exe
So the first time I ran that batch file, it asked for my admin name and password, then it remembers it ("save credentials"). Now I just double-click that shortcut to run Recuva without ever getting the annoying UAC prompt.

[Edit] So, I just tried this with my PureBasic exe and it works. Nope, see new post below. :(

Re: Run my exe as admin without prompt?

Posted: Wed May 23, 2018 10:24 am
by RSBasic
That's a possibility, too.
If "save credentials" works, then it is the better solution.

Re: Run my exe as admin without prompt?

Posted: Sun Dec 30, 2018 3:31 am
by Dude
Using a batch file is NOT the solution after all, as it no longer works. How do other people make their exes run with admin rights? Does the user have to be logged in as admin all the time? Or are they just accepting the UAC prompt for the app every time their PC boots for the day?

Re: Run my exe as admin without prompt?

Posted: Sun Dec 30, 2018 7:45 am
by forumuser
Again: Use schtasks to register your app as a task!

Re: Run my exe as admin without prompt?

Posted: Sun Dec 30, 2018 10:38 am
by Michael Vogel
Hm, if you want to run a program without needing to do interaction you have some chances to do this using WMIC (if enabled on your PC):

wmic /node:10.0.0.1 /user:Admin /password:SuperSecure process call create "C:\Program.exe"

Re: Run my exe as admin without prompt?

Posted: Sun Dec 30, 2018 12:34 pm
by Dude
Michael Vogel wrote:wmic /node:10.0.0.1 /user:Admin /password:SuperSecure process call create "C:\Program.exe"
Thanks Michael, but that doesn't work. :(

@forumuser, I tried to create a scheduled task but that fails too, and also requires admin rights to create the task in the first place, so that's not really a viable solution.

From what I've read online, "runas" doesn't allow credentials to be saved anymore for security reasons. :shock: Apparently you'll always need to physically enter the admin password at some point now.

So the best I could manage was a VBS script I found to run my exe in elevated mode and prompt the user for the admin password. This has two benefits: the user can choose to run my exe alone to use it with limited rights, or run it from the VBS script to use it with admin rights. (If I set "Request administrator" in compiler options, then the user doesn't get a choice on how to launch it).

The VBS code I found was as simple as this (saved as "MyApp.vbs"):

Code: Select all

Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "MyApp.exe", , , "runas", 1

Re: Run my exe as admin without prompt?

Posted: Sun Dec 30, 2018 1:04 pm
by firace
@Dude - not sure if I fully understood your issue, but you may want to give ElevateAs a try:

https://www.trustprobe.com/fs1/apps.html

Re: Run my exe as admin without prompt?

Posted: Sun Dec 30, 2018 1:30 pm
by Dude
Nope, ElevateAs played havoc with my PC and I had to reboot. It was like it was locked in an infinite loop trying to run my exe. Couldn't even kill it in the Task Manager because it kept restarting itself with a different process ID. :shock:

Re: Run my exe as admin without prompt?

Posted: Sun Dec 30, 2018 2:35 pm
by firace
Dude wrote:Nope, ElevateAs played havoc with my PC and I had to reboot. It was like it was locked in an infinite loop trying to run my exe. Couldn't even kill it in the Task Manager because it kept restarting itself with a different process ID. :shock:
Hmm that's rather weird. I've been using it for a while, without any trouble.
It does trigger a UAC prompt, but at least you can pass the password on the command line.

Re: Run my exe as admin without prompt?

Posted: Sun Dec 30, 2018 6:26 pm
by skywalk
Dude wrote:
Michael Vogel wrote:wmic /node:10.0.0.1 /user:Admin /password:SuperSecure process call create "C:\Program.exe"
Thanks Michael, but that doesn't work. :(
Can you explain? wmic should be available on most Windows PC's.

Code: Select all

wmic /node:localhost /user:Admin /password:xyz process process call create "C:\Program.exe"

Re: Run my exe as admin without prompt?

Posted: Sun Dec 30, 2018 11:25 pm
by forumuser
I tried to create a scheduled task but that fails too
And it fails because of what?
and also requires admin rights to create the task in the first place, so that's not really a viable solution.
Em, what? It is a viable solution to let the user enter the password on each single start of the .exe (which is a tedious process) but not just a single time to create the task? *baffled*

Re: Run my exe as admin without prompt?

Posted: Mon Dec 31, 2018 2:55 am
by Dude
firace wrote:Hmm that's rather weird. I've been using it for a while, without any trouble.
It does trigger a UAC prompt, but at least you can pass the password on the command line.
It never gave me a UAC prompt, and my PC's mouse pointer became the "wait" circle that kept flickering on/off very fast. My exe didn't launch. Checked Task Manager, and "Elevateas.exe" kept disappearing and reappearing with a new PID. Because my mouse was hosed, I had to reboot as I couldn't click on anything. :(
skywalk wrote:Can you explain? wmic should be available on most Windows PC's.

Code: Select all

wmic /node:localhost /user:Admin /password:xyz process call create "C:\Program.exe"
Tried the above from a batch file and my exe doesn't launch at all. A command box appears for a moment and then closes. Used the right user/pass combo for it.
forumuser wrote:And it fails because of what?
The scheduled task gets created, and my exe is (apparently) run with admin rights at logon, but it seems to run "silently" in that no window is displayed, it doesn't respond to its hotkeys, its system tray icon doesn't appear, etc. I can see it in the Task Manager and the user name is the LIMITED user name instead of ADMIN user name, and it says "Enabled" for UAC virtualization; however despite all this I can't actually use it. :(

Here's what I used to add it; maybe you can spot the problem, or provide a working alternative?

Code: Select all

; This procedure MUST be run with admin rights to work!

Procedure RunAtLogin(taskname$,exe$,username$="",password$="",elevated=0)
  p$="/Create /TN "+Chr(34)+taskname$+Chr(34)+" /SC ONLOGON /TR "+exe$+" /RU "+username$+" /RP "+password$
  If elevated=1
    p$+" /RL HIGHEST"
  EndIf
  ok=RunProgram("schtasks.exe",p$,"",#PB_Program_Hide)
  ProcedureReturn ok
EndProcedure

Debug RunAtLogin("Run exe at login","c:\path\to\myapp.exe","admin-name","admin-password",1)