简体   繁体   English

从 php 执行 PowerShell 脚本

[英]Execute PowerShell script from php

I want to execute a PowerShell script using php.我想使用 php 执行 PowerShell 脚本。 Using cmd it is working fine, but it is not working using php and I am using wamp server.使用 cmd 它工作正常,但它不能使用 php 而我使用的是 wamp 服务器。

My code is as follows:我的代码如下:

<?php
ini_set('max_execution_time', 300);
//$output=shell_exec('cd C:\AWS\Distributed-setup-3');
//$output=shell_exec('cd');
$output=shell_exec('powershell.exe -command C:\AWS\Distributed-setup-3\AWSExecuter.ps1');
echo $output;
?>

Here I am using Input.yaml file for taking inputs it is giving error as, Problem in Input file 'Input.yaml'.在这里,我使用Input.yaml文件进行输入,它给出的错误是输入文件“Input.yaml”中的问题。 But for manual execution I am using same input file.但是对于手动执行,我使用相同的输入文件。 There it will not give any error.在那里它不会给出任何错误。

Can anyone help me?谁能帮我? Since three days I am working on this.从三天开始,我就在做这件事。 I am getting how to debug this one.我正在了解如何调试这个。

It was problem with python code which I was calling in powershell script, I have solved it.Now it is working fine.我在powershell脚本中调用的python代码有问题,我已经解决了。现在它工作正常。

In php I have used like this,在 php 中,我是这样使用的,

<?php
        ini_set('max_execution_time', 300);
        $output= shell_exec('powershell -command C:/AWS/Distributed-setup-3/AWSExecuter.ps1');
        echo( '<pre>' );
        echo( $output );
        echo( '</pre>' );          
 ?>

Just change $psDIR to your PowerShell path (eg %SystemRoot%\\system32\\WindowsPowerShell\\v2.0)只需将 $psDIR 更改为您的 PowerShell 路径(例如 %SystemRoot%\\system32\\WindowsPowerShell\\v2.0)

<?php
ini_set('max_execution_time', 300);
$psPath = "powershell.exe";
$psDIR = "PathToPowrshell";
$psScript = "C:\AWS\Distributed-setup-3\AWSExecuter.ps1";
$runScript = $psDIR. $psScript;
$runCMD = $psPath." ".$runScript;
$output= shell_exec($runCMD);
echo( '<pre>' );
echo( $output );
echo( '</pre>' );
?>

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

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