Sign in to follow this  
Followers 0
sulfurious

How to use RunAs with ShellExecute

13 posts in this topic

#1 ·  Posted (edited)

You can't. You must start the script with admin rights. You can have the script prompt the user for admin rights by using #RequireAdmin or ShellExecute() with the "runas" verb. On Vista either of these two methods passes through UAC and runs the script with the proper elevated credentials.

Quite simply RunAsSet() did not work how most people thought it did and now they think they are missing functionality that never worked in the first place.

Valik, or whomever knows, how do you get the RunAs verb into ShellExecute? I have tried both

RunAs('user','domain','pass',int,ShellExecute('app.exe'))

and

ShellExecute(RunAs('user','domain','pass',int,'app.exe'))

and neither work. Actually using RunAs within ShellExecute delivers a return value of 0, so the return is there but desired effect is not working.

I have working RunAs usually with logon flags @ 0 or 1. So I don't have a problem making it work as itself is supposed to. Short of either reading the HKCR (yuck) to find the file extension or creating an .ini which is basically making available what is already in the regeistry, how can I do this? If I knew all the extensions to use, I can use RunAs with like '"c:\program files\winrar\winrare.exe" "c:some docs\some zip.rar"' and this works just fine.

Also, I tried a RunAs with an @Comspec and the 'Start' command, but this does not appear to work for all extensions the way ShellExecute does.

So, does anyone have an example to bash across my head and jolt the 'doh! I should have known that' effect to me?

Thanks.

Sul.

Edited by sulfurious

Share this post


Link to post
Share on other sites



Works fine for me:

$sPass = 1
RunAs(@UserName, @ComputerName, $sPass, 0, @ComSpec & " /c start c:\test.txt", @SystemDir, @SW_HIDE)

:mellow:

Yes indeed Start does work, but not in all instances. At least that is what I have found anyway. I have used it but ShellExecute is a more robust method it seems. If I am wrong about this I would love for someone to explain why. I will try to post some of the issues I had with it when I get home tonight.

Just for the interest of learning though I would really like to know how to get ShellExecute to work with RunAs like Valik eludes to.

Thanks for the reply Rasim..

Sul.

Share this post


Link to post
Share on other sites

Just for the interest of learning though I would really like to know how to get ShellExecute to work with RunAs like Valik eludes to.

May be by re-starting your script with RunAS().

If Not IsAdmin() Then
    RunAs(@UserName, @ComputerName, $sPass, 0, @AutoItExe, @SystemDir, @SW_HIDE)
    Exit
EndIf

ShellExecute("myfile.txt", "", @ScriptDir, "edit")

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

Yes, that is a good idea, starting the script with privelages. I should have stated that I know I can start the script with privelages, but that I do not wish to. What I am doing is creating a very simple little tool for down and dirty RunAs. The idea is simple. There are many better apps out there such as SuDoWin or SuRun that are nice and all, but I don't need that kind of functionality. I just want an compiled .exe that can open a file with admin rights, via a small drag and drop window, command line parameter, or a drag and drop on the .exe icon itself.

I have found that using strict rules for calling via command line, ensuring the existence of "quotes" works. But I will assume that my family may not always do that. So rather than giving them instructions that they won't follow anyway, I want to use shellexecute.

Using Start does work, but depending on the call, quotes may or may not exist, and indeed may or may not be needed. Using CmdLineRaw provides a full path, but this is not always the method in invokation in this case.

To better help understand the result, the reason is this. I wish to educate more friends and family on the LUA portion of XP. This can be a troublesome for those used to admin rights. I have played with using a modified GPO, and this does work. Also using SRP works. But to be transparent to them, and not involve the more 'complicated' issues with for example SuRun, and to NOT have to use the real RunAs, I want a small way. My idea to give them a .exe on the desktop. The can drop an item on the .exe, and it used cmdlines. They can execute the app, and a small window for drag/drop opens. They can also use a shortcut/HLKM-run parameter. All this is easy to do. I have an ini file with thier encrypted password/username to use with the RunAs command.

While this is nothing extra-ordinary, there are complications yet in the logic of handling all matters of quotes. I can code up more routines to do the checking. But I noticed Valiks mention of using the RunAs with ShellExecute, and that really got my interest, as I cannot get it to work. A puzzle now that is driving me crazy to solve.

Thanks for the reply Danny35d. New lines of thought are always welcome for me.

Sul.

Share this post


Link to post
Share on other sites

May be this will help you out or get you start it.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

If Not IsAdmin() Then
    RunAs(@UserName, @ComputerName, $sPass, 0, @AutoItExe, @SystemDir, @SW_HIDE)
    Exit
EndIf

