Click here to Skip to main content
15,888,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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"))
        {
            //Some code here
            Console.WriteLine("Working with - Yes");
        }
        Console.WriteLine();
        //Some code here
        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?
Posted
Updated 26-May-22 8:29am
Comments
PIEBALDconsult 26-May-22 11:14am    
How about:
Start-Process -NoNewWindow "echo y | c:\temp\ConsoleApp.exe"
taljit 27-May-22 3:43am    
I tried it, but it does not work
Richard MacCutchan 26-May-22 11:25am    
If you always run it with an automated answer then just do away with the prompt and its corresponding read function.
Dave Kreskowiak 26-May-22 11:47am    
SendKeys only works when there's a target window to send the key messages to.

Since you created the process with -NoNewWindow, there's no window to send the messages to.

In your contrived example, you can either just remove the prompt entirely or you can add support for a command line switch to bypass the prompt.
Member 15627495 26-May-22 13:15pm    
one console can call another cmd.exe
you have two parameters cmd /k | cmd /u

which fire with the windows to stay or to close when the script is done.

in cmd.exe , take few reads by "/?" on the target you need. it's the main help for all possible function.

Copy Code
ftp /?
dir /?
dir /p /?
cmd /?
tree /?
sfc /?
sfc /scannow /?
dism /?
dism /online /?
dism /online /cleanup-image /?
dism /online /cleanup-image /restorehealth /?
wmic /?

Copy Code
shell("Notepad.exe")
process.start("cmd.exe dir /p")
process.start(URL)

1 solution

Redirect "standard input".

process.StartInfo.RedirectStandardInput = true;
process.StartInfo.UseShellExecute = false;


Give user input for batch file via c# code - Stack Overflow[^]
 
Share this answer
 
Comments
taljit 27-May-22 3:01am    
using "Redirect Standard Input", getting the following exception.

Unhandeled Exception : System.InvalidOperationException : Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900