开发者

Sending commands to windows command line (cmd) from PHP

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 some开发者_如何学运维how triggered by the application?


You can use exec() for that.


Have you tried exec?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