I created a PowerShell script designed to be executed by PHP using shell_exec().

The PowerShell script is:

[System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
[System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
$process=Get-Process "cmd" | where {$_.mainWindowTitle -like "*FTB*"}
[Microsoft.VisualBasic.Interaction]::AppActivate($process.ID)
[System.Windows.Forms.SendKeys]::SendWait("whitelist reload{ENTER}")

And the PHP execution line is:

$out = shell_exec("powershell C:\whitelist_reload.ps1");
echo ($out);

The script executes, and I am given return information through the Echo. It it saying that the $process variable assigned to the value of Get-Process in Line 3 of the PS script is empty. The issue lies in that if I execute the script manually through a PowerShell executable, the script executes perfectly, and does exactly what I want.


Is there a particular reason why Line 3 returns nothing when executed by PHP, yet works fully when executed in PowerShell?

有帮助吗?

解决方案

The problem is most likely Session 0 isolation . CMD is running in a user session, while php(or the web server) is running as a service. Services in Windows 7 runs in an isolated session called Session 0. Processes in this session cannot communicate with other processes.

So you Get-process command can't list your CMD-process because it doesn't have access to it. You can verify the isolation by running $process=Get-Process "cmd" and echoing the results. I've never tried it, but I'm pretty sure you won't find any processes that you run interactively.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top