/    Sign up×
Community /Pin to ProfileBookmark

Using a PowerShell command within PHP works until I try this

In a PowerShell console a XenApp cmdlet works simply like this. I get all sessions for the designated Citrix user:

[CODE]Get-XASession | where {$_.accountname -LIKE “domainJoeB”}[/CODE]

That’s simply pure PowerShell.. Now I am able to run PowerShell commands in a PHP script like this:

[CODE]<?php
$my_array2 = array();
exec(‘powershell.exe -command “Get-XASession | Select -Property accountname, logontime ” <NUL ‘, $my_array);`
?>[/CODE]

Simple stuff, but having all Citrix sessions sent to a web page is not very useful. Rather I want to filter the User ID in PHP as I did in the first code example.

However I can’t do this:

[CODE]<?php
$my_array = array();
exec(‘powershell.exe -command “Get-XASession | Select -Property accountname, logontime | where {$_.accountname -LIKE “domainJoeB” } ” <NUL ‘, $my_array);`
?>[/CODE]

As a result I get all sessions in the Citrix farm as the filter for accountname is not read in. It’s seemingly a PHP issue wherein my inclusion of double quotes to define the value of accoutname are messing up the PHP. The command is being truncated prematurely is my guess.

So next I tried this, hoping to have those double quotes surrounding domainjoeb be escaped from the PHP:

[CODE]exec(‘powershell.exe -command “Get-XASession | Select -Property accountname, logontime | where {$_.accountname -LIKE ‘. “domainJoeB” . ‘} ” <NUL ‘, $my_array);`[/CODE]

I get nothing returned. I ultimately want to do this with a variable captured from an HTML form wherein the user supplies the accountame, but for now am just trying to see if this can work with a hard-coded User ID. I think I’m being PHP syntax challenged.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@jedaisoulJul 27.2015 — Hi and welcome to the site. I know nothing about PowerShell, but I know that embedding double quotes in double quotes doesn't work in any language! Try using single quotes for the inner quotes.

PS Oops, just noticed you're already using single quotes. Hmm I vaguely remember that square brackets [] can be used, I'll have to check...
Copy linkTweet thisAlerts:
@TomKazauthorJul 28.2015 — Hi and welcome to the site. I know nothing about PowerShell, but I know that embedding double quotes in double quotes doesn't work in any language! Try using single quotes for the inner quotes.

PS Oops, just noticed you're already using single quotes. Hmm I vaguely remember that square brackets [] can be used, I'll have to check...[/QUOTE]


  • - Thanks for the suggestion but square brackets do not make a difference though. -TK
  • Copy linkTweet thisAlerts:
    @jedaisoulJul 28.2015 — My bad, completely different language! I've parsed the command line and I suspect you need to escape:
    <i>
    </i>"domain\JoeB"


    However you have used single quotes in the PHP, so the escaping will fail. Hence you also need to swap double and single quotes, (except for the escaped ones above), and delete the spurious single quote at the end. So you get:
    <i>
    </i>&lt;?php
    $my_array = array();
    exec("powershell.exe -command 'Get-XASession | Select -Property accountname, logontime | where {$_.accountname -LIKE "domain\JoeB" } ' &lt;NUL ", $my_array);
    ?&gt;


    If that does not work, then I do not know what will... ?
    Copy linkTweet thisAlerts:
    @TomKazauthorJul 29.2015 — My bad, completely different language! I've parsed the command line and I suspect you need to escape:
    <i>
    </i>"domain\JoeB"


    However you have used single quotes in the PHP, so the escaping will fail. Hence you also need to swap double and single quotes, (except for the escaped ones above), and delete the spurious single quote at the end. So you get:
    <i>
    </i>&lt;?php
    $my_array = array();
    exec("powershell.exe -command 'Get-XASession | Select -Property accountname, logontime | where {$_.accountname -LIKE "domain\JoeB" } ' &lt;NUL ", $my_array);
    ?&gt;


    If that does not work, then I do not know what will... ?[/QUOTE]



    I appreciate the advice. I get this however:
    [CODE]unexpected '.', expecting '}'[/CODE]
    This may just be too complicated so I probably need to find another way to run these PowerShell commands in a web format.

    (Also I don't know why in my original postings there appears to be a tick mark at the very end of my line of code, which may only be confusing maters. The last character should only be the PHP semi-colon.)
    Copy linkTweet thisAlerts:
    @jedaisoulJul 29.2015 — I appreciate the advice. I get this however:
    [CODE]unexpected '.', expecting '}'[/CODE]
    [/QUOTE]

    The error is here:
    <i>
    </i>{$_.accountname -LIKE "domain\JoeB" }

    Which looks to me like pure Powershell. Can you test the Powershell syntax directly? Failing that I'd try deleting the '.' to see what occurs?
    ×

    Success!

    Help @TomKaz spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...