How to pass php array to local powershell script

I am writing a powershell script that automates the setup of WordPress. This script will be used by multiple people when creating their sites, and id like to add a bit of extra functionality.

I would like to have my peers access a php page which would allow for them to pick and choose themes and plugins for their page, and then pass their choices (I assumed its easiest through an array) to my powershell scripts. The reason the powershell script must handle this is because after they have picked their files, the powershell script can connect to an internal server and handle the rest.

Read More

I guess my question comes in two parts; first being is the above even possible? and if so does it seem like a logical way to handle this problem?

Second would be how to actually achieve this? Below is my php/html code to handle the download page which will hopefully be able to pass an array to my script. (The code below downloads from a ‘temp’ site for testing purposes and does not really meet my requirements)

<?php

$val=$_POST['theme']; 
if($val == 'aa') {
header('Content-disposition: attachment; filename=all-in-one-slideshow.zip');
header('Content-type: application/zip');
$url  = 'http://mypage.com/wordpressFiles/all-in-one-slideshow.zip';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $path);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
} else if($val == 'bb') {
} else {
}
?>


<form action="download.php" method="post">
<select name="theme" id="theme" class="select-list">
<option value="">Choose A Theme</option>                                       
<option value="aa">All-In-One-Slideshow</br>
<option value="bb">BB</br>
</select>   
<input type="submit" value="Download" />
</form>

Thank you in advance for any help.

Related posts

Leave a Reply

1 comment

  1. If you execute your PowerShell script directly from the web server, you could pass arguments with a system call (exec(), passthrough(), shell_exe()) to powershell.exe. You just need to make sure the arguments are passed in the same order as PowerShell executes them.

    If the argument list or data would be too long, you can create a temporary files. The best way would be doing a delimiter-based setup (like CSV): you create a text file from the array with PHP, save that (now string) into the file, and then in PowerShell, you chunk the file up around the same delimiters. You can use serialize() too, but I am unsure whether there is an ability to unserialize() in PowerShell.

    Of course, using the above method, PowerShell only needs one argument, the path of the temporary file where you stored your array.

    Also, you should look at the following question as it discusses How to pass command-line arguments to a PowerShell ps1 file.