Solved

VERY URGENT - easy help

Posted on 2003-11-17
8
752 Views
Last Modified: 2010-05-01
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
Comment
Question by:rossdagley
  • 3
  • 3
  • 2
8 Comments
 
LVL 3

Expert Comment

by:NBrownoh
ID: 9762709
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
 
LVL 1

Author Comment

by:rossdagley
ID: 9762758
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
 
LVL 3

Expert Comment

by:NBrownoh
ID: 9762778
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Assisted Solution

by:TimCottee
TimCottee earned 100 total points
ID: 9762779
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
 
LVL 1

Author Comment

by:rossdagley
ID: 9762807
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
 
LVL 1

Author Comment

by:rossdagley
ID: 9762815
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
 
LVL 3

Accepted Solution

by:
NBrownoh earned 400 total points
ID: 9762818
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
 
LVL 43

Expert Comment

by:TimCottee
ID: 9762829
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question