dpw70180 2015-11-19 12:56
浏览 75

Powershell脚本在PHP上运行时输出为空白

I am kind of new to both PHP and Powershell. I made a website to run a powershell script , which checks multiple server file directory and returns the items in that directory. On the website they display on a organized data table.

My issue is that when php exec() calls on the script, it runs but the data output is blank. Why does this happen when php runs the script?

Here is the powershell file. I even added some credential sessions, thinking it must be a credentials issue.

  Remove-PSDrive target: -ErrorAction SilentlyContinue
  $global:username = "domain\user"
  $global:password = ConvertTo-SecureString –String "MyPassword" –AsPlainText -Force
  $global:cred = new-object -typename       System.Management.Automation.PSCredential -argumentlist $global:username,  $global:password


   $output_Table = @()
   $today = get-date
   $html_output =@()
   $tableData=@()


  New-PSDrive -Name target -PSProvider FileSystem -Credential $global:cred -Root "\\serverName\filedir" | Out-Null
 $tableData+= '<table><tr><th>First Feed</th><th>Updates:DAILY</th></tr> <tr><th>FileName</th><th>FileDate</th></tr>'
 foreach ($file in (ls target:)) 
 {GetFeed(-7)}
 $tableData +='</table>'
 Remove-PSDrive -Name target -Force


 $html_output += $tableData
 $html_output += '</div>'

 $html_output | Out-File "C:\xampp\htdocs\script_results.txt" -Encoding utf8
 $today | Out-file "C:\xampp\htdocs\script_TimeStamp.txt" -Encoding utf8

The script_result.txt has the files with the HTML tags. PHP will later read this and echo it into the html page.

The PHP code that calls on this script is the following.

<?php
  if(isset($_POST['submit'])){

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

  } ?>

later on theres this piece of code to display the results. Once again, if i run the powershell script myself, it returns the script_result with the desired output. If PHP runs it, the script runs, but the output has no file data in it.

What is causing this?

  • 写回答

1条回答 默认 最新

  • duanduan8439 2016-10-29 18:08
    关注

    Here is a project that allows PHP to obtain and interact dynamically with a real Powershell. Get it here: https://github.com/merlinthemagic/MTS

    After downloading you would simply use the following code:

    $shellObj    = \MTS\Factories::getDevices()->getLocalHost()->getShell('powershell');
    
    $strCmd1   = 'Remove-PSDrive target: -ErrorAction SilentlyContinue';
    $return1  = $shellObj->exeCmd($strCmd1);
    
    $strCmd2   = '$global:username = "domain\user"';
    $return2  = $shellObj->exeCmd($strCmd2);
    
    //etc, etc
    

    This way you can issue each individual command and handle the return, if it is a permissions issue the command that fails will return the error message from Powershell. You can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度