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!

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分