AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Run as Administrator (XP/Vista/7) A_IsAdmin Params [Lib]
Goto page 1, 2, 3  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
shajul



Joined: 15 Sep 2006
Posts: 564

PostPosted: Wed Oct 28, 2009 11:35 am    Post subject: Run as Administrator (XP/Vista/7) A_IsAdmin Params [Lib] Reply with quote

Lib compliant script to Elevate privileges of compiled/non-compiled scripts with/without parameters.

Just put this script to your Standard Library and put this function at the top of the script, as simple as that.
Code:
RunAsAdmin()


Download : RunAsAdmin.ahk


History:
To check if the user has Administrator rights and elevate it if needed by the script, i stumbled upon the help doc, which states that
Quote:
..this would need to be enhanced to work for compiled scripts and/or to pass parameters..
on page http://www.autohotkey.com/docs/Variables.htm#BuiltIn..

The code given is..

Code:
if not A_IsAdmin
{
   DllCall("shell32\ShellExecuteA", uint, 0, str, "RunAs", str, A_AhkPath
      , str, """" . A_ScriptFullPath . """", str, A_WorkingDir, int, 1)
   ExitApp
}


To run for any scripts, compiled or not, with params use this code instead..

Code:
Loop, %0%  ; For each parameter:
  {
    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
}


Edit1: Now it should work with both Basic AHK, Ansi and Unicode Autohotkey_L
Edit2: Shorter code, thanks Lexikos.
Edit3: Lib compliant version.


Also, why not use this code in the help file and docs also, so that any noob like me can benefit?
[/url]


Last edited by shajul on Wed Jan 12, 2011 7:01 am; edited 4 times in total
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Guest






PostPosted: Sun Jan 31, 2010 9:29 am    Post subject: Reply with quote

So that code will not work for compiled (.exe) scripts? What can I do to make it work on compiled scripts?
Back to top
Lexikos



Joined: 17 Oct 2006
Posts: 7295
Location: Australia

PostPosted: Mon Feb 01, 2010 9:49 am    Post subject: Reply with quote

In theory, your script can be simplified as commented below:
Code:
SetWorkingDir %A_ScriptDir%
Loop, %0%  ; No need for the intermediary variable 'param':
    params .= A_Space . """" . %A_Index% . """"
if not A_IsAdmin  ; Check this first to reduce repetition.
{
    if A_IsCompiled
        DllCall("shell32\ShellExecuteA", uint, 0, str, "RunAs", str, A_ScriptFullPath
            , str, SubStr(params,2) , str, A_WorkingDir, int, 1)
    else
        DllCall("shell32\ShellExecuteA", uint, 0, str, "RunAs", str, A_AhkPath
            , str, """" . A_ScriptFullPath . """" . params, str, A_WorkingDir, int, 1)
    ExitApp
}
...however, I haven't tested it. Note the changes I've made to compensate for the extra space at the beginning of the params, and spaces in parameters (i.e. in case the script was invoked as script.ahk "one param" "two params").

I would vote to update the example to demonstrate the difference between compiled and uncompiled scripts, but leave parameters as an exercise for the readers.
Anonymous wrote:
What can I do to make it work on compiled scripts?
I think you missed the point of shajul's post...
Back to top
View user's profile Send private message Visit poster's website
aaffe



Joined: 17 May 2007
Posts: 1002
Location: Germany - Deutschland

PostPosted: Mon Feb 01, 2010 10:29 am    Post subject: Reply with quote

I think the second one must also just Substr:
Code:
SetWorkingDir %A_ScriptDir%
Loop, %0%  ; No need for the intermediary variable 'param':
    params .= A_Space . """" . %A_Index% . """"
if not A_IsAdmin  ; Check this first to reduce repetition.
{
    if A_IsCompiled
        DllCall("shell32\ShellExecuteA", uint, 0, str, "RunAs", str, A_ScriptFullPath
            , str, SubStr(params,2) , str, A_WorkingDir, int, 1)
    else
        DllCall("shell32\ShellExecuteA", uint, 0, str, "RunAs", str, A_AhkPath
            , str, """" . A_ScriptFullPath . """" . SubStr(params,2), str, A_WorkingDir, int, 1)
    ExitApp
}


Edit: Sorry, I saw that the leading space MUST be there in the second one. So forget my changing.
Back to top
View user's profile Send private message
shajul



Joined: 15 Sep 2006
Posts: 564

PostPosted: Tue Nov 02, 2010 4:55 am    Post subject: Updated.. Reply with quote

Just updated my code so that it runs on Autohotkey_L also (unicode)

Also, Lexikos version of the same will also just need the modification of
Quote:
DllCall("shell32\ShellExecuteA"
to be changed to
Quote:
DllCall("shell32\ShellExecute"
as ShellExecuteA expects an Ansi string..

The documentation supplied with Autohotkey_L be changed so that all ansi functions are replaced. I can help in whatever way possible by me.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Lexikos



Joined: 17 Oct 2006
Posts: 7295
Location: Australia

PostPosted: Wed Nov 03, 2010 8:28 am    Post subject: Reply with quote

I'll update the documentation. However, in AutoHotkey_L this is possible:
Code:
if not A_IsAdmin
{
    Run *RunAs "%A_ScriptFullPath%"
    ExitApp
}
This will work with compiled or uncompiled scripts (though the latter only works if AutoHotkey is installed). Unfortunately Run *verb "file" parameters isn't supported... yet.

Edit 2011-01-17: Hmm, apparently doesn't work with uncompiled scripts?? Wonder what I was thinking.


Last edited by Lexikos on Sun Jan 16, 2011 2:45 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
shajul



Joined: 15 Sep 2006
Posts: 564

PostPosted: Wed Nov 03, 2010 8:39 am    Post subject: Good. Reply with quote

I just moved over to AutoHotkey_L, and was updating my scripts to run with it.. The extra features are cool.. Thanks.

Lexikos wrote:
I'll update the documentation. However, in AutoHotkey_L this is possible:
Code:
if not A_IsAdmin
{
    Run *RunAs "%A_ScriptFullPath%"
    ExitApp
}
This will work with compiled or uncompiled scripts (though the latter only works if AutoHotkey is installed). Unfortunately Run *verb "file" parameters isn't supported... yet.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
hotkeyd
Guest





PostPosted: Tue Nov 23, 2010 1:04 pm    Post subject: Reply with quote

Using standard AHK, which version of the above scripts is correct for compiled and un-compiled scripts ?
Back to top
shajul



Joined: 15 Sep 2006
Posts: 564

PostPosted: Tue Nov 23, 2010 1:55 pm    Post subject: for ansi & unicode Reply with quote

hotkeyd wrote:
Using standard AHK, which version of the above scripts is correct for compiled and un-compiled scripts ?


Use this..
Code:
RunAsAdmin:
params := ""
if 0>0
{
    Loop, %0%  ; For each parameter:
    {
        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 A_IsCompiled
{
    if not A_IsAdmin
    {
       DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_ScriptFullPath, str, params , str, A_WorkingDir, int, 1)
       ExitApp
    }
}
Else
{
    if not A_IsAdmin
    {
       DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_AhkPath, str, """" . A_ScriptFullPath . """" . A_Space . params, str, A_WorkingDir, int, 1)
       ExitApp
    }
}
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
hotkeyd
Guest





PostPosted: Tue Nov 23, 2010 2:10 pm    Post subject: Reply with quote

cheers I'll try this later.

Should this allow my script to write ini files and machines where they don't have admin access ?
Back to top
aaffe



Joined: 17 May 2007
Posts: 1002
Location: Germany - Deutschland

PostPosted: Tue Nov 23, 2010 2:28 pm    Post subject: Reply with quote

*Deleted* Sorry, didnt read that you cant overgive parameters via *Runas...
Back to top
View user's profile Send private message
shajul



Joined: 15 Sep 2006
Posts: 564

PostPosted: Tue Nov 23, 2010 3:37 pm    Post subject: Admin Script.. Reply with quote

I have created a scheduled task to run a small script that runs as administrator at logon..
That script spawns all my other scripts, so that they all run with administrator privileges! Smile
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
aaffe



Joined: 17 May 2007
Posts: 1002
Location: Germany - Deutschland

PostPosted: Tue Nov 23, 2010 3:50 pm    Post subject: Reply with quote

Sorry, but this doesnt run for me:
Code:
if not A_IsAdmin
{
    Run *RunAs "%A_ScriptFullPath%"
    ExitApp
}

It says:
Action: RunAs
Verb: RunAs
There is no program bound to this file.

The ahk-file isnt compiled.....does it have to be compiled?
Back to top
View user's profile Send private message
hotkeyd
Guest





PostPosted: Tue Nov 23, 2010 6:45 pm    Post subject: Re: for ansi & unicode Reply with quote

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

return

msgbox %A_IsAdmin%

Running the above on XP logged in with admin rights returns a msgbox with 1

Running as a compiled exe on XP with admin rights returns a msgbox with 1

Running on Win 7 as a compiled exe returns nothing.

Win 7 Right click run as administrator, return msgbox with 1.

I thought this would elevate the script/exe to have admin rights ?
Is that wrong ? or have I missed something ?
Back to top
hotkeyd
Guest





PostPosted: Tue Nov 23, 2010 7:14 pm    Post subject: Reply with quote

Code:
Loop, %0%  ; For each parameter:
  {
    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
}


this seems to work Smile
Back to top
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group