'PHP shell_exec memory leak

I run a powershell script with php:

$output = shell_exec('powershell -File "script.ps1" -path "\\server\\data\\folder"');

script1.ps1:

Param([string]$path = "")
$list = @()

(Get-Acl $path).Access | Foreach-Object {
    
    $list += [PSCustomObject]@{ 
    Principal = $_.IdentityReference.ToString()
    Access = $_.FileSystemRights.ToString()
    }
}

$list = $list | ConvertTo-Json
Write-Host $list

When I run this script in powershell it is working without any problems. But PHP is creating huge memory leaks running this script (>5GB).

What could cause those memory leaks?

Edit: 05/20/22

Sammitch was right in the comments, I should have posted the complete php code. The problem was the code afterwards in the php script:

$output = shell_exec('powershell -File "script.ps1" -path "\\server\\data\\folder"');    

for($i = 0; $i < count($output); ++$i) {

}

The output of the powershell script should look like

[{"Principal":"Groupname1","Access":"Modify"},{"Principal":"Groupname2","Access":"Read"}]

But when there was only one group it looked like

{"Principal":"Groupname1","Access":"Modify"}

Because of this the loop never ended. Thanks for the hint!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source