I am trying to run a console application in
background using
powershell script and in the main method of "program.cs" it requires user confirmation. Working of this console application is to update some configurations and settings before i install a main application.
ConsoleApp.exe ->
below is sample code from my "Program.cs"
public class Program
{
static void Main(string[] args)
{
RunCommandLine();
}
public static void RunCommandLine()
{
Console.WriteLine("Do you wan to continue (Yes/No)?");
var response = Console.ReadKey();
if (response.Key.ToString().StartsWith("y"))
{
Console.WriteLine("Working with - Yes");
}
Console.WriteLine();
Console.WriteLine("Working without -Yes");
Console.ReadLine();
}
}
What I have tried:
Following command is working fine when I am not running this "ConsoleApp.exe" in background
Start-Process "c:\temp\ConsoleApp.exe"
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('ConsoleApp')
$wshell.SendKeys('y')
But it stop working when i run it in background using following command
Start-Process -NoNewWindow "c:\temp\ConsoleApp.exe"
possible reason is when running it in background, there is no interactive window to send keystrokes.
I have tried some other powershell commands as well like
C:\Temp\ConsoleApp.exe -Confirm:$false
echo Y | C:\Temp\ConsoleApp.exe
Write-Output Y | C:\Temp\ConsoleApp.exe
Is there any other way to achieve the same?