如何将php数组传递给本地的PowerShell脚本 [英] How to pass php array to local powershell script

查看:154
本文介绍了如何将php数组传递给本地的PowerShell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个自动化Wordpress设置的PowerShell脚本。这个脚本在创建他们的网站时将被多个人使用,并且id就像添加一些额外的功能。



我想让我的同龄人访问一个php页面这将允许他们为他们的页面选择和选择主题和插件,然后将他们的选择(我将其最简单的数组)传递给我的powershell脚本。 powershell脚本必须处理这个原因是因为在他们选择了他们的文件之后,powershell脚本可以连接到一个内部服务器并处理其余的。



我猜我的问题有两部分:首先是上述甚至可能吗?如果是这样,它似乎是一个合乎逻辑的方式来处理这个问题?



第二个是如何真正实现这一点?以下是我的php / html代码来处理下载页面,希望能够将数组传递给我的脚本。 (以下代码从temp站点下载,以进行测试,并不真正满足我的要求)

 <?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.phpmethod =post>
< select name =themeid =themeclass =select-list>
< option value =\>选择主题< / option>
< option value =aa>一体式幻灯片< / br>
< option value =bb> BB< / br>
< / select>
< input type =submitvalue =下载/>
< / form>

提前感谢您的帮助。

解决方案

如果您直接从Web服务器执行PowerShell脚本,则可以通过系统调用( exec()传递参数, passthrough() shell_exe())至 powershell.exe 。您只需要确保参数按照PowerShell执行的顺序传递。



如果参数列表或数据太长,可以创建一个临时文件。最好的办法就是做一个基于分隔符的设置(如 CSV ):用PHP创建一个文本文件,保存(现在字符串)到文件中,然后在PowerShell中,将文件块分成相同的分隔符。您也可以使用 serialize(),但我不确定PowerShell中是否有能力 unserialize()



当然,使用上述方法,PowerShell只需要一个参数,即存储阵列的临时文件的路径。



此外,您应该查看以下问题,因为它讨论了如何将命令行参数传递给PowerShell ps1文件


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.

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.

解决方案

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.

这篇关于如何将php数组传递给本地的PowerShell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