console output automation

Rahul Fadnavis 1 Reputation point
2021-04-01T17:22:49.477+00:00

Hello,

Below is my command to perform one activity. It accepts few arguments which is fine and no issues there. But when we run that command in cmd, it asks for password and confirm password.
I want to automate that password and confirm password part. How can I do that?

Example:

When we run below command in cmd -

c:\Test\myscript.exe --arg1 --arg2 --config "c:\Temp\config.cfg" --confirm

It shows below in same command prompt-

Performing the script execution for ABCD.

Enter password:

Type again to confirm:

What I need is, How can I automate this entering of password and confirm password, considering I have the password.
Please help here. Powershell or vbscript solution is preferred.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,097 questions
Small BASIC
Small BASIC
A programming language created by Microsoft that serves a stepping stone for beginners from block-based coding languages to more complex text-based languages.
276 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,542 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,320 questions
{count} votes

6 answers

Sort by: Most helpful
  1. MotoX80 31,391 Reputation points
    2021-04-01T23:55:11.04+00:00

    What is myscript.exe? Is it really a script? Powershell? VB script? Or is it a compiled executable?

    You can try piping stdin to the program.

    echo MyPassword | myscript.exe 
    
    0 comments No comments

  2. Rahul Fadnavis 1 Reputation point
    2021-04-02T06:10:16.507+00:00

    Hello,

    Consider Myscript.exe as any third party software and we cannot alter or modify it. The exe has fixed arguments which it takes as an input but not the password by default. below powershell link has similar solution but I am not able to make it. Could you please help me here. Please let me know if any further details are needed here. Just check the last script of below link. Something similar I needed.
    Hence echo will not work here.

    What I actually need is to automate password input for any application/exe via cmd or powershell prompt. something related to New-Object System.Diagnostics.Process should work.

    how-to-provide-input-for-prompts-

    Please check the very last script of above link. similar I need to implement.

    0 comments No comments

  3. MotoX80 31,391 Reputation points
    2021-04-02T12:44:15.417+00:00

    Here is a Powershell script to use nslookup to query a name. Do you need to look for a specific response? That last script that you pointed out should work.

    $MyProcess = New-Object System.Diagnostics.Process
    $MyProcess.StartInfo.FileName = "nslookup.exe"
    $MyProcess.StartInfo.Arguments = "-"
    $MyProcess.StartInfo.UseShellExecute = $false
    $MyProcess.StartInfo.RedirectStandardInput = $true
    $MyProcess.StartInfo.RedirectStandardOutput = $true 
    $MyProcess.StartInfo.RedirectStandardError = $true 
    $MyProcess.Start()
    $StdIn = $MyProcess.StandardInput
    $StdIn.WriteLine("yahoo.com")
    $StdIn.WriteLine("exit")
    $StdIn.Close()
    $MyProcess.WaitForExit()
    $MyProcess.StandardOutput.ReadToEnd()
    $MyProcess.StandardError.ReadToEnd()
    
    0 comments No comments

  4. MotoX80 31,391 Reputation points
    2021-04-04T14:50:59.887+00:00

    I played around some more with calling nslookup and netsh to see if I could simulate a password prompt. I had mixed results. When nslookup outputs a ">", a .readline call will hang on that. I can do a .read to get each character, but once .peek returns a -1, then I have to do a .readline to reset it, otherwise it appears that it will always return a -1 even if more characters are available to be read.

    You will just have to test "MyScript.exe" to see how it interacts with console input/ouput.

    Function GetResponse {
        Start-Sleep -Milliseconds 1000                   # give the proram some time to run
        $buffer = $stdout.readline()                     # Assume the we have at least one line of output
        while ($StdOut.peek() -ne -1) {
            $buffer += [char]$StdOut.read()              # read char by char because readline might hang 
        } 
        return $buffer
    }
    
    
    
    $MyProcess = New-Object System.Diagnostics.Process
    $MyProcess.StartInfo.FileName = "nslookup.exe"
    $MyProcess.StartInfo.Arguments = "-"
    $MyProcess.StartInfo.UseShellExecute = $false
    $MyProcess.StartInfo.RedirectStandardInput = $true
    $MyProcess.StartInfo.RedirectStandardOutput = $true 
    $MyProcess.StartInfo.RedirectStandardError = $true 
    $MyProcess.Start()
    $StdIn = $MyProcess.StandardInput
    $StdOut = $MyProcess.StandardOutput
    $StdErr = $MyProcess.StandardError
    cls 
    "---------------Initial prompt-------------"
    GetResponse
    "---------------yahoo-------------"
    $StdIn.WriteLine("yahoo.com")
    GetResponse
    "---------------msn-------------"
    $StdIn.WriteLine("msn.com")               
    GetResponse
    "---------------exit-------------"
    $StdIn.WriteLine("exit")
    GetResponse
    
    while ($MyProcess.HasExited -ne $true) { Start-Sleep -Milliseconds 50 }
    "Final Stdout-------------"
    $StdOut.ReadToEnd()
    "Final Stderr-------------"
    $StdErr.ReadToEnd()
    return 
    
    0 comments No comments

  5. Rahul Fadnavis 1 Reputation point
    2021-04-07T09:12:08.083+00:00

    Thanks MotoX80 for your response. Unfortunately above script is not working as expected. Above you assume we will enter the password manually but actually we will pass the password as a parameter to powershell script and from there it will read that password and enter the password automatically whenever it prompts for passwords two time. I did not understand the role of GetResponse function in this case.

    0 comments No comments