【问题标题】:How to kill the last opened Internet Explorer window using a command?如何使用命令杀死最后打开的 Internet Explorer 窗口?
【发布时间】:2009-12-10 20:47:10
【问题描述】:

我正在尝试编写一个 Windows 命令文件以在 IE 中打开网页,等待它加载,然后关闭 IE 窗口。以下工作但会杀死所有 IE 窗口,因此在运行 .cmd 之前已经打开的任何窗口也将被关闭。

start iexplore.exe "page to load"
ping localhost -n 10 > nul
taskkill /IM iexplore.exe

我只想杀死打开的IE。我知道如果我知道它的 PID,我可以杀死一个特定的进程,但是如何从命令行中找到它呢?启动IE窗口时有没有办法得到它?我真正想做的是:

start iexplore.exe "page to load"
ping localhost -n 10 > nul
taskkill /PID ?

在哪里?是打开的 IE 的 PID,但我怎样才能得到这个?这需要作为 .cmd 文件运行,无需用户进行任何输入。

【问题讨论】:

  • 也许可以使用 Process Explorer,download.sysinternals.com/Files/ProcessExplorer.zip 之类的工具。我认为您需要做的只是杀死具有最高进程 ID 的 iexplore.exe。我的两分钱。
  • 它将在不同的机器上运行,所以我不必在所有机器上都安装一些东西,这将是一个预定的工作,所以不能依赖用户检查什么最高的是,如果可能的话,我需要能够将其作为命令的一部分。
  • 上次我检查时,杀死一个 iexplore.exe 进程会使它们全部崩溃。
  • 哪个版本的IE? IE6、7 和 8 在进程与窗口的对应方式上都有所不同。 IE6是一个窗口一个进程(一般来说有例外)。 IE7 每个窗口可以有一个进程,外加一个共享进程(ieuser.exe)。 IE8 每个选项卡可以有一个进程,外加一个额外的进程。
  • taskkill的/fi开关可以通过windowtitle来杀死东西。 (请参阅technet.microsoft.com/en-us/library/bb491009.aspx)所以类似 taskkill /fi "Windowtitle eq My Awesome page - Windows Internet Explorer" 应该可以工作。公认的答案可能更好,因为 IE7-8 的多进程性可能会导致一些棘手问题。

标签: windows internet-explorer command-line


【解决方案1】:

IE已经支持自动化,找到并杀死正确的进程没有意义:

Set IE = CreateObject("InternetExplorer.Application")
IE.visible=true
IE.navigate "http://stackoverflow.com/"
while IE.Busy
 WScript.Sleep 555
 wend
IE.Quit

另存为 .vbs(并使用父程序/批处理文件中的 wscript.exe 运行)

【讨论】:

  • 干杯。做我想做的,并且可以准确控制时间(我的 ping 示例有点粗糙)。
【解决方案2】:

使用 vbscript

Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strProcess = objArgs(0) 'argument, which is the process name
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' call WMI service Win32_Process 
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = '"&strProcess&"'")
t=0
For Each objProcess in colProcessList
    ' do some fine tuning on the process creation date to get rid of "." and "+"
    s = Replace( objProcess.CreationDate ,".","")
    s = Replace( objProcess.CreationDate ,"+","")
    ' Find the greatest value of creation date
    If s > t Then
        t=s
        strLatestPid = objProcess.ProcessID
    End If    
Next
WScript.Echo "latest: " & t , strLatestPid
'Call WMI to terminate the process using the found process id above
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process where ProcessId =" & strLatestPid)
For Each objProcess in colProcess
    objProcess.Terminate()
Next

用法:

c:\test>cscript //nologo kill.vbs "iexplore.exe"

【讨论】:

  • 你能解释一下它是如何工作的吗?我以前没用过vbscript。
猜你喜欢
  • 2011-03-18
  • 1970-01-01
  • 2022-01-23
  • 2011-05-17
  • 2017-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-26
相关资源
最近更新 更多