I have PowerShell cleanup script for an application. Ideally processes are discovered by their path, so as not to recklessly kill other processes on the system which may have similar names. We noticed some processes are not being detected/killed, and after much experimentation realized the bit-ness is the issue. The script is bootstrapped in 32-bit for compatibility, but some of the processes are not.
Get-Process
can be called in 32-bit PowerShell and returns all the processes including the 64 bit ones, however as noted in This Ref:
On computers that are running a 64-bit version of Windows, the 64-bit version of PowerShell gets only 64-bit process modules and the 32-bit version of PowerShell gets only 32-bit process modules.
And indeed while the processes are discovered, the process module information (including the Path of the process) is not available for processes whose bit-ness does not match the shell.
This question has some discussion about it: How can I get the executable path of a 64-bit process given its PID from a 32-bit process?
The suggested Get-WmiObject
query does not work for me as shown, it returns 64- bit processes with missing ExecutablePath
information, basically the same as Get-Process
.
So my question is: Is it possible to call the WinAPI functions like QueryFullProcessImageName()
or GetModuleFileNameEx()
from a PowerShell script as a workaround to get this information? Or is there any other way to do this I am not considering?