Jump to content
Sign in to follow this  
Mikeman27294

Run internal function in second process.

Recommended Posts

Mikeman27294

Hey guys,

I just came across this thread;

Where the poster asked how to run an internal function with admin rights. As far as I am aware, there has never been a script example of this. As is debated for the next 10 or so posts, this is not actually running the function with admin rights, it re-runs the file with admin credentials, runs the function with the parameter "a" (This can be changed) and then runs the function, which checks that the user is admin. What happens if they are admin, and what happens if they are not admin can, ofcourse, be changed. So I'll cut the chit-chat, here is the example:

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 have tried this from a guest account on my computer (with and without credentials) and it has worked perfectly. I also tried running it as both compiled, and uncompiled, and again, it worked exactly as expected. It might not be something that people will use all the time, but it's here for anybody who does need it, and I am quite happy with it.

A few other ideas I have had since writing this scipt, as to how it may communicate involve writing to the STDOUT stream at the end of the function, and having the calling part of the script read it, or using mailslot which is inter-script communication.

Edited by Mikeman27294

Share this post


Link to post
Share on other sites
Melba23

Mikeman27294,

Your thread title is somewhat misleading. You are not "running a function with admin rights" - you are "checking the user is an admin before running the function". :)

As I just explained in the thread to which you linked, you cannot run a single function as an admin - it is the whole script or nothing. If you want just part of the script run as an admin then you need to use a separate script using #requireadmin and Run it when required. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Mikeman27294

Well yes and no. I see where you are coming from. Yes, it does check that the user has admin rights. I'll agree, you aren't really running the function with admin rights (it can be run by anybody) but this workaround would definately work. It is a bit more work than #RequireAdmin but it does the job.

Share this post


Link to post
Share on other sites
Melba23

Mikeman27294,

It is a bit more work than #RequireAdmin but it does the job.

Yes, but it does an entirely different job. ;)

Using @RequireAdmin elevates the whole script by demanding that the OS gives it Admin rights via the standard dialog. Your script above is checking that the user is an Admin before running part of the script - it does nothing to help a user who is not logged in as an Admin. :)

If you want to see how the "Run a different script with #RequireAdmin" works - look at how the RunReqAdmin function in Jos' SciTEConfig does its magic (lines 1230 to 1246 in my version). ;)

M23

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Mikeman27294

Fair enough. What I mean to say is that it still ensures that the function is run as an admin. I think that maybe it would be better if rather than re-running the script, it requests a privileges escalation, and then runs the script. That way, it would be more able to do communications between variables, and not use a mailbox or file to communicate between processes though. Just a though.

Share this post


Link to post
Share on other sites
Melba23

Mikeman27294,

I think that maybe it would be better if rather than re-running the script, it requests a privileges escalation, and then runs the script

Which is exactly what #RequireAdmin does. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Beege

What I mean to say is that it still ensures that the function is run as an admin.

But this doesn't ensure that the function is run as admin because if the script dosnt have admin rights, it just exits.

Share this post


Link to post
Share on other sites
Mikeman27294

Mikeman27294,

Which is exactly what #RequireAdmin does. :)

M23

But then that requires the entire script is run with admin credentials.

I mean that it escalates the privilege requirements to run the function only, and then it restores them once the function has been run, rather than just run the whole script as an administrator.

But this doesn't ensure that the function is run as admin because if the script dosnt have admin rights, it just exits.

No, the script runs first. It then asks for the username and password to an admin account. It then re-runs with admin rights, and then runs the function. Edited by Mikeman27294

Share this post


Link to post
Share on other sites
Melba23

Mikeman27294,

I mean that it escalates the privilege requirements to run the function only, and then it restores them once the function has been run, rather than just run the whole script as an administrator.

NO IT DOES NOT! :)

For the last time - you CANNOT run a single function within a script as an adminstrator. You can only run the ENTIRE script at a certain level. Your example in the first post just checks that the user running the script has admin rights before running the function - if the user does not have admin rights the function is not run.

I have explained how you can run other scripts at admin level so that you do not have to elevate the entire script and have even told you where to find a good example. Do you think Jos (an AutoIt Dev) would go to all that trouble to write a file from SciTEConfig if there was an easy solution? ;)

You have now had both a Mod and an MVP tell you that you are not doing what you believe you are doing. Please stop confusing people by claiming that your script does something which AutoIt CANNOT do. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Mikeman27294

Ok, well obviously I am not conveying everything exactly as I understand it (I am horrible with explainations). I know you cannot run just the 1 function with elevated privileges, but I will try and explain what is going through my head.

The script first runs without admin (unless you run it from an admin account)

It requests the credentials

It re-runs the script with the admin credentials, but in the script only runs that one function

That is what I mean to say, sorry I didn't say it the first time, I thought everyone understood what I meant (each time I posted). I hope that clears out what I mean, if not I will just give up trying to explain how I think because I am terrible at it. I am sorry, I don't mean to annoy anybody. Please don't be offended, as I said, the fault is mine, I just am no good at explaining.

