Solved

pass username and password info in my batch file.

Posted on 2004-09-01
10
2,352 Views
Last Modified: 2008-01-09
I am totally new to batch programming.  I have win 2k and need to create a batch file to logon on to my application automatically.

I save the following line in the StartApp.bat file:
j:\source\BIN\flxctl32.exe
The application will start and the logon screen will be shown.

I read some of the other posting, I am not quite sure how to create another file to store the user name and password.  The log on screen has two text boxes.  The top one enter the user name and the bottom one asks for the password.

Also, is there a way to quit the above application through a batch file?

I try to schedule to run the application at night with the nt scheduler and then exit the application once the process is completed.

Thanks for the help.
0
Comment
Question by:yylassie
  • 4
  • 3
  • 3
10 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 11954102
It depends on the application in most cases.
Some things to try:

j:\source\BIN\flxctl32.exe  /?
to see if there are any command line parameters

j:\source\BIN\flxctl32.exe username password
may work...

Otherwise, a macro application may be necessary if it's a gui logon box.
Sysfriend is a good one: http://www.skigreenvalley.com/elm/503467

As far as killing it - pskill is a great tool from sysinternals to do this:
http://www.sysinternals.com/ntw2k/freeware/pskill.shtml

Unless you have access to the resource kit which, I believe, includes a similiar application (kill.exe)
0
 

Author Comment

by:yylassie
ID: 11955501
I tried the /? and no parameters return.
I tried to put the username and password in the same line as the exe and it won't work too.  I saw other posting about storing the username and password in another file, will it work for the GUI front end?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 11957902
No, I don't believe it will - you're initiating a graphical interface from a dos environment.
I think you're stuck with using an automated macro...
0
 
LVL 8

Expert Comment

by:K_2K
ID: 11959083
Or make an automation macro out of VB Script, tailored to your app.

Here's one i made at home wait for a known app to pop up, and then send keys to it.
You just plug in what keys you wish to send and you'll have it done.  Give us a little more info on what the GUI looks like and we can clean it up it you need, but you sound quick enuff to mod this.  

The "App title" should reflect what shows in the title bar of window that asks for the username and password.  You only need enough for it to be unique from any other windows that may be open, and must put it is quotes if there is a space in it.  Alternately,  you could take any of those information bits out of the .bat and hard-code them in the .vbs thinking fewer folks could find it there,  but do NOT fool yourself into thinking either method is safe from prying eyes.  It's up to you to hide either of these solutions from anyone who is not allowed to see the password.

(\o/)  Good Luck,
2K


::Startit.bat
j:\source\BIN\flxctl32.exe
"%windir%\system32\wscript" "Startit.vbs" Username Password "App Title"
:: End of Startit.bat - the rest is in the vbs file


' Startit.vbs
' Enter a Username and password to a window title when it pops up
' This script will show a cryptic error and fail if the user and password is not
'  not on the command line that starts this script.

option explicit
dim  oSH, sUser, sPWD, sTitle, sTmp
Set oSH = WScript.CreateObject("WScript.Shell")
sUser = WScript.Arguments(0)
sPWD = WScript.Arguments(1)
sTitle = "Example of hard-coding a window title so you do not have to put it on the command line"

' set title here only if there's more than just user and password on the command line
if WScript.Arguments.count >2 then sTitle = WScript.Arguments(2)


' focus on the desired window when it arrives
  WaitFor sTitle
 
' Type User name
dSK sUser

' Type a tab key to get to the password field
'  You may need to add tabs if the GUI has other fields in the way.
dSK "{tab}"

' Type the password
dSK sPWD
' Type the enter Key
'  You may need to modify this to a tab and then enter if your GUI requires
'         you to be on a "submit" button to send the info
dSK "{enter}"

' subroutines  - - Program flow ends here and this stuff is called from above as needed
Sub dSK(s)
     ' pause 1/10th of a second before sending anything.  Trust me here.
     WScript.Sleep 100
     oSH.SendKeys s
End Sub
 
Sub WaitFor(s)
  ' check more than 3 times per second for the window.
  sTmp = False
  do until sTmp
     Wscript.sleep 300
  sTmp = oSH.AppActivate (s)
  Loop
