Search code examples
batch-filecmdrunas

RunAs Prompt for credentials


I'm trying to write a batch script that will do a RunAs for any given windows service (using explorer.exe to test) for any given user that can be input at time of running. What I currently have is this.

set /p Var1="Domain = "

set /p Var2="Username = "

set /p Var3="Service to open= "

RunAs /user:%Var1%\%Var2% "%Var3%" /seperate

On command line, this seems to work ok (with coded values instead of variables) but in a batch file it just seems to repeat itself, without opening what it's been specified (it doesn't even prompt for a password, so I can only presume its not even trying). Any idea why it's looping and what I can do to stop it?

Cheers


Solution

  • Hi @Gary Barnett i created something similar to this awhile back.

    Here is an example;

    @ECHO OFF
    set /P Domainn=Enter Domain Name: 
    set /P Usern=Enter Username: 
    set userunas=runas /user:%domainn%\%usern% "
    
    :optionmenu
    CLS
    ECHO 1 - Control.exe
    ECHO 2 - MSinfo32.exe
    ECHO 3 - Explorer.exe    
    ECHO q - Quit
    ECHO.
    set /P optionnum=Enter command number: 
    GOTO option%optionnum%
    
    :option1
    %userunas%control.exe
    goto optionmenu
    
    :option2
    %userunas% %commonprogramfiles%\micros~1\MSInfo\msinfo32.exe"
    goto optionmenu
    
    :option3
    %userunas%explorer.exe
    goto optionmenu
    
    :optionq
    EXIT
    

    Note: You must run the batch file as Administrator (right-click -> Run as Administrator) or it wont work.