Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Run a function with admin rights


  • This topic is locked This topic is locked
21 replies to this topic

#1 akorx

akorx

    Wayfarer

  • Active Members
  • Pip
  • 74 posts

Posted 03 February 2012 - 02:30 PM

Hi Guys,

Few versions ago there was the RusAsSet function that has now disappered... now : how can i do to run a function (not a external program but a autoit function like regwrite) with admin rights ?

I've tried this but it doesn' t work :

$cmd1="RegWrite(" & "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesUSBSTOR" & "," & "start" & "," & "REG_DWORD" & ",0x00000003)"
;MsgBox(4096,"test",$cmd1)
RunAs("Administrateur","********","********",0,$cmd1)
Exit


=> don't forget to change "administrateur" by "administrator" if you are using an english version of XP !

NB : sorry for my bad english, i'm french :)

Edited by akorx, 03 February 2012 - 02:36 PM.

AkorxMail akorx@yahoo.fr







#2 JLogan3o13

JLogan3o13

    115 and holding

  • MVPs
  • 3,489 posts

Posted 03 February 2012 - 02:36 PM

Hi, akorx. Check out RunAs and #RequireAdmin in the help file.

Programming is like sex...one mistake and you have to support it for a lifetime.

My Scripts:Include Source with Compiled Script, Disk Maintenance for Windows XP, "Deal-A-Day" Sites, Windows Firewall UDF


#3 AdmiralAlkex

AdmiralAlkex

    Here be dragons

  • Active Members
  • PipPipPipPipPipPip
  • 4,606 posts

Posted 03 February 2012 - 02:40 PM

RusAsSet() never did anything like that, it did what RunAs()/RunAsWait() do now.

#4 akorx

akorx

    Wayfarer

  • Active Members
  • Pip
  • 74 posts

Posted 03 February 2012 - 02:54 PM

Thx guys for your help... so try to use my script and see what happens : nothing !!!

There is something that i don't understand : every commands in autoit are written in bold... but when i'm writting "rusas" this command stays "unbold" ("unbold" is the good word ? i don't know if it's an english term...).

Why ?

Edited by akorx, 03 February 2012 - 02:58 PM.

AkorxMail akorx@yahoo.fr

#5 Chimaera

Chimaera

    Sorry .. Where am i again?

  • Active Members
  • PipPipPipPipPipPip
  • 1,846 posts

Posted 03 February 2012 - 03:25 PM

Probably because Rusas does'nt exist as a valid command now, i dont know if it did before.

I checked on my autoit and if you type it nothing shows when you reach Rus.

Admiral With hat has given you the commands earlier, and check the help file for refernce.

If you have a very old script you may have to update the commands to the modern versions

Edit typo

Edited by Chimaera, 03 February 2012 - 03:26 PM.


#6 Melba23

Melba23

    I'm old - what's your excuse?

  • Moderators
  • 19,834 posts

Posted 03 February 2012 - 03:31 PM

akorx,

What version of AutoIt are you using? :)

If you do not know run this line in SciTE:

and tell us what is returned in the lower pane. ;)

M23
StringSize - Automatically size controls to fit text                                                               ExtMsgBox - A user customisable replacement for MsgBox
Toast - Small GUIs which pop out of the Systray                                                                Marquee - Scrolling tickertape GUIs
Scrollbars - Automatically sized scrollbars with a single command                                   GUIFrame - Subdivide GUIs into many adjustable frames
GUIExtender - Extend and retract multiple sections within a GUI                                      NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes
ChooseFileFolder - Single and multiple selections from specified path tree structure      Notify - Small notifications on the edge of the display
Date_Time_Convert - Easily convert date/time formats, including the language               GUIListViewEx - Insert, delete, move, drag, sort and edit ListView items

#7 akorx

akorx

    Wayfarer

  • Active Members
  • Pip
  • 74 posts

Posted 03 February 2012 - 03:38 PM

