Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

executing a Powershell script from php

I'm trying to execute a powershell script from PHP, but it does not seem to work.

The script 'newEvent.ps1' creates an event on the Exchange server.

$psPath = "powershell.exe";
$psDIR = "C:\wamp\www\ant\assets\ps";
$psScript = "newEvent.ps1";
$runScript = $psDIR. $psScript;
$runCMD = $psPath." ".$runScript." 2>&1"; 

echo "$psPath  $psPath <br>";
echo "$psDIR  $psDIR <br>";
echo "$psScript  $psScript <br>";
echo "$runScript  $runScript <br>";
echo "$runCMD   $runCMD  <br>";

exec( $runCMD,$out,$ret);

echo "<pre>";
print_r($out);
print_r($ret);
echo "</pre>";

It outputs:

$psPath powershell.exe
$psDIR C:wampwwwantassetsps
$psScript newEvent.ps1
$runScript C:wampwwwantassetsps
ewEvent.ps1
$runCMD powershell.exe C:wampwwwantassetsps
ewEvent.ps1 2>&1

Array
(
    [0] => File C:wampwwwantassetsps
ewEvent.ps1 cannot be loaded because the execut
    [1] => ion of scripts is disabled on this system. Please see "get-help about_signing"
    [2] => for more details.
    [3] => At line:1 char:39
    [4] => + C:wampwwwantassetsps
ewEvent.ps1 <<<<
    [5] =>     + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    [6] =>     + FullyQualifiedErrorId : RuntimeException
    [7] => 
)

If I run powershell.exe C:wampwwwantassetsps ewEvent.ps1 on the command-line, it works fine.

This is the first time im attempting something like this. I ran Set-ExecutionPolicy RemoteSigned -Scope LocalMachine but it still gives me the same error. In fact I ran Set-ExecutionPolicy unristricted, but it's still the same.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like your command is surrounded by single-quotes. I think if you remove them, your command should run.

shell_exec returns the output from the command you run. To further diagnose, store the output in a variable, then print it out:

$output = shell_exec($runCMD);
echo '<pre>' . $output . '</pre>';

Make sure you enable running scripts. That capability is turned off by default. You have to enable the execution of scripts on each machine you want to run PowerShell scripts. Run about help_signing for more information.

Microsoft recommends running Set-ExecutionPolicy RemoteSigned -Scope LocalMachine. This allows all user accounts on a machine to run local scripts without issue, but requires confirmation to run scripts downloaded from the internet. This needs to be run in an administrative prompt. If you are running a 64-bit operating system, you'll need to do this from both a 64-bit and 32-bit shell.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...