Link to home
Start Free TrialLog in
Avatar of Mike
MikeFlag for United States of America

asked on

Need Help with a VB Script

Greeting Experts,
I am in need of some help hardcoding the following script with the username and password to open up a program in windows.  I not sure what I may be doing wrong….?? Can somebody take a look and see what you think….


'Start of Script
'RUNAS.VBS
'USAGE: cscript|wscript VBRUNAS.VBS airseaforwarder\misrshe kinkado "C:\cmd.exe"
'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 <3 then
wscript.echo VBCRLF & "! Usage Error !" & VBCRLF
Usage
end if

sUser='Username'
sPass='Password'
sCmd=''

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 or XP for this script to work."
set WshShell=Nothing
set WshEnv=Nothing
set oArgs=Nothing
set FSO=Nothing
wscript.quit
end if
'Use this line for remote file access and comment out the line below
'rc=WshShell.Run("runas /netonly /user:" & sUser & " " & CHR(34) & sCmd & CHR(34), 2, FALSE)

'Use this line for local file access and comment out the above line
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.

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 runas.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 runas.vbs domain\admin luckydog e:\scripts\admin.vbs" & VBCRLF & VBCRLF &" cscript runas.vbs domain\admin luckydog " & CHR(34) &"e:\program files\scripts\admin.vbs 1stParameter 2ndParameter" & CHR(34)& VBCRLF & VBCRLF & VBCLRF & "cscript runas.vbs /?|-? will display this message."

wscript.echo msg

wscript.quit

end sub
'End of Script 

Open in new window

Avatar of Mike
Mike
Flag of United States of America image

ASKER

Just as a point of ref: I getting stuck on line 26 char 6.... not sure if that the main problem or not.
Avatar of Kimputer
Kimputer

for those 3 lines, change it to:

sUser="Username"
sPass="Password"
sCmd=""

Open in new window


Don't use this character for strings ' (single quote)
Avatar of Mike

ASKER

OK, I change the 3 lines to the following and added the following

sUser='HI\MikeTEst1'
sPass='MikeTest1'
sCmd=''

Open in new window


and I am still getting error message as follows below... I have been playing with it but not sure what is means.... It refers to (sCmd=''). I not for sure how this plays in
User generated image
I said, DON'T USE single quotes. Use DOUBLE quotes instead.
I clearly said use:

sUser="Username"
sPass="Password"
sCmd=""

Open in new window


And described why, and then you adjust your code with SINGLE QUOTES again?
Avatar of Mike

ASKER

Ok, you are saying to replace all single (') with double quotes (") in the Script or just in the 3 lines you refer d in the post..... I understand ... Just want to make sure I am  understand you right....
For now, only the 3 lines. Otherwise you will uncomment the supposed comments.
Avatar of Mike

ASKER

Ok, this is what I have so far and I am still getting the same message. Can you take a look and see what is missing .....


 
Start of Script
'RUNAS.VBS
'USAGE: cscript|wscript VBRUNAS.VBS airseaforwarder\misrshe kinkado "C:\cmd.exe"
'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 <3 then
wscript.echo VBCRLF & "! Usage Error !" & VBCRLF
Usage
end if

sUser="HI\MikeS1"
sPass="MIkeS1"
sCmd=""

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 or XP for this script to work."
set WshShell=Nothing
set WshEnv=Nothing
set oArgs=Nothing
set FSO=Nothing
wscript.quit
end if
'Use this line for remote file access and comment out the line below
'rc=WshShell.Run("runas /netonly /user:" & sUser & " " & CHR(34) & sCmd & CHR(34), 2, FALSE"")

'Use this line for local file access and comment out the above line
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.

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 runas.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 runas.vbs domain\admin luckydog e:\scripts\admin.vbs" & VBCRLF & VBCRLF &" cscript runas.vbs domain\admin luckydog " & CHR(34) &"e:\program files\scripts\admin.vbs 1stParameter 2ndParameter" & CHR(34)& VBCRLF & VBCRLF & VBCLRF & "cscript runas.vbs /?|-? will display this message."

wscript.echo msg

wscript.quit

end sub
'End of Script 

Open in new window

Exact error? Assuming you forgot the first single quote while copying and pasting here ('Start of Script).
Avatar of Mike

ASKER

Ok, I went back and adding the Single Quote and then started getting the following error message for line 24 char 17


'Start of Script
'RUNAS.VBS
'USAGE: cscript|wscript VBRUNAS.VBS airseaforwarder\misrshe kinkado "C:\cmd.exe"
'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 <3 then
wscript.echo VBCRLF & "! Usage Error !" & VBCRLF
Usage
end if

sUser="HI\MikeS1"
sPass="MIkeS1"
sCmd=""

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 or XP for this script to work."
set WshShell=Nothing
set WshEnv=Nothing
set oArgs=Nothing
set FSO=Nothing
wscript.quit
end if
'Use this line for remote file access and comment out the line below
'rc=WshShell.Run("runas /netonly /user:" & sUser & " " & CHR(34) & sCmd & CHR(34), 2, FALSE"")

'Use this line for local file access and comment out the above line
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.

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 runas.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 runas.vbs domain\admin luckydog e:\scripts\admin.vbs" & VBCRLF & VBCRLF &" cscript runas.vbs domain\admin luckydog " & CHR(34) &"e:\program files\scripts\admin.vbs 1stParameter 2ndParameter" & CHR(34)& VBCRLF & VBCRLF & VBCLRF & "cscript runas.vbs /?|-? will display this message."

wscript.echo msg

wscript.quit

end sub
'End of Script 

Open in new window



User generated image
Avatar of Mike

ASKER

I am looking at line 17 of (sUser="HI\MikeS1")  and don't see where it can be unexpected end statement..... Can you look at the hole script and see what mistakes have been made...
ASKER CERTIFIED SOLUTION
Avatar of Kimputer
Kimputer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike

ASKER

Thanks for the help that worked.... :)