Powershell System context issue

I tried running the following snippet from SCCM using a package/program deployment.

$ChromeProcesses = Get-Process -Name chrome -ErrorAction SilentlyContinue | Select-Object Path, Id

foreach ($process in $ChromeProcesses) {
    if ($process.Path -like "*\AppData\Local\Google\Chrome\Application\chrome.exe") {
        Write-Log "Found AppData Chrome - Stopping PID: $($process.id)"
        Stop-Process -Id $process.id -Force
    }
}

The command line was: powershell.exe -WindowStyle Hidden -File Script.ps1

When testing on a Windows 11 23H2 device, this part of the script would not run. No write-log being performed even though the chrome appdata process was running.

When I changed the command line to: "%Windir%\sysnative\WindowsPowerShell\v1.0\powershell.exe" -WindowStyle Hidden -File Script.ps1 - it worked as expected.

I understand the fix (64 vs. 32-bit Powershell) but I guess my confusion is understanding why a 32-bit powershell can't detect the processes. Appreciate any help understanding this.

Posted May 10, 2024, 12:21 PM UTC

43
surfingoldelephant•

Get-Process emits objects of type [Diagnostics.Process].

See the remarks section of the documentation.

32-bit processes cannot access the modules of a 64-bit process. If you try to get information about a 64-bit process from a 32-bit process, you will get a Win32Exception exception. A 64-bit process, on the other hand, can access the modules of a 32-bit process.

Path is an extended type system ScriptProperty added by PowerShell that retrieves its value with $this.Mainmodule.FileName. Per the remark above, as MainModule is empty for each [Diagnostics.Process] instance, so to is the value of Path.

As you've found, ensuring 64-bit powershell.exe is launched by bypassing file system redirection with Sysnative is one approach (and most likely, optimal). Another is to use the Win32_Process class instead.

$procs = Get-CimInstance -ClassName Win32_Process -Filter "Name = 'chrome.exe'"
$procs.Path
4
DenverITGuyOP•

Awesome explanation. Thank you!

2
surfingoldelephant•

You're very welcome.

1

There are probably a myriad reasons to just use a 64-bit process by default for all tasks, except when 32-bit is required.

1
Pollewops65•

Is this issue related to 23H2. I also had dome script tested some time ago. Wanted to use them now with 23h2 and those are not working anymore.

1