$MainGUI = GUICreate("RunAs", 342, 121, -1, -1, -1, $WS_EX_ACCEPTFILES)
GUICtrlCreateLabel("Browse or Drag and Drop a Program.", 40, 20, 260, 17)
GUICtrlCreateLabel("Open:", 0, 40, 33, 17)
$FullPath = GUICtrlCreateInput("", 40, 38, 289, 21)
GUICtrlSetState($FullPath, $GUI_DROPACCEPTED)
$Ok = GUICtrlCreateButton("Ok", 96, 88, 75, 25, 0)
$Cancel = GUICtrlCreateButton("Cancel", 176, 88, 75, 25, 0)
$Browser = GUICtrlCreateButton("Browser...", 256, 88, 75, 25, 0)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $Cancel
            Exit
        Case $Browser
            $message = 'Choose A Program.'
            $FilePath = FileOpenDialog($message, "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", "All (*.*)", 3, '', $MainGUI)
            If @error Then $FilePath = ''               
            GUICtrlSetData($FullPath, $FilePath)
        Case $Ok
            $sProgram = GUICtrlRead($FullPath)
            If $sProgram = '' Then
                MsgBox(48,"RunAs","You need to browse or drag and drop a program.")
                ContinueLoop
            EndIf
            ShellExecute($sProgram, '', @WorkingDir)
            Exit
    EndSwitch
WEnd

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

Did I say you could use the RunAs() function and the ShellExecute() function together? No I did not. I said you could use the "runas" verb with ShellExecute(). It has nothing to do with the built-in function RunAs(), which you would know if you actually bothered to pay attention that I didn't refer to it as the RunAs() function! Simple English.

ShellExecute(@ComSpec, "", @SystemDir, "runas")

Share this post


Link to post
Share on other sites

Hey guys what u think about this? post ur comments, not tested yet.

Func restartme()
    $line = "intPID = " & @AutoItPID &" ' PID of the process to terminate " & @CRLF & 'strComputer = "."' & @CRLF & 'set objWMIProcess = GetObject("winmgmts:\\" & strComputer & _' & @CRLF & Chr(34) & "\root\cimv2:Win32_Process.Handle='" & Chr(34) & " & intPID & " & Chr(34) & "'" & chr(34) & ")" & @CRLF & "intRC = objWMIProcess.Terminate()"  & @CRLF &  'Set WshShell = WScript.CreateObject("WScript.Shell")' & @CRLF & 'WshShell.Run "' & @ScriptName & '", 9'
    FileDelete(@ScriptDir&"\react.vbs")
    FileWrite(@ScriptDir&"\react.vbs",$line)
;~  ShellExecute(@ScriptDir & "\react.vbs","",@ScriptDir)
RunAs(@UserName, @ComputerName, "<user pass goes here>", 0,@ScriptDir&"\react.vbs")
EndFunc

Share this post


Link to post
Share on other sites

Hey guys what u think about this? post ur comments, not tested yet.

Func restartme()
    $line = "intPID = " & @AutoItPID &" ' PID of the process to terminate " & @CRLF & 'strComputer = "."' & @CRLF & 'set objWMIProcess = GetObject("winmgmts:\\" & strComputer & _' & @CRLF & Chr(34) & "\root\cimv2:Win32_Process.Handle='" & Chr(34) & " & intPID & " & Chr(34) & "'" & chr(34) & ")" & @CRLF & "intRC = objWMIProcess.Terminate()"  & @CRLF &  'Set WshShell = WScript.CreateObject("WScript.Shell")' & @CRLF & 'WshShell.Run "' & @ScriptName & '", 9'
    FileDelete(@ScriptDir&"\react.vbs")
    FileWrite(@ScriptDir&"\react.vbs",$line)
;~  ShellExecute(@ScriptDir & "\react.vbs","",@ScriptDir)
RunAs(@UserName, @ComputerName, "<user pass goes here>", 0,@ScriptDir&"\react.vbs")
EndFunc
I think it's stupid. Why are you using VBS?

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Cuz i thought that would be easy cus VBS will be as other user, and then VBS executes script using user used to execute VBS?

or how else could script restart itself? or it can be done without restart of script?

Edited by au3scr

Share this post


Link to post
Share on other sites

The same way you started the VBS as another user. Duh.

Share this post


Link to post
Share on other sites

Did I say you could use the RunAs() function and the ShellExecute() function together? No I did not. I said you could use the "runas" verb with ShellExecute(). It has nothing to do with the built-in function RunAs(), which you would know if you actually bothered to pay attention that I didn't refer to it as the RunAs() function! Simple English.

ShellExecute(@ComSpec, "", @SystemDir, "runas")
I knew you would 'give it to me straight' Valik. I got stuck thinking RunAs instead of 'runas'.

Ah well this is not the answer I was hoping for anyway, but it is nice to find out that there is more to the verb than the help file says. What can one use in the verb parameter? Other than those 4 listed + runas?

Thanks for clearing the matter up. Appreciate it.

Sul.

Share this post


Link to post
Share on other sites

Thanks for the mock-up Danny35d. That works well but still a little too much for what I have in mind. The Start command does work, but you have to use it a little differently when using command line parameters.

It must be used like this

RunAs($USER,@ComputerName,$PASSWORD,0,@ComSpec & ' /c Start "" "' & $CmdLine[1] & '"',@ScriptDir,@SW_HIDE)

I have not used start much except in batch files, so I did not understand that when you pass a fully qualified path with quotes, the command parameters for Start interperets this as the "Title". Kind of silly that to use a path with spaces requires quotes, yet when you give quotes it is seen as the cmd window 'title'. Oh well, by including an extra "" it works anyway. You would think that the 'Title' would be the last parameter to make it easier to send a path with spaces.

I also got the chance to use Assoc and Ftype to verify wether a file type is associated with anything before even calling the RunAs/Start line. This should help those who don't understand what a file type is at least not be confused by why thier .cus file won't open.

Sul.

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  
Followers 0