|
Answer» Hi all,
I want to run the “Run As” command in DOS in a manner where I give password too in a single command line along with user id and domain. Please let me know if this is possible and provide me the syntax too.
Thanks & Regards, SANKAR. Runas only works in NT based OS. Not MS-DOS. You cannot pass password on command line as you describe. Consider SU.exe in NT Resource Kit.
http://www.dynawell.com/support/ResKit/winnt.asp
Runas Allows a user to run specific tools and programs with different permissions than the user's current logon provides
Syntax
runas [{/profile|/noprofile}] [/env] [/netonly] [/smartcard] [/showtrustlevels] [/trustlevel] /user:UserAccountName program
Parameters
/profile : Loads the user's profile. /profile is the default.
/no profile : Specifies that the user's profile is not to be loaded. This allows the application to load more quickly, but it can also cause a malfunction in some applications.
/env : Specifies that the current network environment be used instead of the user's local environment.
/netonly : Indicates that the user information specified is for remote access only.
/smartcard : Indicates whether the credentials are to be supplied from a smartcard.
/showtrustlevels : Lists the /trustlevel options.
/trustlevel : Specifies the level of authorization at which the application is to run. Use /showtrustlevels to SEE the trust levels available.
/user:UserAccountName : Specifies the name of the user account under which to run the program. The user account format should be [emailprotected] or Domain\User.
program : Specifies the program or command to run using the account specified in /user.
/? : Displays help at the command prompt.
Examples
To start an instance of the command prompt as an administrator on the local computer, type:
runas /user:localmachinename\administrator cmd
When prompted, type the administrator password.
To start an instance of the Computer Management snap-in using a domain administrator account called companydomain\domainadmin, type:
runas /user:companydomain\domainadmin "mmc %windir%\system32\compmgmt.msc"
When prompted, type the account password.
To start an instance of Notepad using a domain administrator account called user in a domain called domain.microsoft.com, type:
runas /user:[emailprotected] "notepad my_file.txt"
When prompted, type the account password.
To start an instance of a command prompt window, saved MMC console, Control Panel item, or program that will administer a server in another forest, type:
runas /netonly /user:domain\username "command"
domain\username must be a user with sufficient permissions to administer the server. When prompted, type the account password.
------------------------------------------------------------------------------
Runas REPLACEMENT
RUNAS in Windows 2000 is nice, but not very script friendly. Unlike SU from the NT Resource Kit, you can't pass a password. Here is an alternative.
You can use the following script to execute a command via RUNAS. You might find this helpful for yourself, or run it from a batch file (although the password will be in clear text). Another alternative is to hardcode the command, username and password in the script, then use the Script Encoder (download from http://msdn.microsoft.com/scripting) to encode it. You run the script the same way, the file will just have a different extension: cscript vbrunas.vbe. Then if you have repeatable admin tasks you or your users, just execute the script.
Code: [Select]'Start of Script 'VBRUNAS.VBS 'v1.2 March 2001 'Jeffery Hicks '[emailprotected] http://www.quilogy.com 'USAGE: cscript|wscript VBRUNAS.VBS Username Password Command 'DESC: A RUNAS replacement to take password at a command prompt. 'NOTES: This is meant to be used for local access. If you want to run a command 'across the network as another user, you must add the /NETONLY switch to the RUNAS 'command.
' ********************************************************************************* ' * THIS PROGRAM IS OFFERED AS IS AND MAY BE FREELY MODIFIED OR ALTERED AS * ' * NECESSARY TO MEET YOUR NEEDS. THE AUTHOR MAKES NO GUARANTEES OR WARRANTIES, * ' * EXPRESS, IMPLIED OR OF ANY OTHER KIND TO THIS CODE OR ANY USER MODIFICATIONS. * ' * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED IN A SECURED LAB * ' * ENVIRONMENT. USE AT YOUR OWN RISK. * ' *********************************************************************************
On Error Resume Next dim WshShell,oArgs,FSO
set oArgs=wscript.Arguments
if InStr(oArgs(0),"?")<>0 then wscript.echo VBCRLF & "? HELP ?" & VBCRLF Usage end if
if oArgs.Count <3 then wscript.echo VBCRLF & "! Usage Error !" & VBCRLF Usage end if
sUser=oArgs(0) sPass=oArgs(1)&VBCRLF sCmd=oArgs(2)
set WshShell = CreateObject("WScript.Shell") set WshEnv = WshShell.Environment("Process") WinPath = WshEnv("SystemRoot")&"\System32\runas.exe" set FSO = CreateObject("Scripting.FileSystemObject")
if FSO.FileExists(winpath) then 'wscript.echo winpath & " " & "verified" else wscript.echo "!! ERROR !!" & VBCRLF & "Can't find or verify " & winpath &"." & VBCRLF & "You must be running Windows 2000 for this script to work." set WshShell=Nothing set WshEnv=Nothing set oArgs=Nothing set FSO=Nothing wscript.quit end if
rc=WshShell.Run("runas /user:" & sUser & " " & CHR(34) & sCmd & CHR(34), 2, FALSE) Wscript.Sleep 30 'need to give time for window to open. WshShell.AppActivate(WinPath) 'make sure we grab the right window to send password to WshShell.SendKeys sPass 'send the password to the waiting window.
set WshShell=Nothing set oArgs=Nothing set WshEnv=Nothing set FSO=Nothing
wscript.quit
'************************ '* Usage Subroutine * '************************ Sub Usage() On Error Resume Next msg="Usage: cscript|wscript vbrunas.vbs Username Password Command" & VBCRLF & VBCRLF & "You should use the full path where necessary and put long file names or commands" & VBCRLF & "with parameters in quotes" & VBCRLF & VBCRLF &"For example:" & VBCRLF &" cscript vbrunas.vbs quilogy\jhicks luckydog e:\scripts\admin.vbs" & VBCRLF & VBCRLF &" cscript vbrunas.vbs quilogy\jhicks luckydog " & CHR(34) &"e:\program files\scripts\admin.vbs 1stParameter 2ndParameter" & CHR(34)& VBCRLF & VBCRLF & VBCLRF & "cscript vbrunas.vbs /?|-? will display this message."
wscript.echo msg
wscript.quit
end sub 'End of Script Contrex gave a good answer, but to simplify, (assuming you are running under Windows 2000 / XP) the syntax would be something like: Code: [Select]runas /user:[emailprotected] program.exeIf you need to include the password, try to echo and pipe it to the command like: Code: [Select]echo password|runas /user:[emailprotected] program.exe
|