I have a batch file which prompts the user a few times. I am looking to automate that with powershell. Is there any way to do this? I would need something like this:
Start-Process $InstallDir\Install.bat "y,*,$Version,y,y,y,y,y,y,y,y,y,y,y,y,y"
Install.bat runs an installation and there are a total of 16 prompts. The third I would like the be a variable that I have in my powershell script already, but the others will be static. Also, at the end of the script, you need to press any key to continue.
Is there any way to do this?
Depending on your batch file and what commands actually do the prompt, you might use input redirection <. Put the prompts into a text file pine by line and redirect that into your batch file.
Supposing the batch file prompts.bat contains the following commands...:
#echo off
set /P VAR="Please enter some text: "
echo/
echo Thank you for entering "%VAR%"!
choice /M "Do you want to continue "
if not ErrorLevel 2 del "%TEMP%\*.*"
pause
...and the text file prompts.txt contains the following lines...:
hello world
Y
n
End
...the console output of the command line prompts.bat < prompts.txt would be:
Please enter some text:
Thank you for entering "hello world"!
Do you want to continue [Y,N]?Y
C:\Users\operator\AppData\Local\Temp\*.*, Are you sure (Y/N)?
C:\Users\operator\AppData\Local\Temp\*.*, Are you sure (Y/N)? n
Press any key to continue . . .
(The del command shows two prompts here as it receives the RETURN behind Y which is not consumed by choice; since an empty entry is not accepted, the prompt appears one more time.)
Read-Host will display a prompt for entry, assigning it to a variable means you can then use that entry later in the script.
As your example is non-specific the below will only give you an idea of what you need to do.
$InstallDir = "C:\folder"
$Version = Read-Host -Prompt "Enter Version Number"
Start-Process "$InstallDir\Install.bat" -ArgumentList "y,*,$Version,y,y,y,y,y,y,y,y,y,y,y,y,y"
Related
I have batch file which I want to execute, during execution it asks for user input twice to press enter key. I want to skip this completely either by simulating the enter key press or by somehow completely overcoming it.
Edit : I can't edit the bat file to skip the program asking for user input
I have tried echo | <yourfinecommandhere> but this just simulates one enter key press. I am unable to simulate multiple enter key press
In cmd simply group multiple echos. For example this will print 2 newlines to the pipe
(echo[& echo[) | command
echo( is the most reliable way to print a new line in cmd, but in this case echo[ is probably better. Space is significant here so there must be no space after [
In PowerShell it's easier. For example to print 3 newlines use
"`n`n`n" | command
Actually the above will print 4 new lines, because there's an implicit new line after the string. But that won't affect the output. If you want exactly 3 new lines then use
Write-Output -NoNewline "`n`n`n" | command
I'm trying to make a system which turns the contents of a .txt file into a variable. This isn't my problem, though; for some reason, my files are reading characters I didn't enter, and can't use.
Please note: What I'm doing is in no way efficient, and I'm positive there are other ways to go about this, but this is the best way for me. Also, I'm not amazingly intelligent when it comes to coding. Just thought I'd throw that out there.
First, let me show you the system I have in place.
value.txt
4
This file has the contents which I'd like to make into a variable.
Batch Files
setcmdvar.bat
set cmdvar=
I leave this empty so that I can put the contents of value.txt at the end (more on this later).
start.bat
#echo off
call PowerShell.exe cd "C:\Users\%username%\Desktop\Folder"; $PSvar = Get-Content value.txt; $PSvar >> setcmdvar.bat
pause
call setcmdvar.bat
pause
echo The variable equals %cmdvar%.
pause
exit
The second line from start.bat creates this script in PowerShell:
PowerShell script
cd "C:\Users\%username%\Desktop\Folder\"
$PSvar = Get-Content value.txt; $PSvar >> setcmdvar.bat
This creates a variable in PowerShell, $PSvar, which equals the contents of value.txt; in our case, 4. Then, it puts $PSvar (4) at the end of setcmdvar.bat, using >>, which changes it to:
setcmdvar.bat (changed)
set cmdvar=4
Or, at least, it should, to my knowledge. Instead, it changes the file to this:
set Items=桔瑳楲杮椠業獳湩桴整浲湩瑡牯›⸢ †⬠䌠瑡来牯䥹普††††㨠倠牡敳䕲牲牯›㨨
嵛慐敲瑮潃瑮楡獮牅潲割捥牯䕤捸灥楴湯 †⬠䘠汵祬畑污晩敩䕤牲牯摉㨠吠牥業慮潴䕲灸捥整䅤䕴摮晏瑓楲杮
Or some other strange combination of characters. I looked one up, and it was Chinese. There's also some other characters like †, ⬠, ›, ⸢,
, and . I have no idea why these are being typed. Along with this, start.bat displays the following:
Press any key to continue . . .
(PowerShell script runs here)
'■s' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .
The variable equals .
Press any key to continue . . .
(exit)
I did not type "■s," and I assume this may be the problem. Whatever it is, does anyone have any ideas?
P.S. I'm sorry if my code is complicated, or if it looks bad, but I have it this way for a reason. Mostly my incompetence, actually. But I think it's better that way.
Also, I know there are commands like for /f "delims=" %a in ('ver') do #set foobar=%a (I just took this off the internet) but I've tried commands like those, and I suppose I just don't understand them all that well, because they didn't work.
I appreciate the help!
It would probably be better to avoid the static setcmdvar.bat script and write it all in the script. Using -Encoding ascii is what keeps the output from being Unicode and having a BOM (Byte Order Mark) at the beginning. It has nothing to do with Chinese characters.
ECHO>"%USERPROFILE%\value.txt" 4
SET "CMDFILE=%USERPROFILE%\setcmdvar.bat"
powershell -NoLogo -NoProfile -Command ^
"'SET ""cmdvar=' + (Get-Content $Env:USERPROFILE\value.txt) + '""""' |" ^
"Out-File -FilePath %CMDFILE% -Encoding ascii"
pause
call "%CMDFILE%"
pause
echo The variable equals %cmdvar%
pause
EXIT /B 0
>> has its problems with mixing encodings. Plus it defaults to utf16. I recommend changing
$PSvar >> setcmdvar.bat
to
add-content setcmdvar.bat $PSvar
I'm not sure how to keep everything on one line. You can make setcmdvar.bat like this:
set cmdvar=^
So that it continues.
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:
Detect whether the command execution requires any input to proceed
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:
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 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 does work on Unix platforms (PowerShell Core).
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
For some reason i simply can't understand most of the sites who explain this question. So i'll try to ask here, if i'm am in the wrong place, just tell me in the comments and i'll put this in another forum and delete this question.
Let's say that i have 2 files, Batch.bat and PowerShell.ps1.
Batch.bat:
set A="ThisIsSuchVar!"
PowerShell.ps1:
$B = "Well, i don't know what to do here"
What can i do to the B variable be the same as the A variable?
Remember: I want the Batch variable to go to the PowerShell file. It's an one-way script. I want to use the built-in windows sources. And please, consider that i am a complete newbie in programming and don't speak english very well, so be the simplest possible, please.
In your batch file run.bat, set the environment variable A and run the PowerShell script:
set A=8
PowerShell.exe -File .\script.ps1
pause
In script.ps1, get the environment variable A, and assign its value to B:
$B=$Env:A
echo $B
When you run run.bat you get:
C:\Temp\try>set A=8
C:\Temp\try>PowerShell.exe -File .\script.ps1
8
C:\Temp\try>pause
Press any key to continue . . .
Can you put form elements in a batch file? I was curious when I saw it in Word 5.5 for DOS. I can't find out how to do it, but I know NOTHING about all that junk in an executable file for DOS. Can somebody please help me?
If you want to ask for free text in a batch file use:
SET /P UserName=What is your UserName?
This puts the user's response in an environment variable named UserName, which can be accessed with %UserName%
like this:
ECHO Hello, %UserName%. Welcome to planet Earth!
You can put any executable command in a batch file, hence you can do anything in a batch file. If you see anything terribly fancy (like windowing), you probably are looking at a third party program, rather than a command built into the command processor itself.
Off the top of my head, the closest thing to a form (something that you can select items from) that I can think of is the CHOICE command. To find out how to use it, type this at a command prompt:
CHOICE /?
Here's the description...
This tool allows users to select one item from a list
of choices and returns the index of the selected choice.
Here are some syntax examples...
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."
And here's a full example:
CHOICE /C:ABCDN /N /T:10 /D:N /M:"Format drive A:, B:, C:, D: or None?"
REM Note that ERRORLEVEL handlers must be in decreasing order...
IF ERRORLEVEL 5 SET DRIVE=None
IF ERRORLEVEL 4 SET DRIVE=drive D:
IF ERRORLEVEL 3 SET DRIVE=drive C:
IF ERRORLEVEL 2 SET DRIVE=drive B:
IF ERRORLEVEL 1 SET DRIVE=drive A:
ECHO You chose to format %DRIVE%