How to use Autohotkey commands with admin rights?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Wanderer_82
Posts: 1
Joined: 19 Jan 2017, 09:49

How to use Autohotkey commands with admin rights?

19 Jan 2017, 09:53

Hi together

I try to use the commands "FileDelete" and "FileCreateShortcut" but they only work, if I start the program as administrator. I know that there's the possibility to use the "Run as" command, but this one only works for executing other programs, I guess. So my question is... is there a way to use these commands "FileDelete" and "FileCreateShortcut" as an administrator without starting the entire program as administrator? I can also write the username and password of the administrator inside the programs. So any help would be very appreciated.

Thanks,
Thomas
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to use Autohotkey commands with admin rights?

20 Jan 2017, 04:01

No.

The script is a program. You can have the script run itself as administrator, as you would for any other program. See A_IsAdmin.
Guest

Re: How to use Autohotkey commands with admin rights?

21 Jan 2017, 17:47

Hm okay.

I try to delete a file with administrator rights. I start cmd (comspec) after I used RunAs to introduce the administrator credentials. Starting the device manager for example works like a charm. But when I try to delete a file it always says, "Access denied" in the cmd window, even though I have the administrator rights. When I do it "by hand", so not using the autohotkey script, I don't get the error and the file is deleted.

Can someone help me what's going on here?

Thanks,
Thomas
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to use Autohotkey commands with admin rights?

21 Jan 2017, 19:10

Chances are that you are on a system with UAC, where RunAs is not sufficient to elevate a process. Again,
I wrote:See A_IsAdmin.
... by which, I mean look it up in the help file/documentation index.

... and follow the links from there.

Or you could read the remarks in the documentation for RunAs, which tell you the same thing.
User avatar
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

Re: How to use Autohotkey commands with admin rights?

21 Jan 2017, 19:47

add this head of script

RunAsAdmin()


and this function

Code: Select all

RunAsAdmin() {
	Loop, %0% {
		param := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
		params .= A_Space . param
    }
	ShellExecute := A_IsUnicode ? "shell32\ShellExecute":"shell32\ShellExecuteA"
	if not A_IsAdmin
	{
		If A_IsCompiled
			DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_ScriptFullPath, str, params , str, A_WorkingDir, int, 1)
		Else
			DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_AhkPath, str, """" . A_ScriptFullPath . """" . A_Space . params, str, A_WorkingDir, int, 1)
		ExitApp
	}
}
Wanderer82

Re: How to use Autohotkey commands with admin rights?

22 Jan 2017, 04:12

@Lexikos

Problem is, even running the entire script or compiled program as administrator doesn't work.

I'll try the suggestion by masheen although I don't understand it because I'm a beginner. Do I have to change something to my needs?

Thanks,
Thomas
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to use Autohotkey commands with admin rights?

23 Jan 2017, 20:24

I try to use the commands "FileDelete" and "FileCreateShortcut" but they only work, if I start the program as administrator.
Problem is, even running the entire script or compiled program as administrator doesn't work.
These two statements are contradictory. Which is it?
Guest

Re: How to use Autohotkey commands with admin rights?

28 Jan 2017, 12:39

I tried two different ways. First I used the command from autohotkey which works when I start the program as administrator. I then hoped that starting cmd as an administrator from the script would do the trick. When I try to use cmd from the script even starting the program as an administrator the file isn't deleted. I don't understand that...
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to use Autohotkey commands with admin rights?

28 Jan 2017, 16:41

"used the command from autohotkey [...] when I start the program as administrator" and "use cmd from the script [...] starting the program as an administrator" mean the same thing, yet you say one "works" and the other "doesn't".

If you can't give a better explanation, post both versions of your script.
Guest

Re: How to use Autohotkey commands with admin rights?

28 Jan 2017, 19:47

If I use the commands "FileDelete" and "FileCreateShortcut", then it will work when I run the script as administrator.

If I use the commands "comspec" and "del xy" it won't work even when I run the script as administrator.
Guest

Re: How to use Autohotkey commands with admin rights?

29 Jan 2017, 06:45

Yeah I know, but I was looking for a way to not have it to run as administrator. Is there no way to put the administrator password inside the script so it will work?
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to use Autohotkey commands with admin rights?

29 Jan 2017, 20:46

If the file permissions only allow an administrator to delete the file, you must run as administrator. There is no getting around it, only different methods of running as administrator.

See the pointer I gave in my first reply, which leads to having the script re-launching itself as administrator.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: alawsareps, mebelantikjaya, mikeyww, RussF and 305 guests