简体   繁体   中英

Manage inputs from external command in a powershell script

First, I would like to apologize in case that the title is not descriptive enough, I'm having a hard time dealing with this problem. I'm trying to build an automation for a svn merge using a powershell script that will be executed for another process. The function that I'm using looks like this:

function($target){
   svn merge $target
}

Now, my problem occurs when there are conflicts in the merge. The default behavior of the command is request an input from the user and proceed accordingly. I would like to automatize this process using predefined values (show the differences and then postpone the merge), but I haven't found a way to do it. In summary, the workflow that I am looking to accomplish is the following:

  1. Detect whether the command execution requires any input to proceed
  2. Provide a default inputs (in my particular case "df" and then "p")

Is there any way to do this in powershell? Thank you so much in advance for any help/clue that you can provide me.

Edit:

To clarify my question: I would like to automatically provide a value when a command executed within a powershell script require it, like in the following example:

Requesting user input

Edit 2:

Here is a test using the snippet provided by @mklement0. Unfortunately, It didn't work as expected, but I thought it was wort to add this edition to clarify the question per complete

Expected behavior: 预期的行为

Actual result: 实际结果

You can store the SVN command line output into a variable and parse through that and branch as you desire. Each line of output is stored into a new enumerator (cli output stored in PS variables is in array format)

$var = & svn merge $target
$var

Note :

  • This answer does not solve the OP's problem, because the specific target utility, svn , apparently suppresses prompts when the process' stdin input isn't coming from a terminal (console).

  • For utilities that do still prompt, however, the solution below should work, within the constraints stated.

  • Generally, before attempting to simulate user input, it's worth investigating whether the target utility offers programmatic control over the behavior, via its command-line options , which is both simpler and more robust.


While it would be far from trivial to detect whether a given external command is prompting for user input:

  • you can blindly send the presumptive responses,
  • which assumes that no situational variations are needed (except if a particular calls happens not to prompt at all, in which case the input is ignored).

Let's assume the following batch file, foo.cmd , which puts up 2 prompts and echoes the input:

@echo off
echo begin
set /p "input1=prompt 1: "
echo [%input1%]
set /p "input2=prompt 2: "
echo [%input2%]
echo end

Now let's send responses one and two to that batch file:

C: PS> Set-Content tmp.txt -Value 'one', 'two'; ./foo.cmd '<' tmp.txt; Remove-Item tmp.txt
begin
prompt 1: one
[one]
prompt 2: two
[two]
end

Note:

  • For reasons unknown to me, the use of an intermediate file is necessary for this approach to work on Windows - 'one', 'two' | ./foo.cmd 'one', 'two' | ./foo.cmd does not work.

    • Note how the < must be represented as '<' to ensure that it is passed through to cmd.exe and not interpreted by PowerShell up front (where < isn't supported).
  • By contrast, 'one', 'two' | ./foo 'one', 'two' | ./foo does work on Unix platforms (PowerShell Core).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM