douzhang8144 2015-07-16 19:56
浏览 44

PHP语法挑战

I'm a Citrix admin, a journeyman PowerShell user, a beginner using PHP. I have though managed to create several PHP pages that run simple PowerShell commands as in this example:

PHP snippet example showing how I successfully run POSH commands:

<?php 
  $my_array = array(); 
  exec('powershell.exe -command "Get-XAAdministrator|Select -Property AdministratorName,AdministratorType" <NUL ', $my_array);
?>

However when I try to utilize a POSTED variable from a preceding HTML page that asked for a USER ID I can extract that variable just fine in the PHP page. I can echo and display it as JoeB for example on the resultant PHP page. But it needs to be entered into the PHP/PowerShell syntax encased in double quotes.

Thus this PHP snippet does not work:

<?php 
  $my_array = array();
  exec('powershell.exe -command "Get-XASession | Select -Property servername, accountname, logontime | where {$_.accountname -LIKE $ID}" <NUL ', $my_array);`

This is how it would look and work in a normal powershell console:

Get-XASession  | select -Property servername, accountname, logontime | where { $_.accountname -LIKE "JoeB" } 

(Note that the USER ID JoeB is in double quotes.)

Back to the PHP attempt to do this: - The variable $ID is the USER ID entered into the prior HTML form.

  • It is being extracted from the HTML form as simply JoeB.

  • And if it is entered into the HTML form as "JoeB" and extacted likewise it still does not work.

  • The PHP/PowerShell command actually works, but it displays all XenApp Sessions as it is failing to properly read the value of $ID (accountname) and thus filter the results.

Any guidance is appreciated.

  • 写回答

1条回答 默认 最新

  • duanpai1033 2015-07-16 20:15
    关注

    You are looking for something like the following:

    <?php 
    $my_array = array();
    exec('powershell.exe -command "Get-XASession | Select -Property servername, accountname, logontime | where {$_.accountname -LIKE '.$ID.'}" <NUL ', $my_array);
    

    The problem you are encountering, is that you cannot substitute a variable in single quotes as you have done there. It only works with double quotes. What I did, is end the string with a quote ' and use a dot . which means "concatenate" and then $Id which is a variable (as a string from POST), and then concatenate again the rest of the string.

    Good luck!

    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R