Functions/Process/Get-ProcessByHandle.ps1
Function Get-ProcessByHandle { [CmdletBinding()] Param ( # Handle of Process to Get [Parameter(Mandatory=$true)] $Handle ) Process { try { # Gather Thread and Process Info for this Handle $Thread = Get-CimInstance win32_thread -filter handle = $handle $ePID = [ref]0 $METHOD = @' [DllImport("User32.dll")] public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); '@ $User32 = Add-Type -MemberDefinition $Method -Name 'GWTPID' -Namespace 'Win32' -PassThru $ID = $User32::GetWindowThreadProcessId($Handle,$ePID) $PROC = Get-Process -Id $ePid.Value $PROC } catch {$NULL} } } |