windows 如何使用命令杀死最后打开的 Internet Explorer 窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1875344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:58:14  来源:igfitidea点击:

How to kill the last opened Internet Explorer window using a command?

windowsinternet-explorercommand-line

提问by Chris R

I'm trying to write a Windows command file to open a webpage in IE, wait for it to load, then close the IE window. The following works but will kill all IE windows so any that were already open before running the .cmd will also get closed.

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

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

I only want to kill the IE that was opened. I know I can just kill a particular process if I know its PID but how can find this from the command line? Is there a way to get it when starting the IE window? What I really want to do is:

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

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

where ? is the PID of the IE that gets opened but how can I get this? This needs to run as a .cmd file without any input from a user.

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

回答by Anders

IE already supports automation, there is no point in finding and killing the correct process:

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

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

Save as .vbs (And run with wscript.exe from parent program/batch file)

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

回答by ghostdog74

use 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

usage:

用法:

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