And I think that in saying this: "I think that maybe it would be better if rather than re-running the script, it requests a privileges escalation, and then runs the script" everybody got confused as towards what I meant:

Mikeman27294,

Which is exactly what #RequireAdmin does. ;)

M23

Because after that, I replied with

But then that requires the entire script is run with admin credentials.

I mean that it escalates the privilege requirements to run the function only, and then it restores them once the function has been run, rather than just run the whole script as an administrator.

And you replied with:

Mikeman27294,

NO IT DOES NOT! :)

Obviously, that 1 quote blew the whole idea of what I was saying out of the water.

What I meant by saying that quote, is that a better way of going about writing the script, if it were possible, would be to have the script request admin privileges before running that function, and then after running that function, it restores the original privileges, rather than run the script again with admin privileges, and then run the function. I hope that clears that up?

Edited by Mikeman27294

Share this post


Link to post
Share on other sites
Melba23

Mikeman27294,

Hurray! We understand each other! :)

Please add that explanation to the first post - then anyone else who stumbles on this thread in future will not have to wait until post #10 to find it ;)

M23

P.S. And I am not offended in the slightest - I just wanted to make sure we all understand what is going on. ;)


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Mikeman27294

Mikeman27294,

Hurray! We understand each other! :)

Please add that explanation to the first post - then anyone else who stumbles on this thread in future will not have to wait until post #10 to find it ;)

M23

P.S. And I am not offended in the slightest - I just wanted to make sure we all understand what is going on. ;)

