Reputation: 513
I have a batch script like below. I want to call this script from a terminal window (cmd)
, by running it using powershell
Start-Process
command (Doing this in Azure ARM Template).
I want to pass the INPUT
value while calling the script, probably by using a pipe symbol.
Not able to pass the INPUT
value through pipe symbol to a Start-Process
command.
Am I missing something?
How can I achieve this (without any modifications to this batch file)?
@echo off
SETLOCAL EnableDelayedExpansion
set INPUT=
set /P INPUT=Type yes or no: %=%
echo[
echo actually typed %INPUT%
IF "%INPUT%"=="n" goto :CLICKEDNO
IF "%INPUT%"=="N" goto :CLICKEDNO
IF "%INPUT%"=="NO" goto :CLICKEDNO
IF "%INPUT%"=="no" goto :CLICKEDNO
IF "%INPUT%"=="No" goto :CLICKEDNO
IF "%INPUT%"=="yes" goto :CLICKEDYES
IF "%INPUT%"=="Yes" goto :CLICKEDYES
IF "%INPUT%"=="Y" goto :CLICKEDYES
IF "%INPUT%"=="y" goto :CLICKEDYES
:CLICKEDYES
echo Typed yes
goto :eof
:CLICKEDNO
echo Typed no
goto :eof
echo end of file
Even though I am trying to pass the value "no", it is printing "yes".
Below are some of the commands that I have tried.
echo no | powershell Set-Location -Path C:\CAP\CAP-Installer ; Start-Process -NoNewWindow .\test.bat
echo no | powershell Start-Process -NoNewWindow -FilePath C:\CAP\CAP-Installer\test.bat
Output for the comands 1 and 2:
Type yes or no:
actually typed no
Typed yes
powershell -Command "echo no | Start-Process -NoNewWindow -FilePath C:\CAP\CAP-Installer\test.bat"
echo no | powershell -Command "$input | Start-Process -NoNewWindow -FilePath C:\CAP\CAP-Installer\test.bat"
Output for the comands 3 and 4:
Type yes or no: Start-Process : The input object cannot be bound to any parameters for the command
either because the command does not take pipeline input or the input and its
properties do not match any of the parameters that take pipeline input.
At line:1 char:11
+ echo no | Start-Process -NoNewWindow -FilePath C:\CAP ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (no:PSObject) [Start-Process], Paramet
erBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.Start
ProcessCommand
C:\Users\CAP-VM>
actually typed
Typed yes
C:\Users\CAP-VM>
Any help is appreciated. Thanks is advance.
Upvotes: 0
Views: 1591
Reputation:
You do not need to do a match for each case. use if /i
to match case insensitive, we also need to use %1
to specify :
@echo off
echo Type Yes/no & set /P "in=%1"
if /i "%in%" == "n" goto :no
if /i "%in%" == "no" goto :no
if /i "%in%" == "y" goto :yes
if /i "%in%" == "yes" goto :yes
:yes
echo Typed yes
goto :eof
:no
echo Typed no
goto :eof
obviously it is not even needed to set
anything, so this is a better option:
@echo off
if /i "%~1" == "n" goto :no
if /i "%~1" == "no" goto :no
if /i "%~1" == "y" goto :yes
if /i "%~1" == "yes" goto :yes
:yes
echo Typed yes
goto :eof
:no
echo Typed no
goto :eof
and Even better, use choice
to make choices instead of using if
@echo off
choice /c yn /m "yes/no"
if not "%errorlevel%" equ "0" goto _opt%errorlevel%
exit /b 0
:_opt1
echo You chose Yes
goto :EOF
:_opt2
echo You chose No
goto :EOF
You can simply run it directly from cmd
and not via cmd
to call powershell
to run a batch-file
which runs in cmd
. Much simpler:
echo yes|"C:\CAP\CAP-Installer\test.bat"
If you really need to use it via powershell
send it as a paramater:
powershell Start-Process -NoNewWindow -FilePath C:\CAP\CAP-Installer\test.bat yes
Upvotes: 1