2013-12-17 81 views
1

我是新来的PHP和使用WAMP,并试图做...调用PowerShell脚本使用PHP,但它没有显示我提交时的任何输出。PowerShell脚本与PHP

这里是代码:

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 ' . $_POST['number']; 
    ?> 
    </body> 
    </html> 

我只是得到了一个空白页面,但它重定向页面到localhost /张/时的welcome.php提交

我的PowerShell代码,如果我给这样的

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

即使我试图在PHP具有这个问题,以及:

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

因此,有什么想法可能是错误的或我需要使用PHP调用HTML的文本值PowerShell的变化。

+0

你能否添加PowerShell代码到您的文章? –

+0

如果((GET-PSSnapin |! 位置对象{$ _名-eq “Quest.ActiveRoles.ADManagement”})){ ADD-PSSnapin Quest.ActiveRoles.ADManagement } 获取-QADuser -ObjectAttributes @ {'extensionattribute10'= $ args [0]} |选择姓名 – user3111077

回答

0

如果您只是传递脚本,则不需要命令标志。另外,我发现当我通过PHP执行脚本时,它会被默认的ExecutionPolicy设置阻止,您可以用标志覆盖它。试试这个:

$cmd = 'powershell.exe -ExecutionPolicy RemoteSigned C:\wamp\www\Badge\scripts\badge.ps1 '.$_POST['number']; 
if(exec($cmd, $out)){ 
    echo join($out); 
} 
0

1.Firts,你可以在PHP中使用PowerShell之前,你必须打开在Windows(不使用PHP)的PowerShell和PowerShell中的窗口中键入该脚本(不带引号)“设置ExcetuionPolicy下RemoteSigned” 。要验证此更改,请在PowerShell窗口(不带引号)上键入此命令“Get-ExecutionPolicy”,如果显示“RemoteSigned”文本,则说明您的更改已成功完成。

2.第二,现在你可以使用PHP来访问PowerShell。您不能在exec命令中将双引号用于PowerShell。我不是为什么,但当我尝试返回错误。使用单引号命令尝试此代码。

$output = array(); 
$return_code = 0; 
$last_line = exec('powershell.exe Write-Host Hello World! ', $output, $return_code); 

echo "<pre>"; 
print_r($output); 
echo "</pre>"; 

这段代码用ps脚本。

$output = array(); 
$return_code = 0; 
$last_line = exec('powershell.exe C:\xampp\htdocs\powershell\powerscript2.ps1 2>&1 ', $output, $return_code); 

echo "<pre>"; 
print_r($output); 
echo "</pre>"; 

而这个样本powerscript2.ps1

# Filename: Hello.ps1 
Write-Host 
Write-Host 'Hello World!' 
Write-Host "Good-bye World! `n" 
# end of script