Will do. I'm glad that's all cleared up. I did understand, as I said, I am just horrible at explainations. That's what I was trying to say the whole time but for obvious reasons it came across wrong. I'm glad that you weren't offended (and hopefully Beege wasn't either). I will add the correct description to the first post, and try and think of a more appropreate name (something else I am not good at but I will try my hardest).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • Amixg
      By Amixg
      Hi! I have another problem with AutoIT.  You see, I'm still the one who's just starting out with this great programming language. I have a problem with AutoIT, it seems not to recognize the "StringReplace" function even when it is by default. The code was made half in KODA and half of my own. It is a software programmed for the Italian language (I am Italian) and translate the SMS language into Italian correct. Would you help me? Thanks in advance.
       
      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> $hull = "hello" StringReplace($hull, "hello", "hellx") $Form1 = GUICreate("Linguaggio SMS A Italiano", 507, 498, 192, 124) $Edit1 = GUICtrlCreateEdit("", 24, 40, 465, 177) GUICtrlSetData(-1, "") $Label1 = GUICtrlCreateLabel("Inserisci qui il testo che non riesci a capire:", 24, 8, 205, 17) $Label2 = GUICtrlCreateLabel("Ecco il testo tradotto(non toccare questo campo):", 27, 237, 237, 17) $Edit2 = GUICtrlCreateEdit("", 24, 264, 465, 137) GUICtrlSetData(-1, "") $Button1 = GUICtrlCreateButton("Traduci", 104, 424, 249, 49) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 StringReplace(GUICtrlGetData($Label1), "qlc", "qualcuno") StringReplace(GUICtrlGetData($Label1), "qls", "qualcosa") StringReplace(GUICtrlGetData($Label1), "ke", "che") StringReplace(GUICtrlGetData($Label1), "x", "per") StringReplace(GUICtrlGetData($Label1), "pls", "per favore") StringReplace(GUICtrlGetData($Label1), "tu6", "tu sei") StringReplace(GUICtrlGetData($Label1), "zzz", "mi fai dormire") StringReplace(GUICtrlGetData($Label1), "hagn", "Buonanotte") StringReplace(GUICtrlGetData($Label1), "tvtb", "Ti voglio tanto bene") StringReplace(GUICtrlGetData($Label1), "tat", "Ti amo tanto") StringReplace(GUICtrlGetData($Label1), "lafs", "Amore a prima vista") StringReplace(GUICtrlGetData($Label1), "fli?", "Flirtiamo?") StringReplace(GUICtrlGetData($Label1), "msidt", "Mi sono innamorato di te") StringReplace(GUICtrlGetData($Label1), "ba", "Bacio") StringReplace(GUICtrlGetData($Label1), "midi", "Mi dispiace.") StringReplace(GUICtrlGetData($Label1), "ntm", "Non ti merito") StringReplace(GUICtrlGetData($Label1), "tdp", "Togliti dai piedi.") StringReplace(GUICtrlGetData($Label1), "amò", "Amore") StringReplace(GUICtrlGetData($Label1), "ap", "A presto!") StringReplace(GUICtrlGetData($Label1), "cmq", "comunque") StringReplace(GUICtrlGetData($Label1), "cvd", "Ci vediamo dopo") StringReplace(GUICtrlGetData($Label1), "Tvb", "Ti voglio bene") StringReplace(GUICtrlGetData($Label1), "nn", "non") StringReplace(GUICtrlGetData($Label1), "risp", "rispondimi") StringReplace(GUICtrlGetData($Label1), "cel", "cellulare") StringReplace(GUICtrlGetData($Label1), "dom", "qualcosa") StringReplace(GUICtrlGetData($Label1), "nm", "numero") StringReplace(GUICtrlGetData($Label1), "fv", "favore") StringReplace(GUICtrlGetData($Label1), "-male", "meno male") StringReplace(GUICtrlGetData($Label1), "disc", "discoteca") StringReplace(GUICtrlGetData($Label1), "se#", "settimana") StringReplace(GUICtrlGetData($Label1), "+ o -", "più o meno") StringReplace(GUICtrlGetData($Label1), "ts", "torno subito") StringReplace(GUICtrlGetData($Label1), "tvtbcoa", "Ti voglio tanto bene come amica") EndSwitch WEnd  
    • nacerbaaziz
      By nacerbaaziz
      can we  create a list box with  Columns?
      welcome everybody
      Dears I have a question if you let me
      can we  create a list box with  Columns?
      i know we can create a list view with Columns
      but my question is about the list box
      I'm waiting your responses
      Thank you in advance
    • rkr
      By rkr
      Hi, I have a software which has a 'run file'-ie; when i double click on that file, the input file of the software is run by the software. I wish to double click/execute this run file using autoit. how do i achieve this. i opened the software and it shows the input file and run file in same folder, i will put my autoitscript file too in same location. how do i simulate double clicking the run file (using its file name, say - abc.run ). I entered the following lines to make software window active - it became active, dont know how to go forward
      Winactivate("program title")
      winwaitactivate("program title")
      controlclick("program title","abc.run",1000,"primary",2) ; 1000 is ctrl id
    • Karnalsyn
      By Karnalsyn
      As with a lot of programming, the less manual repetition done to achieve the same outcome...the better.
      I package programs for network deployment at the organization I work for. Sometimes these applications require verbose messaging to the user, and sometimes by special request we have to turn that off and deploy silently.
      The way I currently handle these 2 scenarios is this.
      I have a function that builds the splashwin display screen to variable size depending on message length, and displays the desired message to the end user.
      Then at each point throughout my script as I need to make the user aware of what is currently happening, I inject a splashwin function call with the unique message pertaining to that specific event. ie. "Installing Microsoft Office, please wait..."
      In effort to build a more universal script to handle any type of request submitted. I've incorporated a switch command of '/silent'
      If cmdlineraw detects the usage of that switch when the script is triggered it sets a variable flag, ie. $silent = "on"
      Then at every splashwin call I make, I'm prefixing it with an IF statement that checks for the flag, and then does NOT display the message if that flag is "on". Or does display it if its not. Has worked for me just fine. But while I grow my programming skills and look for more streamlined ways of handling areas I find myself creating repetition. I'm curious if I can relocate that flag check. Taking it out of every single splashwin call, and injecting it into the function itself.
      Example of how I currently display or hide the splashwin based on command line...
      If StringInStr($cmdlineraw, "/silent", 0) Then
          $SilentSwitch = "ON"
      EndIf
      If $SilentSwitch = "OFF" Then SplashWin($Uninstalling & $ProductInfo[1][0])
      Some scripts have dozens of those splashwin calls littered throughout, so Is this the best approach I can take already? Or as stated earlier, can I pull the IF statement away from the splashwin call. And left the function as a whole either activate or deactivate as a result of the command line check.
    • tremolux66
      By tremolux66
      Initial Problem
      I've written several scripts with the following sequence:
      Execute a program using Run w/stdout+stderr captured Typically processes all the files in one directory tree to populate a second tree Execute a second program (also with Run) to monitor the products of the first program and Display a progress bar (percentage of output files complete) Also monitor the first program's process and exit when it terminates The script then calls ProcessWaitClose (no timeout) on the first program's process and Checks the first program's results Kills the monitor program if it hasn't already exited on its own. Sometimes, ProcessWaitClose returns 1 with @error = 0 and @extended = 0xCCCCCCCC (actually, 0xFFFFFFFFCCCCCCCC), which seems ambiguous: the documentation says that @error = non-zero and @extended = 0xCC... means an invalid PID (unclear what the return value is), and 1 is returned for non-existent processes (but no mention of @extended). The 1/0/0xCC... result seems to occur when the first program exits very quickly (with or without an error). Since the exit value is not available, the script scans the program's output and tries to determine whether it ran successfully. This has gotten complicated and unreliable.
      Partial Fix
      I've now implemented a much simpler approach that works for most cases:
      Modify the monitor program so that it ignores the other program's process (the monitor always gets killed by the script anyway) Execute the monitor program first using Run, then execute the processing program with RunWait When RunWait returns, the child process exit value is available, so the script can ignore its output (which isn't available anyway) If the monitor program is still running, kill it. Remaining Issue
      However, there are still a couple of cases where it's necessary to get both the exit value from the processing program and its output. Since RunWait doesn't capture stdout and stderr for the parent script, it's looking like I'll have to call RunWait and redirect the 2 streams to a temp file and then scan it. Also, to do the redirect, I think I'll have to use @ComSpec to execute the processing program, which adds an undesired layer.
      Does anybody have a better (cleaner) way to handle these cases?
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.