I've got the last version (3.3.8.1) and my script is a new one.

If i'm starting to write "run", Scite answer :run / runas / runaswait / runwait so "runas" exists !

I dont't understand why it's not bold... but it's not the problem, the actual problem is "how run an autoit function with admin rights ?" !

Edited by akorx, 03 February 2012 - 03:41 PM.

AkorxMail akorx@yahoo.fr

#8 JLogan3o13

JLogan3o13

    115 and holding

  • MVPs
  • 3,489 posts

Posted 03 February 2012 - 03:40 PM

But you stated in your previous post you typed in Rusas, not RunAs. There is no Rusas.

Programming is like sex...one mistake and you have to support it for a lifetime.

My Scripts:Include Source with Compiled Script, Disk Maintenance for Windows XP, "Deal-A-Day" Sites, Windows Firewall UDF


#9 Aipion

Aipion

    Polymath

  • Banned (NOT IN USE)
  • 200 posts

Posted 03 February 2012 - 03:41 PM

Thx guys for your help... so try to use my script and see what happens : nothing !!!

There is something that i don't understand : every commands in autoit are written in bold... but when i'm writting "rusas" this command stays "unbold" ("unbold" is the good word ? i don't know if it's an english term...).

Why ?


It is "RunAs" Not "rusas"

#10 Mikeman27294

Mikeman27294

    Insert Member Title Here

  • Active Members
  • PipPipPipPipPipPip
  • 469 posts

Posted 03 February 2012 - 03:43 PM

I've got the last version (3.3.8.1) and my script is a new one.

If i'm starting to write "run", Scite answer :run / runas / runaswait / runwait so "runas" exists !

I dont't understand why it's not bold... but it's not the problem, the actual problem is "how run an autoit function with admin rights ?" !

http://www.autoitscript.com/autoit3/docs/functions/RunAs.htm
Scite isn't nearly as helpful as the help file. Press F1 when you are using scite. The help file should be your best friend. It will not allow you to run a function, but it will allow you to run a script. If you want to run a function with admin status, I reccommend you use paramaters. For example, you use the parameter -a to have the program run the function. If you don't understand how to do this, I will attach some code in a minute.

Edited by Mikeman27294, 03 February 2012 - 03:46 PM.

Spoiler


#11 Melba23

Melba23

    I'm old - what's your excuse?

  • Moderators
  • 19,834 posts

Posted 03 February 2012 - 03:45 PM

akorx,

I know RunAs exists. ;)

If you type " RunAs(" into your script, do you get the CallTip (the list of parameters) appearing? :)

M23
StringSize - Automatically size controls to fit text                                                               ExtMsgBox - A user customisable replacement for MsgBox
Toast - Small GUIs which pop out of the Systray                                                                Marquee - Scrolling tickertape GUIs
Scrollbars - Automatically sized scrollbars with a single command                                   GUIFrame - Subdivide GUIs into many adjustable frames
GUIExtender - Extend and retract multiple sections within a GUI                                      NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes
ChooseFileFolder - Single and multiple selections from specified path tree structure      Notify - Small notifications on the edge of the display
Date_Time_Convert - Easily convert date/time formats, including the language               GUIListViewEx - Insert, delete, move, drag, sort and edit ListView items

#12 akorx

akorx

    Wayfarer

  • Active Members
  • Pip
  • 74 posts

Posted 03 February 2012 - 03:53 PM

akorx,

I know RunAs exists. ;)

If you type " RunAs(" into your script, do you get the CallTip (the list of parameters) appearing? :)

M23


