简体   繁体   English

使用PHP的Powershell脚本

[英]Powershell script with PHP

I am new to PHP and using WAMP and trying to do ...call an powershell script using php but it doesnt show my any output upon submit . 我是不熟悉PHP的人,正在使用WAMP并尝试做...使用php调用Powershell脚本,但提交时不显示我的任何输出。

Here is code: 这是代码:

   HTML :
  <html>
  <head>
  <title>Tool</title>
  <body><center><h1>Welcome</h1><center/>
  <p><img src="images/google.gif"></p>
  <form action="welcome.php" method="post">
  <input type="text" name="number"><br><br>
  <input type="submit" value="Submit">
 </form>

   welcome.php code :

   <html>
   <body>
   <?php
   $CMD = 'powershell -command C:\wamp\www\Badge\scripts\badge.ps1 ' . $_POST['number'];
   ?>
   </body>
  </html>

I just get a blank page but it redirects the page to localhost/badge/welcome.php upon submit 我只是得到一个空白页,但是提交后它将页面重定向到localhost / badge / welcome.php

My powershell code runs if i give like this 如果我这样给我的PowerShell代码运行

  powershell.exe -command C:\wamp\www\Badge\scripts\badge.ps1 12345 and it displays my name.

Even i tried having this in php as well : 甚至我也尝试在php中使用它:

 <?php
 shell_exec ($_POST("powershell.exe -command C:\wamp\www\Badge\scripts\badge.ps1 ['number']");
 shell_exec("exit");
  ?>

So any ideas on what might be wrong or what changes i need to make call the text value of html to powershell using php . 因此,关于什么可能是错误的或需要进行哪些更改的任何想法,我都需要使用php将html的文本值调用到powershell。

You don't need the command flag if you're just passing the script. 如果您只是传递脚本,则不需要命令标志。 Also, I find that when I execute the script via PHP it gets blocked by the default ExecutionPolicy setting, you can overwrite it with a flag. 另外,我发现当我通过PHP执行脚本时,默认的ExecutionPolicy设置阻止了该脚本,您可以用一个标志将其覆盖。 Try this: 尝试这个:

$cmd = 'powershell.exe -ExecutionPolicy RemoteSigned C:\wamp\www\Badge\scripts\badge.ps1 '.$_POST['number'];
if(exec($cmd, $out)){
    echo join($out);
}

1.Firts, before you can using powershell on php you must open the powershell on Windows(no using php) and type this script in powershell window (without quotes) "Set-ExcetuionPolicy RemoteSigned". 1.首先,在php上使用powershell之前,必须在Windows上打开powershell(不使用php),然后在powershell窗口中键入此脚本(不带引号)“ Set-ExcetuionPolicy RemoteSigned”。 To verify this change type this command on window powershell(without quotes) "Get-ExecutionPolicy" if you show "RemoteSigned" text, your changes is successfully. 要验证此更改,请在窗口Powershell(不带引号)“ Get-ExecutionPolicy”上键入此命令(如果显示“ RemoteSigned”文本),则更改成功。

2.Second, now you can using php to access powershell. 2.其次,现在您可以使用php访问powershell。 You can't using double quotes in exec comand to powershell. 您不能在exec comand和powershell中使用双引号。 I don't why but when i try that return error. 我不为什么,但是当我尝试返回错误时。 Try this code with single quotes command. 尝试使用单引号命令将此代码。

$output = array();
$return_code = 0;
$last_line = exec('powershell.exe Write-Host  Hello World!  ', $output, $return_code);

echo "<pre>";
 print_r($output); 
echo "</pre>";

And this code with ps script. 而这段代码带有ps脚本。

$output = array();
$return_code = 0;
$last_line = exec('powershell.exe C:\xampp\htdocs\powershell\powerscript2.ps1  2>&1 ', $output, $return_code);

echo "<pre>";
 print_r($output); 
echo "</pre>";

And this sample powerscript2.ps1 而这个样本powerscript2.ps1

# Filename: Hello.ps1
Write-Host
Write-Host 'Hello World!'
Write-Host "Good-bye World! `n"
# end of script   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM