1

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.

2 Answers 2

2

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');
Sign up to request clarification or add additional context in comments.

3 Comments

Ive changed my code to: echo exec('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe "C:\\Wordpress Setup\\setupThemes.ps1" $chooseTheme 2>&1'); And I receive the error: sh: C:WindowsSystem32WindowsPowerShellv1.0powershell.exe: command not found By command not found I assume this means the server is trying to run the script but does not have access to my local machine? Sorry again not sure the solution for this.
check if the apache (or IIS) user have read permissions in windows folder
yes, it seems is permission problem, see this comment in the php.net page: php.net/manual/es/function.exec.php#101837 (about windows and exec)
0

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.