Jump to content

Run a function with admin rights


akorx
 Share

Recommended Posts

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

AkorxMail akorx@yahoo.fr

Link to comment
Share on other sites

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

AkorxMail akorx@yahoo.fr

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Moderators

akorx,

What version of AutoIt are you using? :)

If you do not know run this line in SciTE:

ConsoleWrite(@AutoItVersion & @CRLF)

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

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

AkorxMail akorx@yahoo.fr

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Moderators

akorx,

I know RunAs exists. ;)

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

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

AkorxMail akorx@yahoo.fr

Link to comment
Share on other sites

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

AkorxMail akorx@yahoo.fr

Link to comment
Share on other sites

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.

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
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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 Drache

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...