我正在尝试编写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文件运行。
答案 0 :(得分:4)
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运行)
答案 1 :(得分:0)
使用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"