【问题标题】:Run powershell in php在 php 中运行 powershell
【发布时间】:2013-12-22 08:13:09
【问题描述】:

我有一个非常小的程序,我想在按下按钮时执行它。在 html 中,它包含一个带有图像的按钮,当我按下按钮时,它应该调用我的 php 脚本,该脚本具有 powershell 并且必须执行。但它不会通过附加的手动命令添加打印机。

我这里的html代码如下:

<html>
<head>
<form action="rabbit.php" method="POST">
<input type="image" src="image/GOOGLE.jpg" value="submit" alt="Submit" width="78" 
height="78">
</head>
</html>

php代码:

 <?php 
  echo powershell.exe (New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\\file-computer-01\IN-SEA-GOOGLE");
 ?>

即使我尝试过这种方式,它也只是回显命令:

 <?php
 $psPath = 'c:\\Windows\\System32\WindowsPowerShell\v1.0\\powershell.exe';
 $psDIR = "c:\\wamp\\www\\printer\\scripts\\";
 $psScript = "rabbit.ps1";
 $runScript = $psDIR. $psScript;
 $runCMD = $psPath.  "&". $runScript;
 echo $runCMD;
 ?>

此外,我在页面中得到的输出为:

c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe&c:\wamp\www\printer\scripts\rabbit.ps1

我的 powershell 代码包含这样的内容:

(新对象-ComObject WScript.Network).AddWindowsPrinterConnection("\\file-computer-01\IN-SEA-GOOGLE")

如果我直接执行 powershell 脚本,它可以工作,但不能通过 PHP。

在这两种情况下,它只是给出回显而不添加打印机。

请有任何想法或建议......因为我是 php 新手,为了弄清楚这一点而苦苦挣扎。

谢谢,

【问题讨论】:

    标签: php powershell


    【解决方案1】:

    它只是回显命令,因为这是您的代码告诉它执行的操作 :) 尝试替换

    echo $runCMD;
    

    echo exec($runCMD);
    

    另外,您可能还需要将 ExecutionPolicy 指令添加到您的命令中,因为默认情况下禁止执行脚本。所以你的命令应该是:

    <?php
    $psPath = 'c:\\Windows\\System32\WindowsPowerShell\v1.0\\powershell.exe';
    $psDIR = "c:\\wamp\\www\\printer\\scripts\\";
    $psScript = "rabbit.ps1";
    $runCMD = $psPath. ' -ExecutionPolicy RemoteSigned '.$psDIR.$psScript;
    
    exec($runCMD, $out);
    echo join($out);
    ?>
    

    【讨论】:

      【解决方案2】:

      您可能想要 PHP 的 system 函数。或者它的popen...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-13
        • 1970-01-01
        • 2016-09-14
        相关资源
        最近更新 更多