Yes the list appears... i use RunAs (not "rusas", i have made an error when i have posted my script but on my real scrip it's written "runas").

Edited by akorx, 03 February 2012 - 03:56 PM.

AkorxMail akorx@yahoo.fr

#13 akorx

akorx

    Wayfarer

  • Active Members
  • Pip
  • 74 posts

Posted 03 February 2012 - 03:55 PM

http://www.autoitscript.com/autoit3/docs/functions/RunAs.htm
Scite isn't nearly as helpful as the help file. Press F1 when you are using scite. The help file should be your best friend. It will not allow you to run a function, but it will allow you to run a script. If you want to run a function with admin status, I reccommend you use paramaters. For example, you use the parameter -a to have the program run the function. If you don't understand how to do this, I will attach some code in a minute.


I'm using F1... but what do you mean when you say "use the parameter -a to have the program run the function" ? you are using a program that calls another ? i would like to have all the things in just ONE script .... can you give me an example ?

Edited by akorx, 03 February 2012 - 03:59 PM.

AkorxMail akorx@yahoo.fr

#14 Mikeman27294

Mikeman27294

    Insert Member Title Here

  • Active Members
  • PipPipPipPipPipPip
  • 469 posts

Posted 03 February 2012 - 04:17 PM

I'm using F1... but what do you mean when you say "use the parameter -a to have the program run the function" ? you are using a program that calls another ? i would like to have all the things in just ONE script .... can you give me an example ?

I'm glad you asked. Here it is. As you can see, it is just 1 script.

AutoIt         
For $Repeat = 1 To $CmdLine[0];The $cmdline array is automatically set by autoit. It contains any parameters given to the program when it is run.     ;Hold Shift and press F8 in scite to set parameters. Enter "a" into the first box, without quotes.     MsgBox(0,"",$CmdLine[$Repeat]) ;For debug reasons - shows you what the current parameter is.     If $CmdLine[$Repeat] = "a" Then ;Checks to see that the current parameter is "a"         _AdminFunction()     EndIf Next If @Compiled = 1 Then     RunAs(InputBox("Username", "Insert the username of the administrator", "Administrator"), "", InputBox("Password", "Insert the password of the admin account.", ""), 0, FileGetShortName(@ScriptFullPath) & " a");Runs the compiled script with the a parameter Else     RunAs(InputBox("Username", "Insert the username of the administrator", "Administrator"), "", InputBox("Password", "Insert the password of the admin account.", ""), 0, FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath)& " a");Runs the uncompiled script with the a parameter EndIf Func _AdminFunction()     If IsAdmin() Then;Ensures that the current user is admin as a normal user could run the program with the parameter set (if they have access to the code or try the parameter by pure chance to see what it does)         MsgBox(0,"","Running Function as Admin")         ;You can place your required script here.     Else         MsgBox(0,"","Running Function as Normal User. Function will not attempt")         ;Function is not run be an administrator and so it will exit.     EndIf     Exit EndFunc

I'm actually quite proud of it, myself.
The comments explain what happens, but I will explain it too.
The first thing the program does, is checks all parameters passed to it. If one of them is the parameter "a" then it runs the "_AdminFunction". If not, then it exits the for loop, and asks you for credentials. If the credentials are correct, it runs the program with the specified parameter. If not, then the run function doesn't run the program.
In the "_AdminFunction" it ensures the user is admin, incase by pure chance, the program is run with this parameter by a normal user. This can also be done if the person inserts the credentials for a non-administrator account. If the user is an administrator, it displays the message to say so. If not, again, it displays a message to say they are not.

Please note that I have run this from the guest account, trying both guest credentials, and admin credentials. I tried both compiled and non-compiled, so it should work for you exactly as it worked for me.

Edited by Mikeman27294, 03 February 2012 - 04:19 PM.

Spoiler


#15 Melba23

Melba23

    I'm old - what's your excuse?

  • Moderators
  • 19,834 posts

Posted 03 February 2012 - 04:20 PM

akorx,

I have just reread your initial post:

how can i do to run a function (not a external program but a autoit function like regwrite) with admin rights ?

