Managing inputs from external command in powershell script
First, I would like to apologize in case the title is not descriptive enough, I am having a hard time dealing with this issue. I am trying to build an automation for an svn merge using a powershell script to be executed for another process. The function I'm using looks like this:
function($target){
svn merge $target
}
Now my problem occurs when there are conflicts in the merge. The default command behavior prompts for user input and acts accordingly. I would like to automate this process using predefined values ββ(show differences and then defer the merge), but I haven't found a way to do this. So, the workflow I want to follow is the following:
- Determine if the command needs to be executed to make any changes.
- Provide default input (in my particular case "df" followed by "p")
Is there a way to do this in powershell? Thank you so much for any help / hint you can provide me.
Edit:
To clarify my question: I would like to automatically specify a value when a command executed in a powershell script is required, as in the following example:
Edit 2:
Here is a test using the snippet provided by @ mklement0. Unfortunately it didn't work as expected, but I thought it was a must to add this edition to clarify the issue in full
source to share
Note :
-
This answer does not solve the OP's problem, because the particular target utility
svn
seems to suppress requests when the "stdin" input of the process does not come from the terminal (console). -
However, for utilities that are still asking, the solution below should work within the specified limits.
-
Generally, before attempting to simulate user input, it is worth investigating whether the target utility offers programmatic control over behavior through command line parameters that is simpler and more reliable.
Although it would be far from trivial to find out if a given external command is prompting for user input:
- you can blindly submit presumptive responses,
- which assumes that situational changes are unnecessary (except in cases where individual calls do not occur at all, in which case the input is ignored).
Suppose the following batch file foo.cmd
, which produces 2 prompts and an input echo:
@echo off
echo begin
set /p "input1=prompt 1: "
echo [%input1%]
set /p "input2=prompt 2: "
echo [%input2%]
echo end
Now send the answers one
and two
in this 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 for this approach to work with the Windows -
'one', 'two' | ./foo.cmd
does not work.- Note that it
<
must be presented as'<'
to make sure it is passed incmd.exe
and not interpreted by PowerShell in front (where<
not supported).
- Note that it
-
In contrast, it
'one', 'two' | ./foo
runs on Unix platforms (PowerShell Core).
source to share