查找打开多个实例的浏览器页面标题,多个标签

时间:2016-10-29 18:25:42

标签: vb.net powershell vbscript

我有多个Firefox打开的实例,每个都有多个标签,Chrome也在运行。我希望能够找到哪个浏览器运行Pandora的选项卡,例如。

我主要在VBScript中工作,但可以解决PowerShell问题,并可能访问VB.net。

1 个答案:

答案 0 :(得分:3)

使用VBScript,您需要第三方组件才能访问此类信息。否则,您唯一的选择是shell out并解析tasklist命令的输出。

searchTerm = "Pandora"

Set sh = CreateObject("WScript.Shell")
Set ps = sh.Exec("tasklist /v /fo csv")
data = ps.StdOut.ReadAll

searchTerm = "Pandora"

Set re = New RegExp
re.Pattern    = "^""(?:chrome|firefox|iexplore|opera)\.exe"""
re.IgnoreCase = True

For Each line In Split(data, vbNewLine)
  If re.Test(line) Then
    fields = Split(Mid(line, 2, Len(line)-2), """,""")
    If InStr(fields(UBound(fields)), searchTerm) > 0 Then
      pid = CInt(fields(1))
    End If
  End If
Next

sh.AppActivate pid

在PowerShell中,这样做更容易,因为Get-Process已经为您提供了窗口标题。

$searchTerm = 'Pandora'
Get-Process | Where-Object { $_.MainWindowTitle -like "*$searchTerm*" }
相关问题