If you want a VBScript to run a program using RunAs DOS command, go straight to the bottom of the blog. If you want to read the build-up for the final product, keep reading.
Time to time I came across I wanted to run a script or program as administrative privileges. The few needs are like,
- Install a program or do something on client machines via logon script
- Run a program using different account
Duh! RunAs command. Sure RunAs command works, but using VBScript??? You can find a VBScript example for running a program using RunAs command like below,
set oShell= Wscript.CreateObject("WScript.Shell")
oShell.Run "runas /user:domainaccount ""Notepad.exe""", 2 ‘Note 2 means minimized Window
WScript.Sleep 100
oShell.Sendkeys "password~" ‘Note the ~ character in the end. ~ means ENTER
Wscript.Quit
What’s wrong with the above script? After all it should work as expected. You know who the villain for above script. Guess…..OK. It’s attention hungry programs. When you run this script, it might send the password keys to wrong window, while you running along with zillion programs in foreground and background. So what to do?
I rewrite the script few times and tested the script running along many programs. (Take ‘many programs’ as WinDVD, IE, Downloading a file in IE, Notepad, Command prompt and Active Directory Users and Computers). The final product is below,
Dim oShell
set oShell= Wscript.CreateObject("WScript.Shell")
oShell.Run "runas /user:domainuser ""program.exe""",2 ‘Note 2 means minimized Window
WScript.Sleep 100
For i = 0 to 10
oShell.Popup "Opening Program.exe…",3,"Administrator",0+64
‘3 means 3 seconds, 0+64 means OK button with Information Icon
Activated = oShell.AppActivate ("runas")
WScript.Sleep 10
If Activated = 0 Then
oShell.Sendkeys "password~" ‘Note the ~ in the End..~ means ENTER
Exit For
End If
Next
Wscript.Quit
Few Tips and Tricks:
- If you want to run a VBScript in runas, try running RunAs /user:domainuser "WScript script1.vbs"
- RunAs command run only executable files, so *.VBS or *.JS will not run
- If you want to run an *.MSC file, try running RunAs /user:domainuser "MMC test.msc"
- If you want tor run an Non-Executable file (other than above), try creating a DOS batch file and run the batch file using RunAs
- It’s not really safe to leave the VBScript file command location. Passwords are visible to any eyes. Try Encoding the VBScript using VBScript Encoder (can be downloaded from Microsoft).
Alternatives
For non-believer of my above VBScript sample, here is the nice list of alternatives.
http://www.commandline.co.uk/sanur_unsupported/index3.html
http://www.wingnutsoftware.com/erunas.html
http://www.shareup.com/RunAs_Professional-download-12339.html
set WshShell = WScript.CreateObject(“WScript.Shell”)
WshShell.run “runas /user:domain\user %comspec%” ‘Open command prompt
WScript.Sleep 1000
WshShell.SendKeys “pass” ‘send password
WshShell.SendKeys “{ENTER}”
WScript.Sleep 1000
WshShell.SendKeys “C:\Windows\System32\dsa.msc” ‘run de exe file
WshShell.SendKeys “{ENTER}”
WshShell.SendKeys “exit” ‘Close command prompt
WshShell.SendKeys “{ENTER}”
set wshshell = nothing
Than You Guys, just what I was looking For.
Nice DaY
Script not working
yes it is not working ..both the code are working same..