doubiao9775 2016-10-17 14:36
浏览 34

php中shell_exec的问题

I have a HTML form with some PHP to execute a PowerShell script.

All works fine, except for the textarea input field. If I do a single line of text in the textarea box, it works OK and launches the PowerShell script. If I insert multiples lines of text, including carriage returns, it only will take the first word/line and cut the rest.

Here is the command that launches the PowerShell script:

with one word in the textarea box:

 powershell -command file.ps1 -textarea 'test'

with multiple words in the box

powershell -command file.ps1 -textarea 'firstword

As shown above, when multiple lines are in the TextArea field, it will strip it and only send the first word. This causes the PowerShell script to fail as the terminator is missing.

Any ideas?

Here is the code:

if($isValid) {
$customerid     = $_POST["DLState"];
$changesubject  = $_POST['subject'];
$starttime      = $_POST['starttime'];
$endtime        = $_POST['endtime'];
$changedetails  = $_POST['details'];
$psScriptPath   = "*****";

$query = shell_exec("powershell -command $psScriptPath -username '$user' -subject '$changesubject' -orgid '$customerid' -textarea '$changedetails' -edt '$endtime' -sdt '$starttime' < NUL");
  }

HTML text area tag

<textarea id="txtarea" name="details" cols="40" rows="6"></textarea>
  • 写回答

2条回答 默认 最新

  • dsf4354353452 2016-10-17 15:01
    关注

    If I correctly understand your script and all those username/subject/orgid/etc are script properties for $psScriptPath then you could try to run it this way:

      $query = shell_exec("powershell -command &quot;& {$psScriptPath -username '$user' -subject '$changesubject' -orgid '$customerid' -textarea '$changedetails' -edt '$endtime' -sdt '$starttime'}&quot;");
    

    At least this works for me quite well:

    Test Script:

    PS C:\> gc c:\1.ps1
    param($App)
    & $App
    

    Invoking from CMD shell:

    C:\powershell -noprofile -command "& {c:\1.ps1 -App 'Notepad.exe'}"
    
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计