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,253 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.
277 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,572 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,367 questions
{count} votes

6 answers

Sort by: Most helpful
  1. MotoX80 31,571 Reputation points
    2021-04-07T13:00:20.157+00:00

    GetResponse is just a function that the script calls to get the output of the program. This makes it easier if there are multiple interactions with the program where the script enters data for the program to process and then does something based on the output.

    This might be more of what you are looking for but you will need to test it because i do not know what program you are calling and what output it produces.

    param ($password ="")
    
    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 read 
        } 
        return $buffer
    }
    
    if ($password -eq "") {
        "Please enter a password as a command line parameter"
        return
    }
    
    $MyProcess = New-Object System.Diagnostics.Process
    $MyProcess.StartInfo.FileName = "Cscript.exe"                                # program name
    $MyProcess.StartInfo.Arguments = "C:\scripts\myscript.vbs"              # it's command line 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 
    "---------------Look for the program to output the work password-------------"
    while ($true) {
        if ($MyProcess.HasExited -eq $true) {
            "The program has ended."
            break
        }
        $buffer = GetResponse
        "Program output-------------------------------------------"
        $buffer
        "End of output buffer-------------------------------------"
        if ($buffer -match 'password') {
            "I found the word password. Entering it now."
            $StdIn.WriteLine($password)
            break
        }
    }
    "Password processing is complete. Waiting until the process ends." 
    while ($MyProcess.HasExited -ne $true) { Start-Sleep -Milliseconds 50 }
    "Final Stdout-------------"
    $StdOut.ReadToEnd()
    "Final Stderr-------------"
    $StdErr.ReadToEnd()
    return 
    
    0 comments No comments