Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Run as Administrator (XP/Vista/7) A_IsAdmin Params [Lib]


  • Please log in to reply
49 replies to this topic
aaffe
  • Members
  • 1045 posts
  • Last active: Jan 16 2014 01:32 PM
  • Joined: 17 May 2007
Thanks, this works for me too.

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 :)



Visioneer
  • Members
  • 287 posts
  • Last active: Sep 17 2017 10:07 PM
  • Joined: 19 Nov 2007
So, if I just want it simple with no params used, would this
function work for compiled/not compiled, ahk basic/ahk_L,
all windows versions?

;at top of script
RunAsAdmin()

RunAsAdmin() 
{
ShellExecute := A_IsUnicode ? "shell32\ShellExecute":"shell32\ShellExecuteA" 
if not A_IsAdmin 
{ 
    If A_IsCompiled 
       DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_ScriptFullPath, str, A_WorkingDir, int, 1) 
    Else 
       DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_AhkPath, str, """" . A_ScriptFullPath . """", str, A_WorkingDir, int, 1) 
    ExitApp 
}
}


shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006

So, if I just want it simple with no params used, would this
function work for compiled/not compiled, ahk basic/ahk_L,
all windows versions?


Yes, it will work in AHK / AHK_L, upwards (and including) Win 2000

.

  • Guests
  • Last active:
  • Joined: --
Good to know.

Visioneer
  • Members
  • 287 posts
  • Last active: Sep 17 2017 10:07 PM
  • Joined: 19 Nov 2007
Thanks shajul.

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
Standard Library compliant function added - see first post!

Newbie007
  • Members
  • 82 posts
  • Last active: Sep 30 2011 03:12 PM
  • Joined: 01 Dec 2010
hey guys. Ihave putten run as admin function
RunAsAdmin() {
global
Loop, %0%  ; For each parameter:
    params .= A_Space . %A_Index%
local ShellExecute
ShellExecute := A_IsUnicode ? "shell32\ShellExecute":"shell32\ShellExecuteA"
if not A_IsAdmin
    {
      A_IsCompiled
        ? DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_ScriptFullPath, str, params , str, A_WorkingDir, int, 1)
        : DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_AhkPath, str, """" . A_ScriptFullPath . """" . A_Space . params, str, A_WorkingDir, int, 1)
      ExitApp
    }
}
and want tp know what will be with xp users?

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
just a few posts up..

Yes, it will work in AHK / AHK_L, upwards (and including) Win 2000.


If i've seen further it is by standing on the shoulders of giants

my site | ~shajul | WYSIWYG BBCode Editor

fragman
  • Members
  • 1591 posts
  • Last active: Nov 12 2012 08:51 PM
  • Joined: 13 Oct 2009
Question: Does anyone have an inverse function? Or a method to temporarily lower your permissions and regain them without showing UAC dialog?

Right now I'm using a somewhat limited method I wrote in C++ that uses the dispatch interface of explorer to launch something from the desktop explorer process, but it's limited in some areas (the ShellExecute there doesn't return the PID for example) and I would like to have a pure AHK solution.

ABCza
  • Members
  • 132 posts
  • Last active: Jan 04 2015 01:02 AM
  • Joined: 03 Jun 2008

Question: Does anyone have an inverse function? Or a method to temporarily lower your permissions and regain them without showing UAC dialog?

Right now I'm using a somewhat limited method I wrote in C++ that uses the dispatch interface of explorer to launch something from the desktop explorer process, but it's limited in some areas (the ShellExecute there doesn't return the PID for example) and I would like to have a pure AHK solution.


If you need to run some code without the admin token you can add it inside the "if not A_IsAdmin" statement. It will be run before the elevation prompt.

A solution to your needs could be to drop the admin token but i think that you must start a new process. However take a look at the "DropMyRights" source code, maybe you can find something useful...
All my scripts/snippets are released under the WTFPL: http://sam.zoy.org/wtfpl/COPYING

ABCza
  • Members
  • 132 posts
  • Last active: Jan 04 2015 01:02 AM
  • Joined: 03 Jun 2008

Question: Does anyone have an inverse function? Or a method to temporarily lower your permissions and regain them without showing UAC dialog?

Right now I'm using a somewhat limited method I wrote in C++ that uses the dispatch interface of explorer to launch something from the desktop explorer process, but it's limited in some areas (the ShellExecute there doesn't return the PID for example) and I would like to have a pure AHK solution.


If you need to run some code without the admin token you can add it inside the "if not A_IsAdmin" statement. It will be run before the elevation prompt.

A solution to your needs could be to drop the admin token but i think that you must start a new process. However take a look at the "DropMyRights" source code, maybe you can find something useful...


I was intrigued by the argument and checking by myself i found that the correct way to do what you ask is to use the "CreateRestrictedToken" winapi function. I made it to work, get a look here: <!-- m -->http://www.autohotke... ... 727#484727<!-- m -->
All my scripts/snippets are released under the WTFPL: http://sam.zoy.org/wtfpl/COPYING

panofish
  • Members
  • 179 posts
  • Last active: Apr 24 2014 03:24 PM
  • Joined: 05 Feb 2007
Excuse my ignorance.

I am always administrator on my boxes, but if I were a user with limited privileges and ran a script which contained this library function call... Would this really enable that script to run with full admin privileges without providing an administrator login and password?

I feel like I must be missing something obvious here.

ABCza
  • Members
  • 132 posts
  • Last active: Jan 04 2015 01:02 AM
  • Joined: 03 Jun 2008

Excuse my ignorance.

I am always administrator on my boxes, but if I were a user with limited privileges and ran a script which contained this library function call... Would this really enable that script to run with full admin privileges without providing an administrator login and password?

I feel like I must be missing something obvious here.


This is supposed to work with UAC, don't know how it behaves with XP :)
All my scripts/snippets are released under the WTFPL: http://sam.zoy.org/wtfpl/COPYING

panofish
  • Members
  • 179 posts
  • Last active: Apr 24 2014 03:24 PM
  • Joined: 05 Feb 2007
I guess I don't understand UAC on windows 7 well enough.
I do have a problem with windows 7 machines in the office that don't like AHK scripts and I suspect that disabling UAC might be the fix.

I wonder if RunAsAdmin would fix this problem with WIN7?

ABCza
  • Members
  • 132 posts
  • Last active: Jan 04 2015 01:02 AM
  • Joined: 03 Jun 2008

I guess I don't understand UAC on windows 7 well enough.
I do have a problem with windows 7 machines in the office that don't like AHK scripts and I suspect that disabling UAC might be the fix.

I wonder if RunAsAdmin would fix this problem with WIN7?


It's simple. Basically there are 2 situations you have to take into account with UAC on:

1) You use a standard user account. You run any programs as standard user and if you elevate (like with using this script), UAC asks you the credential of an admin user.

2) You use an administrator account. Your programs are run in your context but with stripped rights (like if you were a standard user). If you elevate, UAC asks your confirmation and eventually restore your stripped rights.

About your machines, any problem is relative to the single script... Try to ask for help in the relative forum section.
All my scripts/snippets are released under the WTFPL: http://sam.zoy.org/wtfpl/COPYING