浏览器的进程 ID window Microsoft Explorer

Process ID of a browser window Microsoft Explorer

有没有办法获取浏览器 windows 的进程 ID?当我打开多个浏览器 windows 时?

$windowTitle='Facebook(.*?)'
Get-Process | Where-Object {$_.mainWindowTitle -match $windowtitle} | 
Format-Table Id, Name, mainWindowtitle -AutoSize    

有了这个我只能搜索主要window标题。

您可以尝试参考下面的 PowerShell 脚本,它会为您提供每个 windows 的进程 ID 及其标题。此外,您可以根据需要修改脚本。

示例脚本:

$sig = @"
[DllImport("user32.dll", SetLastError=true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

[DllImport("user32.dll")]
public static extern IntPtr GetTopWindow(IntPtr hWnd);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

public enum GetWindow_Cmd : uint {
    GW_HWNDFIRST = 0,
    GW_HWNDLAST = 1,
    GW_HWNDNEXT = 2,
    GW_HWNDPREV = 3,
    GW_OWNER = 4,
    GW_CHILD = 5,
    GW_ENABLEDPOPUP = 6
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern int GetWindowTextLength(IntPtr hWnd);
"@
Add-Type -MemberDefinition $sig -Namespace User32 -Name Util -UsingNamespace System.Text

$iethreads = get-process iexplore |?{!$_.MainWindowTitle} |%{$_.ID}

$p=0
$window = [User32.Util]::GetTopWindow(0)

while ($window -ne 0) {
    [User32.util]::GetWindowThreadProcessId($window, [ref]$p) |out-null
    if ($iethreads -contains $p) {
        $length = [User32.Util]::GetWindowTextLength($window)
        if ($length -gt 0) {
            $string = New-Object System.Text.Stringbuilder 1024
            [User32.Util]::GetWindowText($window,$string,($length+1)) |out-null
            if ($string.tostring() -notmatch '^MSCTFIME UI$|^Default IME$|^SysFader$|^MCI command handling window$') {
                new-object psobject -Property @{PID = $p;Title = $string.tostring()}
            }
        }
    }
    $window = [User32.Util]::GetWindow($window, 2)
}

输出:

参考:

Finding the thread (PID) that belongs to a tab in IE with PowerShell