Solved

VERY URGENT - easy help

Posted on 2003-11-17
8
753 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

710 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