user3111077
user3111077

Reputation: 13

Run powershell in php

I have a very small program which I want to execute upon button press . In html it contains an button with an image and when i press the button it should call my php script which has powershell and has to be executed . But it doesnt add the printer through the manual command attached is working.

My html code here is as follows :

<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 code :

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

Even I tried in this way and it just echo's the command :

 <?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;
 ?>

Moreover I get the output as in the page as :

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

My powershell code contains like this :

(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\file-computer-01\IN-SEA-GOOGLE")

If I execute the direct powershell script it works but not via PHP .

In both the cases, it just gives echo and doesnt add the printer .

Any ideas or suggestions please.... as i new to php struggling a lot to figure it out this.

Thanks,

Upvotes: 0

Views: 6351

Answers (2)

Katya S
Katya S

Reputation: 1353

It just echo's the command because that's what your code tells it to do :) Try replacing

echo $runCMD;

with

echo exec($runCMD);

PS also you will probably need to add the ExecutionPolicy directive to your command, because by default script execution is prohibited. So your command should be:

<?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);
?>

Upvotes: 2

You probably want PHP's system function. Or perhaps its popen...

Upvotes: 0

Related Questions