Jump to content

Simple Script - RunAs not working

Recommended Posts

Trying to do a very simple script that will run a batch file as a specified account.  The batch will remove one icon from the desktop and replace it with another one.  This has to be done in a limited user context, thus I am trying to use autoit to make a quick exe so that the password for an account with elevated rights will not be sitting on the device in plain text.

The script is a total of three lines:

$uname = "Username"
$pwd = "Password"

RunAs ($uname, @ComputerName, $pwd, 0, "c:\testme\loopit.cmd")

This debugs cleanly, but when I compile and run it, it generates the error Line 3 Error: Error Parsing function call.

Any help at all would be appreciated

EDIT/UPDATE:  I tried changing the third line as follows:

RunAsWait($uname, @ComputerName, $pwd, 0, @ComSpec & " /c" & "c:\testme\loopit.cmd")

And it no longer generates the function call error, but it also does not actually run the script. :(

I should add that if I do this the manual way - command prompt, run as, launch the .cmd that way, everything works as expected.  I just need AutoIT to elevate privleges for the script without exposing the password.


Edited by vyperhand
Additional Information Discovered.
Link to post
Share on other sites

If you have an error in the compiled script, the error is present in the non-compiled.
 The code you posted is good, but it's probably not the real one, isn't it ?

That is actually everything except the comments - with the actual username and password removed, of course.

Link to post
Share on other sites

The second example is missing a space between the "/c and the following quote. Also, it should be written like this instead of concatenating the /c and the command, make it one string.

RunAsWait($uname, @ComputerName, $pwd, 0, @ComSpec & " /c c:\testme\loopit.cmd")

You could always get rid of the batch file and just use AutoIt to run the commands that are in it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to post
Share on other sites

i suspect that (a) you need to RequireAdmin in the EXE and (b) you need to ensure that the batch process is allowed to run with same inherited permissions or elevated permissions.

Also, see the difference between ControlSend and Send for some background info

Note that Windows can suppress processes where the user is not currently logged on.

AV software can also suppress such processes.

I would really like to hear of your solution, as I struggle with something similar.



See post #20 by AdamUL

Edited by Skysnake
found good answer


Why is the snake in the sky?

Link to post
Share on other sites

You could always get rid of the batch file and just use AutoIt to run the commands that are in it.

I was debating that - I was thinking I could put in the guts of the batch file as a series of @ComSpec strings, but I was hoping not to have to.  The .cmd is being provided from another department, and I was hoping to be able to just call it as-is rather than re-construct it each time they update the script.  However, I have yet to get this working as expected any other way, so I will try that next.

@SkySnake - this is in a thinclient /windows embedded environment with EWF enabled, so no AV and no UAC as such.  AdamUL's solution looks promising though.  Will add that to the list.

I have temporarily brute-forced a solution using PSExec to run the batch as system, but I would rather not have that be the final solution. 

Link to post
Share on other sites

My environment is more user driven :)

How about this?  Read the .bat file, parse the contents and simply feed it line  by line to the AutoIt script to run as a set of "RunWait $var " commands, until the batch process is complete.

So you do not actually run the batch file, you have AutoIt execute the batch line by line...  End user should be none the wiser and could easily resolve this? 

Following this topic...


Why is the snake in the sky?

Link to post
Share on other sites
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version:
 Author:        Skysnake

 Script Function:
    Demo on reading content of batch file, parsing to ComSpec

    Motivation - AutoIt EXE can readily acquire elevate privileges.  Elevating
    batch not always possible or easy.  This method pushes batch commands
    through the AutoIt EXE, and does not launch a batch child process.

#ce ----------------------------------------------------------------------------

#include <MsgBoxConstants.au3>

; First example
; RunWait external batch file

; requires a small batch file show.bat
; --------------------------- batch file content start

echo show.bat content > showme.txt
echo %date% %time% >> showme.txt
echo Example One >> showme.txt
echo %computername% >> showme.txt
ipconfig >> showme.txt

; --------------------------- batch file content end

Local $BatName="show.bat"

; Open the file for reading and store the handle to a variable.
Local $hFileOpen = FileOpen($BatName,0)
If $hFileOpen = -1 Then
    MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file." & @CRLF & $hFileOpen,15)

RunWait(@ComSpec & " /c " & $BatName, @ScriptDir, @SW_MAXIMIZE)
MsgBox(0,"First Example Done","Inspect the output file now")
FileDelete("showme.txt") ; delete the output file

; Second example
; RunWait lines read from btach file
; do not create external process

While 1
    ; Read the fist line of the file using the handle returned by FileOpen.
    Local $sFileRead = FileReadLine($hFileOpen)
    If $sFileRead="" Then ExitLoop ; if blank file in external batch, then exit loop
    MsgBox(0,"Line Read",$sFileRead,1)
    RunWait(@ComSpec & " /c " & $sFileRead, @ScriptDir, @SW_MAXIMIZE)
    ; Close the handle returned by FileOpen.

Simply replace Run/RunAs/RunWait as required.

File Read To Array, and then simply pushing the entire array to the ComSpec may be neater. But this works. :) 

Edited by Skysnake
Spelling fixed


Why is the snake in the sky?

Link to post
Share on other sites
; Third example
; RunWait lines read from batch file to Array
; do not create external process


Func Example()
    ; Read the current script file into an array using the filepath.
    Local $aArray = FileReadToArray($BatName)
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file.
        For $i = 0 To UBound($aArray) - 1 ; Loop through the array.

            RunWait(@ComSpec & " /c " & $aArray[$i], @ScriptDir, @SW_MAXIMIZE)
EndFunc   ;==>Example



Why is the snake in the sky?

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...