Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 767
  • Last Modified:

VERY URGENT - easy help

Hi

I *urgently* need some help.

Please see http://www.experts-exchange.com/Operating_Systems/Win2000/Q_20674387.html

I want to use this script, but hard code the username and password into it (I still want to pass the command to it). I am a VB novice, and dont know how to do this. I've tried simply replacing the arguements, but cant get it to work right.

Can someone help? thanks guys!


Ross
0
rossdagley
Asked:
rossdagley
  • 3
  • 3
  • 2
2 Solutions
 
NBrownohCommented:
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.

on that part do this:

rc=WshShell.Run("runas /user YourUName " & 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 "ThePassWord" 'send the password to the waiting window.
0
 
rossdagleyAuthor Commented:
Thanks for that - do I need to change anything else? it doesnt seem to pass the command to the script. I've tried changing the sCmd=oArgs line to another number (presumably this is where it reads what command to run off the command line?) but this doesn't work?

Thanks for the fast reply - I'm in a bit of a hole here!

Ross
0
 
NBrownohCommented:
try changin this line:
Dim WshShell, oArgs, FSO
to:
Dim WshShell, oArgs(), FSO

and this line:
Set oArgs = wscript.Arguments
to:
Set oArgs() = wscript.Arguments

if that doesnt work then dont do the last thing there and try that.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
TimCotteeCommented:
Hi rossdagley,

You also need to send a carriage return as well!

rc=WshShell.Run("runas /user YourUName " & 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 "ThePassWord{ENTER}" 'send the password to the waiting window.

Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 
rossdagleyAuthor Commented:
Thanks again for the fast reply.

NBrownoh - Changing the lines you said now makes it run the error routine (I tried both of what you said). If I leave it how it was, it doesn't do anything, but it does pass the password out - to the window running the cscript command. IE, i use the command "cscript vbrunas.vbs cmd.exe" and a new window does not open, but from the command line I ran that line from, I see the password I specified in my file.

Here's what I've got at the moment.

'Start of Script
'VBRUNAS.VBS
'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.

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 <1 then
wscript.echo VBCRLF & "! Usage Error !" & VBCRLF
Usage
end if

sUser=oArgs(2)
sPass=oArgs(1)&VBCRLF
sCmd=oArgs(0)

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 administrator" & 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 "password" '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

0
 
rossdagleyAuthor Commented:
And thanks for telling me about passing the carrage return - thats bits now working, but obviously it's still not being passed the command properly as it just does as above - puts the password in my current command window.

Thanks guys

Ross
0
 
NBrownohCommented:
ok i just tried it again myself, take change this line:
rc=WshShell.Run("runas /user administrator" & CHR(34) & sCmd & CHR(34), 2, FALSE)
to this:
sUser = "administrator"
rc = WshShell.Run("runas /user:" & sUser & " " & Chr(34) & sCmd & Chr(34), 2, False)

for some reason that seems to work.
0
 
TimCotteeCommented:
Indeed, the syntax is:

RunAs /User:DOMAIN\USERNAME "Command String"

So the : is important. Include the DOMAIN\USERNAME if using an NT domain rather than a win 2K domain which uses USERNAME@DOMAIN.COM style instead. If you do not include a domain it will be the local administrator of the machine that is used.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now