End Sub
' end of Startit.vbs
0
 
LVL 8

Expert Comment

by:K_2K
ID: 11959114
p.s.  We could sleep 1000 times X number of seconds you wish this to run, then switch to the app window (not the password window) and send the Alt/F4 key:
    dSK "%{F4}"
to ask the app nicely if it would like to save or empty it's temp files and close,  but that would be pushing the edge of over-cuteness,  when TSKILL.exe or PSKILL.exe work almost as well.

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:yylassie
ID: 11964594
I am new to vbs too.  Do I save the vbs file in the same directory as the bat file?  Do I need the VB application to run the vbs file?  Thank you for the help.
0
 

Author Comment

by:yylassie
ID: 11965888
I checked the logon screen and find out it's defulted with the last signon name so I do not have to do the send key for the user name.  ALl I have to do is to send the password and press enter.  However I failed after I created the files based on the instructions I got.  I must be doing sth. wrong.  The logon screen has the title "Sign In" in the title bar, the first text box asked for the name and the second text box asked for the password, the OK and cancel buttons are on the right hand side.

Can I place the .bat and .vbs file anywhere I want?  Do they need to be in the same directory or the .vbs has to be in the same directory as the wscript.exe file?

I got the error message and said there's no script engine for the vbs file but I see the wscript under the c:\winnt\system32 directory

Here's the files that I created:

::startit.bat
j:\source\BIN\flxctl32.exe
"%windir%\system32\wscript" "Startit.vbs" Password "Sign In"

'startit.vbs
' Enter a Username and password to a window title when it pops up
' This script will show a cryptic error and fail if the user and password is not
'  not on the command line that starts this script.

option explicit
dim  oSH, sUser, sPWD, sTitle, sTmp
Set oSH = WScript.CreateObject("WScript.Shell")
'sUser = WScript.Arguments(0)
sPWD = WScript.Arguments(0)
sTitle = "Sign In"

' set title here only if there's more than just user and password on the command line
if WScript.Arguments.count >1 then sTitle = WScript.Arguments(1)


' focus on the desired window when it arrives
  WaitFor sTitle
 
' Type User name
'dSK sUser

' Type a tab key to get to the password field
'  You may need to add tabs if the GUI has other fields in the way.
'dSK "{tab}"

' Type the password
dSK "abcde"
' Type the enter Key
'  You may need to modify this to a tab and then enter if your GUI requires
'         you to be on a "submit" button to send the info
dSK "{enter}"

' subroutines  - - Program flow ends here and this stuff is called from above as needed
Sub dSK(s)
     ' pause 1/10th of a second before sending anything.  Trust me here.
     WScript.Sleep 100
     oSH.SendKeys s
End Sub
 
Sub WaitFor(s)
  ' check more than 3 times per second for the window.
  sTmp = False
  do until sTmp
     Wscript.sleep 300
  sTmp = oSH.AppActivate (s)
  Loop
End Sub
' end of Startit.vbs
0
 
LVL 8

Accepted Solution

by:
K_2K earned 500 total points
ID: 11978419
Not having a designated script engine is usually indicative of something odd having changed the system default for VB file types, and usually fixed forever with a one time run of :
    cscript //H:WSCRIPT
forever until something moves it, but usually you know when and why it moved after you've started knowing the setting is there.
Calling wscript directly should make that a moot point, but alas you did that the first time. You may need to download and install run-times from MS although they were included with every OS since Windows 98.  It wouldn't hurt to pull the latest from http://msdn.microsoft.com/library/default.asp?url=/downloads/list/webdev.asp to get all the latest features.

First of all,  when you run this manually, does the enter key work? or are you FORCED to click on the OK button?  If so, does a number of tabs and then a space bar work?  If so, did you try to code a dSK "{TAB}{SPACE}" or dSK "{TAB}{TAB}{SPACE}" or whatever?


For being new to .vbs, your mods show great promise of being able to understand the beast (VB) in general.

