VBS关闭所有Chrome标签

时间:2017-03-09 08:40:21

标签: google-chrome vbscript

我正在努力创造这个:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Chrome.exe'")

Set oShell = CreateObject("WScript.Shell")
For Each objProcess in colProcessList
oShell.Run "taskkill /im chrome.exe", , True
Next

Dim iURL 
Dim objShell

iURL = "www.google.com.au"

set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "chrome.exe", iURL, "", "", 1

代码有效,但如果打开的Chrome标签太多,则不会关闭所有标签。关闭标签中有时也会出现错误消息。

1 个答案:

答案 0 :(得分:0)

实现此目的的一种方法是以incognito模式打开Chrome,这样您就不会看到Restore Session错误。

Dim objExec, objShell, iURL

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("tasklist /fi " & Chr(34) & "imagename eq chrome.exe" & Chr(34))
If Not InStr(1, objExec.StdOut.ReadAll(), "INFO: No tasks", vbTextCompare) Then
    objShell.Run "taskkill /f /t /im chrome.exe", 0, True
End If

iURL = "www.google.com.au"
objShell.Run "chrome.exe -incognito " & iURL

Set objExec = Nothing : Set objShell = Nothing

WScript.Quit