Solved

VERY URGENT - easy help

Posted on 2003-11-17
8
751 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

856 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