1.

Solve : run as in VBS?

Answer»

I am trying to make a VBS that will prompt for a password and use the run-as command for a specific exe. so far I have this but it keeps giving me issues.

Code: [Select]Option explicit
Dim oShell
set oShell= Wscript.CreateObject("WScript.Shell")

oShell.Run "runas /noprofile /user:[domain\user] ""[program]"""

WScript.Sleep 1000

objShell.SendKeys "PASSWORD"

objShell.SendKeys "{enter}"

Wscript.Quit

I am new to vbs but trying to learn. any help would be awesome.

thanks,
WayneDon't know your OS, but if you have XP Pro or above you can use the ScriptPW object:

Code: [Select]Set objPassword = CreateObject("ScriptPW.Password")
WScript.StdOut.Write "Please enter your password:"

strPassword = objPassword.GetPassword()
Wscript.Echo
Wscript.Echo "Your password is: " & strPassword

If you have ACCESS to a XP Pro machine, you can borrow a copy. It will work on any NT OS. File name is ScriptPW.dll. Put in a directory and register: regsvr32 ScriptPW.dll. Note: script must be run with cscript not wscript

Code: [Select]Set oShell= Wscript.CreateObject("WScript.Shell")
Set objPassword = CreateObject("ScriptPW.Password")
WScript.StdOut.Write "Please enter your password:"
strPassword = objPassword.GetPassword()

oShell.Run "runas /noprofile /user:[domain\user] ""[program]"""WScript.Sleep 1000
oShell.SendKeys strPassword
oShell.SendKeys "{enter}"

Wscript.Quit

The code will not work. The sendkeys does not get EXECUTED until the runas is complete. Too LATE to be of any use. Runas REQUESTS the password in a new window, by moving the sendkeys prior to the runas, the results are keystrokes sent to a non-existent window.

You might try this workaround

Good luck. Try with next code:


strComputer = "."
Set oShell = WScript.CreateObject ("WSCript.shell")
usuario = "[emailprotected]"
clave = "somepasswd"


' ejecuta modificacion Zona en Registro
WScript.echo "modificando reg"
netsh1="regedit /s \\prestored.cl\NETLOGON\TZupdate_inv.reg"
txtDNS1 = "runas /noprofile /user:"& usuario &" """& netsh1 &""""
oShell.run txtDNS1, 2
WScript.sleep 1000
oShell.AppActivate "C:\Windows\system32\runas.exe"
oShell.SendKeys clave & "{ENTER}"
WScript.sleep 5000

' ejecuta carga de zona en pc
WScript.echo "refrescando cambios"
netsh1="cscript \\prestored.cl\NETLOGON\refreshTZinfo.vbs"
txtDNS1 = "runas /noprofile /user:"& usuario &" """& netsh1 &""""
oShell.run txtDNS1, 2
WScript.sleep 1000
oShell.AppActivate "C:\Windows\system32\runas.exe"
oShell.SendKeys clave & "{ENTER}"
WScript.sleep 5000

WScript.Quit



Discussion

No Comment Found