Sending commands to windows command line (cmd) fro

2019-08-27 18:29发布

Is this possible?

What I want to do is send:

run_app.exe -param 'test' -name 'tester'

to a windows cmd line from PHP.

Is this possible or do I need to write a windows service that is somehow triggered by the application?

5条回答
做个烂人
2楼-- · 2019-08-27 19:04

Here is a project that allows PHP to obtain and interact dynamically with a real cmd terminal. Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

//if you prefer Powershell, replace 'cmd' with 'powershell'
$shellObj    = \MTS\Factories::getDevices()->getLocalHost()->getShell('cmd');

$strCmd1   = 'run_app.exe -param "test" -name "tester"';
$return1   = $shellObj->exeCmd($strCmd1);

The return will give you the command return OR error from cmd, just as if you sat at the console. Furthermore, you can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script. So instead of bundling commands in a script file, just issue them one by one using the exeCmd() method, that way you can also handle the return and any exceptions.

查看更多
爷的心禁止访问
3楼-- · 2019-08-27 19:06

You can use exec() for that.

查看更多
Animai°情兽
4楼-- · 2019-08-27 19:10

Or you can use:

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run(strCommand, [intWindowStyle], [bWaitOnReturn]);

Here you can find the run method params: http://msdn.microsoft.com/en-us/library/d5fk67ky%28v=vs.85%29.aspx

And here is the COM class doc: http://www.php.net/manual/en/class.com.php

With this method you can do so much more in windows :). I used it becaus of the [bWaitOnReturn] parameter which I couldn't do using any other method.

查看更多
放我归山
5楼-- · 2019-08-27 19:18

Have you tried exec?

查看更多
The star\"
6楼-- · 2019-08-27 19:25

I suggest using standard PHP system command. There is plenty of info and examples in official PHP site.

There is an answered question on SO for differences between exec(), system() and passthru().

查看更多
登录 后发表回答