代码之家  ›  专栏  ›  技术社区  ›  Chris R

如何使用命令终止上次打开的Internet Explorer窗口?

  •  2
  • Chris R  · 技术社区  · 15 年前

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

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

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

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

    在哪里?是打开的IE的PID,但我如何才能得到它?这需要以.cmd文件的形式运行,而不需要用户的任何输入。

    2 回复  |  直到 15 年前
        1
  •  4
  •   Anders    15 年前

    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运行)

        2
  •  0
  •   ghostdog74    15 年前

    使用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"