Solved

VERY URGENT - easy help

Posted on 2003-11-17
8
747 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 43

Assisted Solution

by:TimCottee
TimCottee earned 100 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:rossdagley
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now