将参数从javascript函数传递到批处理文件

passing parameters from javascript function to batch file

本文关键字:批处理文件 函数 参数 javascript      更新时间:2023-09-26

我正在尝试为客户端创建一个html页面,他将能够启动或停止任何远程桌面上的特定窗口服务。总没有。范围内的服务大约有20个。而不是创建20(开始)+ 20(停止)+ 20(重新启动)批处理文件,我想有一个批处理文件与if else条件,我可以在javascript函数中传递参数。html页面代码如下:

<!DOCTYPE html>   
<html>
<head>
<script type="text/javascript">
MyObject = new ActiveXObject("WScript.Shell")
function start()
{
MyObject.Run("'"E:''start.bat'"");
}
function stop()
{
MyObject.Run("'"E:''stop.bat'"");
}
function restart()
{
MyObject.Run("'"E:''restart.bat'"");
}
</script>
<title>Our Company</title>
</head>
<body>
<table background-color:#f1f1c1; border="1" style="width:50%" align="center">
<tr>
<th style="text-align:center">Instance Name</th>
<th style="text-align:center">Accessible URL</th> 
<th style="text-align:center">Status</th>
<th style="text-align:center">Start</th>
<th style="text-align:center">Stop</th>
<th style="text-align:center">Restart</th>
</tr>
<tr height="70%">
<td style="text-align:center">netadds</td>
<td style="text-align:center">
<a href="http://localhost/">localhost:netadds</a></td> 
<td style="text-align:center">Running</td>
<td style="text-align:center"><button onclick="start()">Start</button></td>
<td style="text-align:center"><button onclick="stop()">Stop</button></td>
<td style="text-align:center"><button onclick="restart()">Restart</button>          </td>
  </tr>
</table>
</body>
</html>

和start.bat中的代码是:

@echo off
net start AMAPTestJetty8i0T1
if ERRORLEVEL 1 goto error
exit
:error
echo Could not start service
pause

请建议如何在批量中应用if else语句并在开始函数中传递参数。

将参数传递给BAT文件:

MyObject.Run("E:'start.bat The_parameter1 The_parameter2");

获取BAT文件中的参数:

echo parameter 1 = %1
echo parameter 2 = %2

在BAT中进行IF ELSE测试

net start AMAPTestJetty8i0T1
if %ERRORLEVEL%==1 (goto error
    ) else (
 goto NoError)

或者使用重定向

net start AMAPTestJetty8i0T1 && goto NoError || goto Error