Restarting the Internet with the VBS implementation EXPLORER_VBS

Source: Internet
Author: User
Tags time interval
Ask:
Hello, Scripting Guy! How do I restart Internet Explorer if no other instances are running?
--MT
For:
Hello, MT. Thank you for your question. You know, everyone is picky about poor Internet Explorer today (although most Windows users are still using Internet Explorer). But now a question has been raised that he wants to make sure Internet Explorer is always working. See, Internet Explorer: There are people like you!
Although we were supposed to be happy with Internet Explorer, we have to admit that the problem is a bit embarrassing for us. After all, you can solve this problem in a number of different ways, depending on whether you need to restart Internet Explorer now or wait a while to start it. After meditating on this question for a moment, we decided to do what we usually do: take the simplest solution, and in this example, check every 60 seconds if any Internet Explorer instances are running. If so, the script only returns to hibernation, waits 60 seconds, and then checks again. If there are no instances of Internet Explorer running, the script starts a new copy of Internet Explorer, sleeps for a moment, waits 60 seconds, and then checks again.
Yes, it's kind of like a Scripting Guy's day job. Well, do you know what we mean? The difference is waking every 60 seconds to do something.
Here's the script we've provided:
Copy Code code as follows:

StrComputer = "."
Set objWMIService = GetObject ("winmgmts:\\" & StrComputer & "\root\cimv2")
Set Objshell = CreateObject ("Wscript.Shell")
Do While True
Set Colprocesses = objWMIService.ExecQuery _
("SELECT * from Win32_Process Where Name = ' iexplore.exe '")
If colprocesses.count = 0 Then
Objshell.run "Iexplore.exe"
End If
Wscript.Sleep 60000
Loop

Yes: It's essentially just a normal WMI script. Therefore, it first connects to the WMI service on the local computer. But there is a major difference between this script and other WMI scripts. Usually at this point we say, "However, you can also run this script against a remote computer." Unfortunately, this is not the case this time. Technically, you can run the script against a remote computer, but any Internet Explorer instance that you start will run in an invisible window (at least on Windows XP and Windows Server 2003), so you won't be able to see it on the screen. This is a built-in security feature in the operating system: a remote-initiated process always runs in a hidden window. That is, the script must be running on the local computer.
Note: Is there a solution to this problem? In fact, there is a workaround, although it requires you to start the process locally rather than remotely. For an example of how to resolve this issue, see Hello, Scripting Guys! Column.
After connecting to the WMI service, we create an instance of the Wscript.Shell object that we will use to generate all the new instances of Internet Explorer. (Yes, we could have used WMI to do this, but most people find it easier to run programs using Wscript.Shell than to use WMI.) Then we build a Do loop that runs if True equals True. (unless there is any new progress in philosophy, this means that the script will run forever.) To stop the script, you need to terminate the script process. If you're running in a command window under Cscrip, it's easy to just press CTRL + C or close the command window. )
So what does this loop do? First, we use the following code to retrieve the collection of all processes named Iexplore.exe that are currently running on the computer:
Set Colprocesses = objWMIService.ExecQuery _
("SELECT * from Win32_Process Where Name = ' iexplore.exe '")
Although the suspense is lost, it is still consistent with the collection of all Internet Explorer instances that are currently running on the computer. We then examine whether the Count property, which tells us the number of items in the collection, is equal to 0:
If colprocesses.count = 0 Then
If Count is equal to 0, no instances of Internet Explorer are running on the computer. So we use the Shell object and the Run method to start a completely new instance:
Objshell.run "Iexplore.exe"
That's it. Next, use the Sleep method to pause the script for 60 seconds (60,000 milliseconds). After 60 seconds, the script continues to execute, looping again and repeating the process until forever. If the time interval you want is not 60 seconds, adjust the value. For example, the following line of code will check every 30 seconds (30,000 milliseconds):
Wscript.Sleep 30000
The following code runs a check every 10 minutes (60,000 milliseconds per minute and 10 minutes):
Wscript.Sleep 600000
Admittedly, the code checks whether Internet Explorer is working every 10 minutes. For the Scripting Guys, there's no need to check whether they're working every 10 minutes: after all, there's no script, you know the answer to that question.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.