Note, I can't get your Powershell snippet to work. When I run it, the shell gets hung up on the less-than "NUL" snippet (< NUL
).
Powershell and PHP support string interpolation in double-quoted strings. From the shell_exec
call, I'd think the variables I see (e.g., $sessionId
, $userName
, and $_
) are PHP variables being interpolated, hence the undefined error. I'm not sure if that's what you want.
It looks like you're trying to get a session identifier from a username. You might want to try something like...
$machineName = 'myComputer';
$userName = 'me';
$command = sprintf("quser /server:%s | findstr %s", $machineName, $userName);
$session = shell_exec($command);
$session_id = preg_split('/ +/', $session, 0)[3];
print($session_id);
I'm using sprintf() to generate the shell command because its interpolation safe. And, in this example, I'm ONLY using shell to return the user's session. I'm doing everything else in PHP. Lastly, I'd factor Powershell out of this. I love Powershell, but it's only going to make things more complicated.