call powershell from php
Posted by: kunnu (---.amazon.com)
Date: December 17, 2013 10:04AM

Hi All ,

I am new to PHP and WAMP . So its an very excitement to me in doing this .

I am trying to call an powershell script using php but it doesnt show my any output upon submit .

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 < NUL';
if ( $a = shell_exec($CMD) ) {
echo "<span style='color:blue;'>$a</span><br/><br/>";
}

?>
</body>
</html>

If I run this powershell code like this it gives the output .



So when i execute this php code no display comes just normal blank page .



My powershell code runs if i give like this

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


So any ideas on what might be wrong...

Options: ReplyQuote
Re: call powershell from php
Posted by: RiggsFolly (---.as13285.net)
Date: December 17, 2013 11:30AM

Froma quick search using 'php powershell shell_exec'

[stackoverflow.com]

Hope this helps

---------------------------------------------------------------------------------------------
(Windows 10 Pro 64bit) (Wampserver 3.3.4 64bit) Aestan Tray Menu 3.2.5.4
<Apache versions MULTIPE> <PHP versions MULTIPLE> <MySQL Versions MULTIPLE>
<MariaDB versions MULTIPLE> <phpMyAdmin versions MULTIPLE> <MySQL Workbench 8.0.23>

Read The Manuals Apache -- MySQL -- PHP -- phpMyAdmin
Get your Apache/MySQL/mariaDB/PHP ADDONs here from the WAMPServer alternate Repo
-X-X-X- Backup your databases regularly Here is How dont regret it later! Yes even when developing -X-X-X-

Options: ReplyQuote
Re: call powershell from php
Posted by: kunnu (---.amazon.com)
Date: December 17, 2013 12:38PM

So, I tried changing my php code like this :

<?php


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

?>


It gives an error with

) Fatal error: Function name must be a string in C:\wamp\www\Badge\welcome.php on line 30
Call Stack
# Time Memory Function Location
1 0.0000 141952 {main}( ) ..\welcome.php:0


Can you please let me know how i can get the value of number from html to php so that the value will be passed into powershell .


Thanks in advance

Options: ReplyQuote
Re: call powershell from php
Posted by: RiggsFolly (---.as13285.net)
Date: December 17, 2013 12:48PM

No, I have never used it.

All I can say is that the $_POST array will not have a key like that so why are you doing that.

This is not a WAMPServer issue so this is not the right place to ask the question.
This is a 'I dont know how to code this PHP syntax' question.

You are going to have to do some of your own research with google.

Alternatively ask the question on StackOverflow.


I might give this a try though:


$CMD = 'powershell -command C:\wamp\www\Badge\scripts\badge.ps1 ' . $_POST['number'] . ' 2>&1';

---------------------------------------------------------------------------------------------
(Windows 10 Pro 64bit) (Wampserver 3.3.4 64bit) Aestan Tray Menu 3.2.5.4
<Apache versions MULTIPE> <PHP versions MULTIPLE> <MySQL Versions MULTIPLE>
<MariaDB versions MULTIPLE> <phpMyAdmin versions MULTIPLE> <MySQL Workbench 8.0.23>

Read The Manuals Apache -- MySQL -- PHP -- phpMyAdmin
Get your Apache/MySQL/mariaDB/PHP ADDONs here from the WAMPServer alternate Repo
-X-X-X- Backup your databases regularly Here is How dont regret it later! Yes even when developing -X-X-X-

Options: ReplyQuote


Sorry, only registered users may post in this forum.