1

I want to execute a PowerShell script using php. Using cmd it is working fine, but it is not working using php and I am using wamp server.

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'. 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.

1
  • Please provide the contents of Input.yaml. Commented Mar 6, 2018 at 5:57

2 Answers 2

1

It was problem with python code which I was calling in powershell script, I have solved it.Now it is working fine.

In php I have used like this,

<?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>' );          
 ?>
Sign up to request clarification or add additional context in comments.

Comments

0

Just change $psDIR to your PowerShell path (e.g. %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>' );
?>

4 Comments

I have tried this one it is not giving any output or any error.II have mentioned like this in $psDIR %SYSTEMROOT%\System32\WindowsPowerShell\v1.0.
Can you change %SYSTEMROOT% like this $psDIR = "C:\Windows\System32\WindowsPowerShell\v1.0";
It was problem with AWS python code, I have solved it.Now it is working fine. In php I have used like this,<?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>' ); ?>
That's nice to hear mate. Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.