Jump to content

Inserting Variables into RunAs Script


Recommended Posts

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")

Link to comment
Share on other sites

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
Link to comment
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

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

  • Recently Browsing   0 members

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