Sign in to follow this  
Followers 0
southboyj

Inserting Variables into RunAs Script

6 posts in this topic

I am trying to execute a remote file using adminstrative credentials. Variables work with other scripts. PSEXEC resides in the same folder as the script and the FILENAME.CMD resdies on the remote computer that the user account has access to. I have tried several combinations of syntax without success. Any advice would be greatly appriciated. Thanks...

;Variables

$UserID = InputBox ("User Name", "Enter User name", "", "")

$Password = InputBox ("Password", "Enter Password","", "X")

$Domain = InputBox ("Domain", "Enter Domain","" , "")

$ComputerName = InputBox ("Computer Name", "Enter Computer Name","", "")

RunAs ($UserID, $Domain, $Password, 0, @ComSpec & " /c " & @ScriptDir & "\PSEXEC.EXE \\" & $Computername "c:\FILENAME.cmd" & "-u" $UserID & "-p" $Password")

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I am trying to execute a remote file using adminstrative credentials. Variables work with other scripts. PSEXEC resides in the same folder as the script and the FILENAME.CMD resdies on the remote computer that the user account has access to. I have tried several combinations of syntax without success. Any advice would be greatly appriciated. Thanks...

;Variables

$UserID = InputBox ("User Name", "Enter User name", "", "")

$Password = InputBox ("Password", "Enter Password","", "X")

$Domain = InputBox ("Domain", "Enter Domain","" , "")

$ComputerName = InputBox ("Computer Name", "Enter Computer Name","", "")

RunAs ($UserID, $Domain, $Password, 0, @ComSpec & " /c " & @ScriptDir & "\PSEXEC.EXE \\" & $Computername "c:\FILENAME.cmd" & "-u" $UserID & "-p" $Password")

Try a couple of tweaks:

1. @ScriptDir might have spaces in it, so quote the executable path.

2. Dropped a space before batch file path.

3. Dropped spaces between parameters.

4. No ampersand for appending $Password.

5. Stray " after $Password

RunAs($UserID, $Domain, $Password, 0, @ComSpec & ' /c "' & @ScriptDir & '\PSEXEC.EXE" \\' & $Computername & ' c:\FILENAME.cmd -u ' & $UserID & ' -p ' & $Password)

I wonder also if you are confused about the context of the RunAs() account. Are you sure the same account is appropriate for running psexec and for psexec to use on the remote computer? If the local account running the script has rights to run psexec, then all you need might be:

Run('"' & @ScriptDir & '\PSEXEC.EXE" \\' & $Computername & ' c:\FILENAME.cmd -u ' & $UserID & ' -p ' & $Password)
The username\password would only be needed on the remote end.

;)

Edit: Oops. Fixed missing string formatting in Run functions per comments below.

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Thanks for keeping me straight. You are absolutely correct....local user account has access to PCEXEC therefore no need to RunAs. However, when I copy & paste code, I get an error:

Run(@ScriptDir & '\PSEXEC.EXE" \\' & $Computername ' c:\FILENAME.cmd -u ' $UserID & ' -p ' & $Password)

Run(^ ERROR

>Exit code: 1

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

he forgot the & before $user and after $computername

try this

Run(@ScriptDir & '\PSEXEC.EXE" \\' & $Computername & ' c:\FILENAME.cmd -u ' & $UserID & ' -p ' & $Password)
Edited by tlman12

Share this post


Link to post
Share on other sites

Worked great. Thanks

Share this post


Link to post
Share on other sites

he forgot the & before $user and after $computername

Oops. Also dropped the preceding '"' on the path (or " need removed after psexec.exe). :evil:

Could also be:

ShellExecute(@ScriptDir & '\PSEXEC.EXE', '\\' & $Computername & ' c:\FILENAME.cmd -u ' & $UserID & ' -p ' & $Password)

Not tested.

;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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