Powershell:通过调用操作符

时间:2016-03-31 07:54:32

标签: .net windows powershell

我需要通过调用操作符(&)动态执行批处理文件。批处理文件接受参数,但我还需要在stdin上传入一些输入。我认为这样可行:

$command = "somebatchfile.bat"
"y" | & $command arg1 arg2

但是,"y"不会在stdin中传递。如果我像这样执行批处理文件:

"y" | somebatchfile.bat arg1 arg2

然后一切都按预期工作。但我不能硬编码这条道路。

如何执行一个动态确定的批处理文件,该文件将参数传递给某些标准输入?

更新

为了重现这一点,我做了以下工作:

script.bat

@echo off
call script2.bat %*

script2.bat

@echo off
set /p x="Enter: "
echo foo %* %x%

Powershell会话

> $command = "./script.bat"
> "yes!" | & $command whatever
Enter: foo whatever yes!

所以它在这里按预期工作。但是,当我将它与实际批处理文件(android.bat,Android SDK的一部分)一起使用时,输入不会传递给java.exe。

以下是android.bat的内容:

n@echo off
rem Copyright (C) 2007 The Android Open Source Project
rem
rem Licensed under the Apache License, Version 2.0 (the "License");
rem you may not use this file except in compliance with the License.
rem You may obtain a copy of the License at
rem
rem      http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.

rem Useful links:
rem Command-line reference:
rem   http://technet.microsoft.com/en-us/library/bb490890.aspx

rem don't modify the caller's environment
setlocal enableextensions

rem Set up prog to be the path of this script, including following symlinks,
rem and set up progdir to be the fully-qualified pathname of its directory.
set prog=%~f0

rem Grab current directory before we change it
set work_dir=%cd%

rem Change current directory and drive to where the script is, to avoid
rem issues with directories containing whitespaces.
cd /d %~dp0


rem Check we have a valid Java.exe in the path.
set java_exe=
call lib\find_java.bat
if not defined java_exe goto :EOF

set jar_path=lib\sdkmanager.jar;lib\swtmenubar.jar

rem Set SWT.Jar path based on current architecture (x86 or x86_64)
for /f "delims=" %%a in ('"%java_exe%" -jar lib\archquery.jar') do set swt_path=lib\%%a

:MkTempCopy
    rem Copy android.bat and its required libs to a temp dir.
    rem This avoids locking the tool dir in case the user is trying to update it.

    set tmp_dir=%TEMP%\temp-android-tool
    xcopy "%swt_path%" "%tmp_dir%\%swt_path%" /I /E /C /G /R /Y /Q > nul
    copy /B /D /Y lib\common.jar         "%tmp_dir%\lib\"          > nul
    copy /B /D /Y lib\commons-codec*     "%tmp_dir%\lib\"          > nul
    copy /B /D /Y lib\commons-compress*  "%tmp_dir%\lib\"          > nul
    copy /B /D /Y lib\commons-logging*   "%tmp_dir%\lib\"          > nul
    copy /B /D /Y lib\dvlib.jar          "%tmp_dir%\lib\"          > nul
    copy /B /D /Y lib\gson*              "%tmp_dir%\lib\"          > nul
    copy /B /D /Y lib\guava*             "%tmp_dir%\lib\"          > nul
    copy /B /D /Y lib\httpclient*        "%tmp_dir%\lib\"          > nul
    copy /B /D /Y lib\httpcore*          "%tmp_dir%\lib\"          > nul
    copy /B /D /Y lib\httpmime*          "%tmp_dir%\lib\"          > nul
    copy /B /D /Y lib\layoutlib-api.jar  "%tmp_dir%\lib\"          > nul
    copy /B /D /Y lib\org-eclipse-*      "%tmp_dir%\lib\"          > nul
    copy /B /D /Y lib\sdk*               "%tmp_dir%\lib\"          > nul
    copy /B /D /Y lib\swtmenubar.jar     "%tmp_dir%\lib\"          > nul

    rem jar_path and swt_path are relative to PWD so we don't need to adjust them, just change dirs.
    set tools_dir=%cd%
    cd /d "%tmp_dir%"

:EndTempCopy

rem The global ANDROID_SWT always override the SWT.Jar path
if defined ANDROID_SWT set swt_path=%ANDROID_SWT%

if exist "%swt_path%" goto SetPath
    echo ERROR: SWT folder '%swt_path%' does not exist.
    echo Please set ANDROID_SWT to point to the folder containing swt.jar for your platform.
    goto :EOF

:SetPath
rem Finally exec the java program and end here.
REM set REMOTE_DEBUG=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
call "%java_exe% %REMOTE_DEBUG%" "-Dcom.android.sdkmanager.toolsdir=%tools_dir%" "-Dcom.android.sdkmanager.workdir=%work_dir%" -classpath "%jar_path%;%swt_path%\swt.jar" com.android.sdkmanager.Main %*

rem EOF

java应用程序的执行提示许可证接受,这就是我想将"y"传递给它的原因。如果我直接管道它可以工作,但如果我通过Powershell的呼叫操作员管道它不会。

这有效:

"y" | android.bat update sdk -u -a -t 38

这不是:

$command = "android.bat"
"y" | & $command update sdk -u -a -t 38

任何人都可以启发我吗?

1 个答案:

答案 0 :(得分:2)

我相信使用&调用可执行文件要求参数是字符串数组而不仅仅是字符串。我认为PowerShell对如何处理这些输入感到困惑。因此,根据您的示例,类似这样的内容应该正确传递输入和参数:

$command = ".\script1.bat"
$x = "update sdk|-u|-a|-t 38"
"myinput" | & $command $x.Split("|")

导致输出

Enter: foo "update sdk" -u -a "-t 38" myinput