Use vbs to record the start time and end time of the Screen Saver

Source: Internet
Author: User

Q:
Hello, script expert! How does one record the screen saver start time and end time?
-- JS
A:
Hello, JS. You know, a scripting expert (Hey, who said "It must be Greg "?) When you are older, remember the days when the screen saver first appeared. At that time, such scripts were meaningless. After all, after the Screen Saver is started, everyone is fascinated and never wants to end it. In fact, the scripting expert, as a computer support specialist, must first create shortcuts on each person's desktop so that they can start the "Flying oven" at any time ".
At that time, people were very happy.
Ah, but it doesn't make sense to live in the past, right? In today's modern society, it is clear that not only do you need to stop the screen saver, but you also need to record the stop time. After clarifying this, let's take a look at the following WMI event monitoring script, which will track every start and stop of the Screen Saver: Copy codeThe Code is as follows: strComputer = "."
Set ob1_miservice = GetObject ("winmgmts: \" & strComputer & "\ root \ cimv2 ")
Set objEventSource = obw.miservice. ExecNotificationQuery _
("SELECT * FROM _ InstanceOperationEvent WITHIN 5 WHERE TargetInstance ISA 'win32 _ process '")
Do While True
Set objEventObject = objEventSource. NextEvent ()
If Right (objEventObject. TargetInstance. Name, 4) = ". scr" Then
Select Case objEventObject. Path _. Class
Case "_ InstanceCreationEvent"
Wscript. Echo "Screensaver" & objEventObject. TargetInstance. Name &_
"Started:" & Now
Case "_ InstanceDeletionEvent"
Wscript. Echo "Screensaver" & objEventObject. TargetInstance. Name &_
"Ended:" & Now
End Select
End If
Loop

It looks a little complicated, isn't it? But don't worry: in terms of design, the WMI event script always looks a bit complicated. Fortunately, these scripts just look complicated; you will see that these scripts are actually not that difficult to understand.
Note: Well, we 'd better limit the last statement: As long as you understand the basic idea of WMI event composition, it is not that difficult to understand. If you do not know about it, you 'd better spend some time writing a 2nd-week webcast script. This webcast provides you with all the background information you need to understand today's column.
Good idea! Although it may not help clarify the information of a column, it at least helps to understand the meaning of this script code.
This specific script is initially connected to the WMI Service on the local computer in a long history. Generally, WMI queries are executed to return information. As you can see, we also need to do this in this script, but the query looks a bit different:
Set objEventSource = obw.miservice. ExecNotificationQuery _
("SELECT * FROM _ InstanceOperationEvent WITHIN 5 WHERE TargetInstance ISA 'win32 _ process '")
Needless to say, this is not the WMI query type you are used to writing, because we call the execicationicationquery method instead of ExecQuery. (Why? To monitor WMI events, you must use the ExecNotificationQuery method .) We cannot explain this query in detail today, but we can say that we require WMI to notify us immediately if any WMI event (creation, deletion, or modification) occurs. There is only one problem: we only want to be notified when the TargetInstance (the project created, deleted, or modified) is an instance of the Win32_Process class.
Note: Of course, from a technical point of view, there is another problem: we only check for a new event every five seconds. If the screen saver starts and ends 3 seconds later, we will probably not be notified.
In other words, assume that a new file is created. Is the new file an instance of the Win32_Process class? No; it is an instance of the CIM_DataFile class. Therefore, we do not want to be notified. Assume that a service is modified. Do we want to be notified? No, because the service is a Win32_Service instance. Okay, let's assume that the new process (such as the screen saver) starts. Do we want to be notified? Of course. Do not forget that the new process is an instance of the Win32_Process class. If you create, delete, or modify a process at any time, we want to get a notification.
However, you are aware of these, right?
To get these notifications, we create a Do loop that runs when True is equal to True:
Do While True
The syntax of a sentence is a bit weird, but it keeps the script running and constantly monitors Process Creation, deletion, and modification until the script is terminated or the computer is restarted. Without such a loop, the script will notify us when the screen saver will start, but the script will end later. As a result, we will never be notified of the time when the screen saver ends.
In the loop, the first thing we need to do is to execute the following line of code:
Set objEventObject = objEventSource. NextEvent ()
What we do is to tell the script to wait until the next event we care about occurs. In other words, the script stays on this line of code until a process is created, deleted, or modified. If the process remains unchanged, we will never create, delete, or modify the process. In this case, the script will always stop here and wait patiently. Just in case.
Now, we know what you are thinking. You are thinking, "Hi, please wait. We only care about screen saver. Microsoft Word also runs in the process. If we start Microsoft Word to create a new instance of the Winword.exe process, will the notification be triggered ?"
You are right: a notification will be triggered. This line of code is used to solve this problem. When you start Word (or any executable file, in this case), a notification is sent. But we can use the following line of code to solve this problem:
If Right (objEventObject. TargetInstance. Name, 4) = ". scr" Then
Here, we use the Right function to check the name of the process that triggered the notification. If the rightmost four characters in the name are equal to. scr, we assume that the Screen Saver is being processed, because the name of the Screen Saver is similar to Marquee. scr. If the last four characters in the name are not. scr, we only loop once and wait for the next event to happen.
So what if the last four characters are. scr? In this case, we only care about two possibilities: Screen Saver startup or screen saver termination. (We don't care if someone modifies the properties of the screen saver .) To handle these two possibilities, we set a Select Case block to check the Class of the event instance:
Select Case objEventObject. Path _. Class
If the Class is equal to _ InstanceCreationEvent, it means that a new process (that is, a new screen saver) has been created ). In the first Case statement, we check whether the Class is equal to _ InstanceCreationEvent. If it is equal to, the following fact is displayed: a specific screen saver (represented by a process name) starts at a specific time (using the VBScript function Now:
Case "_ InstanceCreationEvent"
Wscript. Echo "Screensaver" & objEventObject. TargetInstance. Name & "started:" & Now
The meaning is clear, right? Now, assuming that the screen saver has ended, this will cause the screen saver process to be deleted. To handle this possibility, check whether the _ InstanceDeletionEvent class has a new instance. If an event of this type (indicating that the screen saver process has been deleted) occurs, the following fact is displayed: the specified Screen Saver stops at the specified time:
Case "_ InstanceDeletionEvent"
Wscript. Echo "Screensaver" & objEventObject. TargetInstance. Name & "ended:" & Now
So far, you have achieved your goal. After running this script, the following information is returned:
Screensaver Script Center. scr started: 2/9/2006 9:11:07 AM
Screensaver Script Center. scr ended: 2/9/2006 9:11:17 AM
Note: What is Script Center. scr? Download it and view it on your own.
We also need to add two things. First, it is best to run this script in the Command window under Cscript. That is to say, to start monitoring, open the command window and Type A Command similar to the following (of course, the specific content depends on the Script Name ):
Cscript screensaver_monitor.vbs
Second, as we mentioned earlier, this script is designed to run forever. On the other hand, nothing will last forever, right? To stop monitoring, press Ctrl + C to close the command window or terminate the CScript.exe process. Remember, the scripting experts will never keep you in an infinite loop without exit. (You know: this is a perfect description of our work .)

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.