To the best of my knowledge you cannot run a single function in an AutoIt script with admin rights - you must elevate the whole script with #requireadmin. If it is a relatively simple thing that you want to want to run elevated, then you can compile a separate script containing the necessary code with #requireadmin and use RunAs to run it from your user-level script. :)

As to why RunAs does not appear in the same colour as the other functions - check that "RunAs" exists in this file:

"C:Program FilesAutoIt3SciTEPropertiesau3.keywords.properties"

as this is the file SciTE uses to colour the name in the correct fashion. ;)

M23
StringSize - Automatically size controls to fit text                                                               ExtMsgBox - A user customisable replacement for MsgBox
Toast - Small GUIs which pop out of the Systray                                                                Marquee - Scrolling tickertape GUIs
Scrollbars - Automatically sized scrollbars with a single command                                   GUIFrame - Subdivide GUIs into many adjustable frames
GUIExtender - Extend and retract multiple sections within a GUI                                      NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes
ChooseFileFolder - Single and multiple selections from specified path tree structure      Notify - Small notifications on the edge of the display
Date_Time_Convert - Easily convert date/time formats, including the language               GUIListViewEx - Insert, delete, move, drag, sort and edit ListView items

#16 akorx

akorx

    Wayfarer

  • Active Members
  • Pip
  • 74 posts

Posted 05 February 2012 - 12:56 PM

I'm glad you asked. Here it is. As you can see, it is just 1 script.

AutoIt         
For $Repeat = 1 To $CmdLine[0];The $cmdline array is automatically set by autoit. It contains any parameters given to the program when it is run.     ;Hold Shift and press F8 in scite to set parameters. Enter "a" into the first box, without quotes.     MsgBox(0,"",$CmdLine[$Repeat]) ;For debug reasons - shows you what the current parameter is.     If $CmdLine[$Repeat] = "a" Then ;Checks to see that the current parameter is "a"         _AdminFunction()     EndIf Next If @Compiled = 1 Then     RunAs(InputBox("Username", "Insert the username of the administrator", "Administrator"), "", InputBox("Password", "Insert the password of the admin account.", ""), 0, FileGetShortName(@ScriptFullPath) & " a");Runs the compiled script with the a parameter Else     RunAs(InputBox("Username", "Insert the username of the administrator", "Administrator"), "", InputBox("Password", "Insert the password of the admin account.", ""), 0, FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath)& " a");Runs the uncompiled script with the a parameter EndIf Func _AdminFunction()     If IsAdmin() Then;Ensures that the current user is admin as a normal user could run the program with the parameter set (if they have access to the code or try the parameter by pure chance to see what it does)         MsgBox(0,"","Running Function as Admin")         ;You can place your required script here.     Else         MsgBox(0,"","Running Function as Normal User. Function will not attempt")         ;Function is not run be an administrator and so it will exit.     EndIf     Exit EndFunc

I'm actually quite proud of it, myself.
The comments explain what happens, but I will explain it too.
The first thing the program does, is checks all parameters passed to it. If one of them is the parameter "a" then it runs the "_AdminFunction". If not, then it exits the for loop, and asks you for credentials. If the credentials are correct, it runs the program with the specified parameter. If not, then the run function doesn't run the program.
In the "_AdminFunction" it ensures the user is admin, incase by pure chance, the program is run with this parameter by a normal user. This can also be done if the person inserts the credentials for a non-administrator account. If the user is an administrator, it displays the message to say so. If not, again, it displays a message to say they are not.

Please note that I have run this from the guest account, trying both guest credentials, and admin credentials. I tried both compiled and non-compiled, so it should work for you exactly as it worked for me.

Thx for your code Mikeman27294, it 's not what i really want in this case but i keep it for improve another script that i've got, it's a good script that will help me !
AkorxMail akorx@yahoo.fr

#17 akorx

akorx

    Wayfarer

  • Active Members
  • Pip
  • 74 posts

Posted 05 February 2012 - 01:02 PM

akorx,