Yes, you should be able to put the .vbs anywhere in your path, just as you could a .bat .cmd .exe .com or any other known file type.  (Try tossing your favorite "song.mp3" in the %windir% folder, then click start, run, "song.mp3"  or enter it at the command prompt.  quotes are needed if there's a space in it, but i re-digress)
My preference is to put it in the same folder as the batch file that calls it, so i can also keep the mods together.  This ALSO allows %~dp0 in the batch to use the batch's drive and path to make sure i'm calling the same .vbs that is close to the batch, but that's just my preference, and as you saw full path is not needed if you expect to be in that path so i don't always add the path, but i now realize you just might call this batch from another folder:


::startit.bat
j:\source\BIN\flxctl32.exe
"%windir%\system32\wscript" "%~dp0Startit.vbs"


You'll notice i also took the title out of the call, since you correctly hard-coded it into the .vbs, we don't need it in both.
You hard-coded the password in the .vbs correctly, so no need for it here, nuther ;)


I suspect the reason it is not answering the password is because the first and second text box ( asking for the name and password ) has a different title bar than the main app.  Both section in this .vbs are finding the app open and sending responses to it possibly before the first box opens.
If there's NOT a different title bar for those,  you can simply add a long enough pause between entries so the boxes can come up before the keys are sent.  This could cause a visible delay, but hey,  this is automation, if it works without my help why would I be waiting around to watch it?

I'll throw both the pause and a not quite corrected title bar fix in,  and you can comment out whichever you don't need:

(\o/) Enjoy,
2K


'startit.vbs
' Enter a Username and password to a window title when it pops up
' This script will show a cryptic error and fail if you change to read the password and
'  then do not put it on the command line that starts this script.

option explicit
dim  oSH, sUser, sPWD, sTitle, sTmp
Set oSH = WScript.CreateObject("WScript.Shell")
' sUser = WScript.Arguments(0)
' sPWD = WScript.Arguments(0)
sTitle = "Sign In"

' wait for app to start in case we have to use pauses
WaitFor sTitle

' focus on the user name box when it arrives
'
'  IMPORTANT - This script will sit in the task manager list forever waiting if you don't have a title bar that matches it !
'  if that's the case, take out these WaitFor lines and just use the timed pauses
'
' this should be the title bar of the text box asking for user name
sTitle = "User Name"
WaitFor sTitle

' if you do not have a title bar for above, this lag time may do the trick  
wscript.sleep 5000    ' 5 seconds seems like a long time, but let's crawl before we run
' Once you time the wait between screens, make sure you double it and add another second
'  somwtimes other things running can really get in the way.
' naturely you'll want to take these lags out if the title bar change works
 
' Type User name
'  If we don't need to change the name, we'll just press "Enter" to get to the password box
dSK "{enter}"

' Type a tab key to get to the password field
'  You may need to add tabs if the GUI has other fields in the way.
'  it's good to see this commented out
'dSK "{tab}"

' focus on the password window when it arrives
' IMPORTANT - ditto above important note
sTitle = "Password"
WaitFor sTitle

' if you do not have a title bar for above, this lag time may do the trick  
wscript.sleep 5000    ' 5 seconds seems like a long time, but let's crawl before we run
' naturely you'll want to take these lags out if the title bar change works

' Type the password
' hard coded here?  guess we don't need anything on the command line.
dSK "abcde"

' Type the enter Key
'  You may need to modify this to a tab and then enter if your GUI requires
'         you to be on a "submit" button to send the info
dSK "{enter}"

' subroutines  - - Program flow ends here and this stuff is called from above as needed
Sub dSK(s)
     ' pause 1/10th of a second before sending anything.  Trust me here.
     WScript.Sleep 100
     oSH.SendKeys s
End Sub
 
Sub WaitFor(s)
  ' check every third of a second for the window.
  sTmp = False
  do until sTmp
     Wscript.sleep 300
  sTmp = oSH.AppActivate (s)
  Loop
End Sub
' end of Startit.vbs
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 11978772
K_2K - you scare me...you really do...
LOL : )
0
 
LVL 8

Expert Comment

by:K_2K
ID: 11979362
no, no, really, take out all those comments and you just got a little code there.

one module to wait till an application is available,
another to send keys,
and a little glue to slap the two together.


*<;-))
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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

9 Experts available now in Live!

Get 1:1 Help Now