Search code examples
vbacmdms-wordfilesystemobject

VBA command line object requesting login, waiting for user input


I wrote a script that obtains some information from a program through a command line prompt. Obtaining the information works as long as you are logged into the program I'm trying to extract the information from.

This is how I'm connecting to the program:

Set Appl = CreateObject("WScript.Shell").Exec("cmd.exe im connect")

Returning (if you're not logged in): (please note: this is the theoretical output if I manually type the command into a command window)

Connecting to <some server or another>...
Authentication failed for <user: me> on <some server or another>
Enter password for <user: me> on <some server or another> (*****):

And this is where my macro gets stuck.

I've tried checking for the return string "Enter password", but since the file system object calling the shell gets stuck waiting for the user input I'm not able to really do anything about it.

So all of this is not working:

Set ApplOutput = Appl.StdOut
While Not ApplOutput.AtEndOfStream
    sLine = ApplOutput.ReadLine
    If Left(sLine, 14) = "Enter password" Then
        MsgBox ("ERROR!!" & vbCrLf & "Please login to the Integrity Client") 
    End If
Wend

Is there any way to check if the command window is waiting for a user input (as in the case above) and if so bring the command window to the front and ask the user to input their password? If that was possible the macro should be running smoothly afterwards.

Thanks for any kind of input in advance!

edit: As the command I'm trying to run is not very common, consider the problem running a "runas" command through a macro. The runas will similarily be prompting for a password , e.g.:

runas /noprofile /user:Administrator cmd

edit2: The problem with providing the credentials every time is the following: The program requesting the password saves the password to login to the server after you've successfully entered it once. Meaning the next time you have to enter it is (in the best case) three months afterwards when the standard password rotation requires you to set a new password for your user account. Therefore it'd be really annoying to have to enter the password every single time just because every three months it doesn't work..


Solution

  • If "shell gets stuck waiting for the user input" then there may not be a simple solution.... VBA isn't multi-threaded, so if shell .Exec is waiting for user input, no error has been raised, and the execution is ongoing. Unfortunately this means that you can't "break" in to that thread and use a FindWindow or other API call to manipupate the CMD window.

    One approach, would be to pass one of the AppWinStyle arguments to the Exec statement (see also: this vb.net answer), in order to maximize the CMD window so that it will be immediately visible to the user irrespective of the credential check. This way, if the login fails, they'll see it and have the opportunity to respond to it.