ReSTART Internet Explorer with vbs

Source: Internet
Author: User

Q:
Hello, script expert! If no other instances are running, how do I reSTART Internet Explorer?
-- MT
A:
Hello, MT. Thank you for your question. You know, now everyone is picky about poor Internet Explorer (although most Windows users are still using Internet Explorer ). But now someone raised a question, he wants to ensure that Internet Explorer is always running normally. You see, Internet Explorer: Some people like you!
Although we were supposed to be happy with Internet Explorer, we had to admit that this problem had made us a little embarrassed. After all, you can solve this problem through several different methods, depending on whether you need to restart Internet Explorer immediately or wait for a while to start it again. After a moment of meditation on this problem, we decided to do what we usually do: Use the simplest solution. In this example, check whether any Internet Explorer instance is running every 60 seconds. If yes, the script will only return to the sleep state, wait for 60 seconds, and then re-check. If no Internet Explorer instance is running, the script starts a new copy of Internet Explorer, then sleeps for a moment, waits for 60 seconds, and then checks again.
Yes, it's a bit like a day's work by a scripting expert. Well, do you know what we mean? The difference is that you wake up every 60 seconds to do something.
The following are the scripts we provide: Copy codeThe Code is as follows: strComputer = "."
Set ob1_miservice = GetObject ("winmgmts: \" & strComputer & "\ root \ cimv2 ")
Set objShell = CreateObject ("Wscript. Shell ")
Do While True
Set colProcesses = obw.miservice. ExecQuery _
("Select * from Win32_Process Where Name = 'ipolice.exe '")
If colProcesses. Count = 0 Then
ObjShell. Run "ipolice.exe"
End If
Wscript. Sleep 60000
Loop

Yes: it is essentially a common WMI script. Therefore, it first connects to the WMI Service on the local computer. However, there is a major difference between this script and other WMI scripts. We usually say, "however, you can also run this script on a remote computer ". Unfortunately, this is not the case. Technically, you can run this script on a remote computer, however, any Internet Explorer instance you start will run in an invisible window (at least in Windows XP and Windows Server 2003). Therefore, you will not be able to see it on the screen. This is a built-in security feature in the operating system: remotely started processes are always running in hidden windows. That is, the script must be run on the local computer.
Note: Is there a solution to this problem? In fact, there is a solution, although it requires you to start the process locally rather than remotely. For examples of how to solve this problem, see hello, script expert! Column.
After connecting to the WMI Service, we will create an instance of the WScript. Shell object, which we will use to generate all new Internet Explorer instances. (Yes, we could have used WMI for this purpose, but most people find it easier to run a program using Wscript. Shell than Using WMI .) Then we create a Do loop that runs as long as True is equal to True. (Unless there are any new advances in philosophy, this means that the script will run forever. To stop the script, you must terminate the script process. If you are running in the Command window under CScrip, you only need to press Ctrl + C or close the command window to implement this operation, which is very easy .)
So what operations does this loop perform? First, we use the following code to retrieve a set of all processes running on a computer named iexplore.exe:
Set colProcesses = obw.miservice. ExecQuery _
("Select * from Win32_Process Where Name = 'ipolice.exe '")
Despite the suspense, it is still necessary to be consistent with the set of all Internet Explorer instances currently running on the computer. Then, we check whether the value of the Count attribute (which tells us the number of items in the set) is equal to 0:
If colProcesses. Count = 0 Then
If Count is equal to 0, no Internet Explorer instance is running on the computer. Therefore, we use the Shell object and Run method to start a brand new instance:
ObjShell. Run "ipolice.exe"
That's it. Next, use the Sleep method to pause the script for 60 seconds (60,000 ms ). 60 seconds later, the script continues to run, repeats the process again, and continues until it is permanent. If the required time interval is not 60 seconds, adjust this value. For example, the following line of code checks every 30 seconds (30,000 milliseconds:
Wscript. Sleep 30000
The following code runs a check every 10 minutes (60,000 ms/minute multiplied by 10 minutes:
Wscript. Sleep 600000
It is undeniable that the Code only checks whether Internet Explorer is working every 10 minutes. For script experts, there is no need to check whether they are working every 10 minutes: after all, you know the answer to this question if there is any script.

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.