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.

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办