Passing Params from PHP to Powershell exec

Deadly 提交于 2019-12-11 11:47:39

问题


I am attempting to write a PHP script that will allow for me to select a few files to download from a predetermined location. I'd like my script to pass an array to a Powershell script that id written earlier and have my Powershell script handle the downloading (basically the php file just needs to tell the powershell file what needs to be downloaded). I've looked at a few options, and it seems that exec is the command I should use for this (as I do not care about command line output I shouldnt need shell_exec).

So far I've turned OFF safe mode to allow me to use this command. I should also note that the php file will be run from a server, however the powershell files are located on a local machine.

A snippet of the code so far to handle the param passing looks like this:

if(isset($_POST['formSubmit'])) 
{
    $choosePlugin = $_POST['wpPlugin'];
    $chooseTheme = $_POST['wpTheme'];

    if(isset($_POST['wpTheme'])) 
    {
        echo("<p>You selected: $chooseTheme</p>\n");
        exec('powershell.exe C:\Wordpress Setup\setupThemes.ps1 $chooseTheme');

    } 
    else 
    {
        echo("<p>You did not select a theme</p>\n");
    }

I am a bit confused as to what I should put inside the exec. When I run the above code there are no errors however nothing happens. I am a bit new to this so I apologize if more information is required. Any help is appreciated thank you.


回答1:


Try to do:

echo exec('powershell.exe C:\\Wordpress Setup\\setupThemes.ps1 $chooseTheme');

to see the results of powershell.exe (remember the double \), also make sure to put the absolute path to the exe file:

 echo exec('c:\\PATH_TO_POWERSHELL.EXE\\powershell.exe C:\\Wordpress Setup\\setupThemes.ps1 $chooseTheme');



回答2:


If you want to pass the contents of the variable you should use double quotes to actually expand it, I guess. Furthermore you should quote the script name because the path contains spaces:

exec("powershell.exe \"C:\Wordpress Setup\setupThemes.ps1\" $chooseTheme");


来源:https://stackoverflow.com/questions/11617938/passing-params-from-php-to-powershell-exec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!