Link to home
Start Free TrialLog in
Avatar of prostang
prostangFlag for United States of America

asked on

vb script: sendkeys not working

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



Avatar of BrianGEFF719
BrianGEFF719
Flag of United States of America image

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
Avatar of prostang

ASKER

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.
Avatar of dave_moats
dave_moats

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
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.
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?
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.
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.
Yes I see that....I think it is  something with passing the command to runas incorrectly
ASKER CERTIFIED SOLUTION
Avatar of dave_moats
dave_moats

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
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.