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