I have just reread your initial post:

To the best of my knowledge you cannot run a single function in an AutoIt script with admin rights - you must elevate the whole script with #requireadmin. If it is a relatively simple thing that you want to want to run elevated, then you can compile a separate script containing the necessary code with #requireadmin and use RunAs to run it from your user-level script. :)

As to why RunAs does not appear in the same colour as the other functions - check that "RunAs" exists in this file:

"C:Program FilesAutoIt3SciTEPropertiesau3.keywords.properties"

as this is the file SciTE uses to colour the name in the correct fashion. ;)

M23


I'm not at work but i will verify on monday...

Do you know where i can suggest to developpers of Autoit to try to add this kind of fonction? i think that a lot of people would like to have it.

=> please correct my last expression because i think it's not really english ! what is the good expression ?
AkorxMail akorx@yahoo.fr

#18 Melba23

Melba23

    I'm old - what's your excuse?

  • Moderators
  • 19,834 posts

Posted 05 February 2012 - 01:09 PM

akorx,

Do you know where i can suggest to developpers of Autoit to try to add this kind of fonction? i think that a lot of people would like to have it.

You can ask for additional features to be added to AutoIt via the "Bug Reports and Feature Requests" link on the forum home page. But I would counsel you NOT to ask for this as it will almost certainly be refused. ;)

Why? Because you can very easily run your whole script at the elevated level (Mikeman27294's script just reruns itself elevated ;)) or you can do what I suggested and run a separate script elevated to run specific functions. If you want an example of how to do that take a look at how the RunReqAdmin function in Jos' SciTEConfig does its magic (lines 1230 to 1246 in my version). :)

M23
StringSize - Automatically size controls to fit text                                                               ExtMsgBox - A user customisable replacement for MsgBox
Toast - Small GUIs which pop out of the Systray                                                                Marquee - Scrolling tickertape GUIs
Scrollbars - Automatically sized scrollbars with a single command                                   GUIFrame - Subdivide GUIs into many adjustable frames
GUIExtender - Extend and retract multiple sections within a GUI                                      NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes
ChooseFileFolder - Single and multiple selections from specified path tree structure      Notify - Small notifications on the edge of the display
Date_Time_Convert - Easily convert date/time formats, including the language               GUIListViewEx - Insert, delete, move, drag, sort and edit ListView items

#19 Blue_Drache

Blue_Drache

    Revolutionary Flavour

  • Active Members
  • PipPipPipPipPipPip
  • 3,466 posts

Posted 05 February 2012 - 02:29 PM

Melba: Actually, this kind of request happens about once every 3 months or so. I don't really like the idea, much less think it's possible, to tell the truth, since a program really only asks if you should grant admin privileges once. And windows grants it once... even if there are multiple administration level calls.
Lofting the cyberwinds on teknoleather wings, I am...The Blue DrachePosted ImagePosted Image

#20 Melba23

Melba23

    I'm old - what's your excuse?

  • Moderators
  • 19,834 posts

Posted 05 February 2012 - 02:49 PM

Blue_Drache,

Which is what I said a few posts above. ;)

The post immediately above was explaining why there was no chance of a feature request ever being approved given that it is easy to do with existing code. :)

M23
StringSize - Automatically size controls to fit text                                                               ExtMsgBox - A user customisable replacement for MsgBox
Toast - Small GUIs which pop out of the Systray                                                                Marquee - Scrolling tickertape GUIs
Scrollbars - Automatically sized scrollbars with a single command                                   GUIFrame - Subdivide GUIs into many adjustable frames
GUIExtender - Extend and retract multiple sections within a GUI                                      NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes
ChooseFileFolder - Single and multiple selections from specified path tree structure      Notify - Small notifications on the edge of the display
Date_Time_Convert - Easily convert date/time formats, including the language               GUIListViewEx - Insert, delete, move, drag, sort and edit ListView items




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users