Solved

vb script: sendkeys not working

Posted on 2006-11-01
10
1,692 Views
Last Modified: 2008-02-07
I have an application that I have started working on to create an environmental variable using the "runas" command.  The application is firing and no errors are found, but I am prompted for the password and the sendkeys command isn't working.  The window will stay open until I close it from frustration.  Here is the program:

'Set oFSO = CreateObject("Scripting.FileSystemObject")
set oShell= Wscript.CreateObject("WScript.Shell")
password = "{[}password{)}"                                                                          'the special characters are by design

intButton = oShell.Popup("Conducting an EAB update.  Do not interrupt this process.  Do not open any applications.  You will be logged out when completed.",5,"EAB Update",48)
program = "\\Eabrn1\cadd\Bentley\updates\supporting_files\inroads_server_licensing.reg"
licvalue= "\\Eabrn1\cadd\Bentley\Civil\licensing\"


      '---------------------------------------------------      
      'Replace the path with the program you wish to run within the quotes...
      '---------------------------------------------------
      oShell.Run "runas /user:asetdm\adeashwy \\Eabrn1\cadd\Bentley\updates\supporting_files\inroads_server_licensing.reg"
      WScript.Sleep 100
      oShell.AppActivate "runas.exe"
      '---------------------------------------------------
      'enter the admin password to be used.
      'within the quotes.  the curly brackets are needed
      'to send special characters.  You must include the tilde "~"
      'The tilde is the equivalent of hitting enter on the keyboard
      '---------------------------------------------------
      oShell.SendKeys password
      oShell.SendKeys "{ENTER}"                                                                           'this is questionable

'Wscript.Quit



0
Comment
Question by:prostang
  • 5
  • 4
10 Comments
 
LVL 19

Expert Comment

by:BrianGEFF719
Comment Utility
Does RunAs open as a window or a console application, because on my XP computer its opening as a console and the title is "C:\Windows\System32\runas.exe"


Brian
0
 

Author Comment

by:prostang
Comment Utility
you are right.  I just put that line of code in shortly before posting here.  The app is run when the user logs on so the focus is on the console.
0
 
LVL 6

Expert Comment

by:dave_moats
Comment Utility
Looks like if you specify set a variable to store the result from the oShell.Run call and then use it in the oShell.AppActivate, it will send the keys to the window.  Atleast it did on my XP box :-)

Since I do not know what you are actually doing, I spawned an application using runas.  

     ---------------------------------------------------
     'Replace the path with the program you wish to run within the quotes...
     '---------------------------------------------------
     cmdID = oShell.Run( "runas /user:asetdm\adeashwy \\Eabrn1\cadd\Bentley\updates\supporting_files\inroads_server_licensing.reg" )
     WScript.Sleep 100
     oShell.AppActivate( cmdID )
     '---------------------------------------------------
     'enter the admin password to be used.
     'within the quotes.  the curly brackets are needed
     'to send special characters.  You must include the tilde "~"
     'The tilde is the equivalent of hitting enter on the keyboard
     '---------------------------------------------------
     oShell.SendKeys password
     oShell.SendKeys "~"   'Tried both "~" and "{ENTER}" and both worked

Dave
0
 

Author Comment

by:prostang
Comment Utility
The .reg file being executed changes an environmental variable for an application to acquire a shared site license.  I have to go thru this method because the users aren't admin.  I copied and pasted your cmdID line and changed the AppActivate line and it still doesn't send the password.
0
 
LVL 6

Expert Comment

by:dave_moats
Comment Utility
Are these machines XP ?

Is the .reg file a registry file and you are trying to update the registry?  Or is it just a similiar name?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:prostang
Comment Utility
here is the what the .reg file has:  

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment]
-"INROADS_LICENSING"="\\\\Asgopinon\\cadd\\Program Files\\Bentley\\Civil\\licensing"

[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment]
"INROADS_LICENSING"="\\\\Eabrn1\\CADD\\Bentley\\Civil\\licensing\\"

The value has changed servers, so this is the file that will do it for me.  When I run the file thru the Window Run command, it will work if I don't use the runas statement.  I will not work (delete or create the new registry value) if I use the runas command.
0
 

Author Comment

by:prostang
Comment Utility
I am getting a "...reg is not a valid Win32 application." error when trying to run the .reg file from the cmd window using the runas command.

Sorry, it is an XP pro machine with SP2.
0
 
LVL 6

Expert Comment

by:dave_moats
Comment Utility
Yes I see that....I think it is  something with passing the command to runas incorrectly
0
 
LVL 6

Accepted Solution

by:
dave_moats earned 500 total points
Comment Utility
Try this, I create a variable to hold the command, this will install a test .reg file on my box.

cmdText = "runas /user:asetdm\adeashwy ""regedit /s \\Eabrn1\cadd\Bentley\updates\supporting_files\inroads_server_licensing.reg"""

     '---------------------------------------------------
     'Replace the path with the program you wish to run within the quotes...
     '---------------------------------------------------
     cmdID = oShell.Run( cmdText )
     WScript.Sleep 100
     oShell.AppActivate( cmdID )
     '---------------------------------------------------
     'enter the admin password to be used.
     'within the quotes.  the curly brackets are needed
     'to send special characters.  You must include the tilde "~"
     'The tilde is the equivalent of hitting enter on the keyboard
     '---------------------------------------------------
     oShell.SendKeys password
     oShell.SendKeys "~"   'this is questionable
0
 

Author Comment

by:prostang
Comment Utility
OK.  I tried your code on a different network and different computer and voila, it worked.  I then tried my original code, and it also worked.  Thanks for your efforts.  I know that the computer I need it to work on has issues, and after talking to the local IT guys, it seems to be part of a degrading network (there are many other symptoms).  Thanks again.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

728 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