如何使用2个参数运行批处理的计划任务?

如何使用2个参数运行批处理的计划任务?

我有一个批处理文件,需要每 15 分钟运行一次。

当我双击该批次时:

  1. test需要输入要运行的名称,并且它始终是Scripttest.jmx,如下所示:

在此处输入图片描述

  1. 需要y在此SS中输入:

在此处输入图片描述

  1. 此后批处理成功运行。

我正在尝试创建计划任务,并且正在执行以下操作:

在此处输入图片描述

但它不起作用。对于上述场景,我究竟应该如何在计划任务中编写代码?谢谢,Inna

答案1

请尝试制作以下批处理文件:(打开记事本并粘贴以下代码,然后将其另存为"MyCustomBatch.bat"

cd "C:\Users\innashn\QVScalabiltiy\ScriptExecutor"
.\ExecuteTests.bat
Scripttest.jmx
y

这应该将目录更改为 executetasks 批处理文件所在的位置(第 1 行),然后它将运行该批处理文件(第 2 行)并在自动前进时输入自定义命令(第 3 行和第 4 行)。

答案2

我感觉你需要传递输入,而不是确切的参数。

为了确认,我尝试下载并验证您问题中使用的工具。

然后我意识到你正在使用一个 bat 来调用另一个要求数据输入的 bat(不是真正的参数,而是输入)。

为了在您的案例中提供帮助,我选择建议您复制最初使用的 bat 文件ExecuteTests.bat,将其重命名为类似的名称,并添加下面和行ExecuteTests2.bat的版本并添加必要的参数以便您在调度中使用它。bolditalic


@echo off

cls && cd /d "%~dp0"

setlocal enabledelayedexpansion

:readBatParam
if /I [%~1]==[debug] set "debug=on"
if /I [%~x1]==[.jmx] set "testInput=%~nx1"

:readConfig
REM ## Fetch the PATH of the JMeter binary from the config file ##
REM ## Fetch JVM Heap Size from the config file ##

:JmeterPath
for /f "eol=# tokens=*" %%a in ('FIND "JMeter Installation Path" "config.txt"') do (
  for /f "delims== tokens=2" %%i in ("%%a") do (
    set JmeterPath=%%i
:removeLeftSpace
if "!JmeterPath:~0,1!"==" " set "JmeterPath=!JmeterPath:~1!"&GOTO :removeLeftSpace
:removeRightSpace
if "!JmeterPath:~-1!"==" " set "JmeterPath=!JmeterPath:~0,-1!"&GOTO :removeRightSpace
  )
)

:HeapSize
for /f "eol=# tokens=*" %%a in ('Find "JVM Heap Size" "config.txt"') do (
  for /f "delims== tokens=2" %%i in ("%%a") do (
    set heapSize=%%i
    set heapSize=!heapSize: =!
  )
)

if [%heapSize%]==[] (
set heapSize=3072
)



REM ## Verify the path is correct to avoid error when execute the JMeter binary. ##
:checkJMTpath
dir "%JmeterPath%" | FIND "ApacheJMeter.jar" >NUL
rem echo %errorlevel%
if errorlevel 1 goto :JMTpathError

:start
echo.
echo                Welcome To The Generic Test Suite
echo  **********************************************************************
echo.
echo * The available test script(s) to run :
echo.

REM ## List all .jmx files under .\DestJMXs folder ##
CD .\DestJMXs
for %%a in (*.jmx) do (
  echo   + "%%a"
)

echo.
echo Please choose the test(s) you want to run. You can :
echo . Run ALL test scripts by entering '*'. 
echo . Run A single test script by entering the file name.
echo . Run a GROUP of test scripts sharing the common text in file name by  
echo   entering 'TEXT*' (replace the TEXT part with the common text).
echo . EXAMPLES: -  "*";  "Films";  "Fi*" .
:selectTest
echo.
if /I Not "%testinput%" == "%~nx1" ( 
     set testInput=
     set /p testInput=Enter the TEST(s) you want to run :
    )
    
IF /i "%testInput%" == "" GOTO :selectTest
REM ## Remove empty space from the input, to avoid JMeter execution error. ##
  for /f "delims=. tokens=1" %%i in ("%testInput%") do (
    set testInput=%%i
    set "testInput=!testInput: =!"
  )
IF /i "%testInput%" == "" GOTO :selectTest

REM ## Check the input file name are existing, to avoid the execution error. ##
:checkTest
echo.
echo * The test script(s) you have chosen :
echo.
for %%a in (%testInput%.jmx) do (
  echo   -"%%a"
)  
echo.
DIR %testInput%.jmx >NUL
rem echo %errorlevel%
if errorlevel 1 goto :404Error

:confirmTest
if /I not  "%~2" == "y" ( 
     set confirmInput=
     set /p confirmInput=   DO YOU WANT TO PROCEED? : [Y/n] 
    ) else (
     IF /i "%confirmInput%" == "" GOTO :executeTest
     IF /i "%confirmInput%" == "Y" GOTO :executeTest
     IF /i "%confirmInput%" == "N" GOTO :selectTest
  )
    
:executeTest
rem setlocal disabledelayedexpansion
CD ..
SET wdir="%cd%\DestJMXs"
for %%a in (DestJMXs\%testInput%.jmx) do (
  rem echo "%%a"
  set testName=%%a
  start "JMeter Test Window" call .\Executor\JMeterRunGen.bat
  timeout /t 2 /nobreak >NUL
)
pause

:end
GOTO:EOF

:JMTpathError
echo.
echo                             xxxxxxxx
echo                               ERROR
echo   Can not find "ApacheJMeter.jar" file under the path you input:
echo   "%JmeterPath%". 
echo   Please check the setting in your "config.txt" file.
echo.
pause
GOTO:EOF

:404Error
rem echo.
echo ERROR: Please check your file "%testInput%.jmx" exists in ".\DestJMXs\" directory.
pause
GOTO:EOF

观察:1答案是考虑到您将使用输入的 bat 文件是: %UserProfile%\QVScalabilityTools\ScriptExecutor<b>ExecuteTests2.bat

观察:2版本发生的线路分别建议为:

003:  cls && cd /d "%~dp0"

008:  if /I [%~1]==[debug] set "debug=on"
009:  if /I [%~x1]==[.jmx] set "testInput=%~nx1"

069:  if /I Not "%testinput%" == "%~nx1" ( 
070:       set testInput=
071:       set /p testInput=Enter the TEST(s) you want to run :
072:      )

096:  if /I not  "%~2" == "y" ( 
097:       set confirmInput=
098:       set /p confirmInput=   DO YOU WANT TO PROCEED? : [Y/n] 
099:       ) else (
100:        IF /i "%confirmInput%" == "" GOTO :executeTest
101:        IF /i "%confirmInput%" == "Y" GOTO :executeTest
102:        IF /i "%confirmInput%" == "N" GOTO :selectTest
103:   )

观察:3编辑使你的 bat 使用%~1%~2参数,它们分别用于相关输入:

..\ExecuteTests2.bat Scripttest.jmx y

相关内容