Use VBS to record the start time and end time of the screen saver _vbs

Source: Internet
Author: User
Tags case statement
Ask:
Hello, Scripting Guy! How do I log the start time and end time of the screen saver?
--JS
For:
Hello, JS. You know, a Scripting Guy (hey, who says "definitely Greg"?) Old enough to remember the day the screen saver just appeared. At the time, such scripts were meaningless. After all, after the screen saver started, everyone was so obsessed with it that they never thought about letting it end. In fact, one of the first things that the Scripting Guy, as a computer support person, has to do is create shortcuts on everyone's desktop so they can start the "flying little ovens" at any time.
It was easy for people to get happy then.
Ah, but it doesn't make sense to live in the past, does it? In today's modern society, it is clear that not only the screen saver needs to be stopped, but also the time to stop. With that in mind, let's take a look at this WMI event monitoring script, which tracks every start and stop of the screen saver:
Copy Code code as follows:

StrComputer = "."
Set objWMIService = GetObject ("winmgmts:\\" & StrComputer & "\root\cimv2")
Set Objeventsource = Objwmiservice.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 does look a little complicated, doesn't it? But don't panic: as far as design is concerned, WMI event scripts always look a bit complicated. Luckily, these scripts just look complicated, and you'll see that these scripts aren't really that hard to understand.
Note: Well, we'd better qualify the last statement: As long as you understand the basic ideas that the WMI event consists of, it's not that hard to understand. If you don't know, it's a good idea to take a moment to read the Scripting 2nd week webcast. This webcast will provide you with all the background information you need to understand today's column.
Good idea! While it may not be helpful to find out about one of our columns, at least it helps to figure out what the script code means.
The beginning of this particular script is to connect to the WMI service on the local computer in a historically long way. Usually here, we're going to execute a WMI query to return the information. As you can see, we're going to do this in this script, but the query looks a little different:
Set Objeventsource = Objwmiservice.execnotificationquery _
("SELECT * from __InstanceOperationEvent WITHIN 5 WHERE targetinstance ISA ' Win32_Process '")
Needless to say, this is not the type of WMI query you are accustomed to writing, because we're calling the ExecNotificationQuery method, not the ExecQuery. (For what?) Because you want to monitor WMI events, you must use the ExecNotificationQuery method. Today we cannot explain this query in detail, but we can say that we require WMI to notify us as soon as a WMI event (creation, deletion, modification) occurs. There is only one problem: we only want to be notified when TargetInstance (created, deleted, or modified) is an instance of the Win32_Process class.
Note: Of course, from a technical standpoint, there is a second problem: we only check for new events every 5 seconds. If the screen saver starts and ends in 3 seconds, we probably won't be notified.
In other words, suppose you create a new file. 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. Suppose a service has been modified. Do we want to be notified? Don't want to because the service is an instance of the Win32_Service class. OK, let's say that a new process, such as a screen saver, starts. Do we want to be notified? Of course you do. Don't forget, the new process is an example of a Win32_Process class. Any time you create, delete, or modify a process, we want to be notified.
But you're aware of that, right?
To get these notifications, we establish a Do loop that runs when True equals true:
Do While True
The syntax of a sentence is indeed a bit weird, but this syntax keeps the script running and constantly monitors the creation, deletion, and modification of the process until it terminates the script or restarts the computer. Without such a loop, the script will tell us when the screen saver starts, but then the script will end. As a result, we will never get a notification of when the screen saver ends.
Within the loop, the first thing we do is execute the following line of code:
Set objeventobject = Objeventsource.nextevent ()
What we do is tell the script to wait until the next event that we care about occurs. In other words, the script will stay on this line of code until a process is created, deleted, or modified. Assuming that the process is always the same, suppose we never create, delete, or modify a process. In this case, the script will always stop here and wait patiently. Case.
Now, we know what you're thinking. You are thinking: "Hey, wait a minute." 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, does that not also trigger the notification? ”
You're right: it triggers the notification. This line of code is then used to solve the problem. Starting Word (or any executable file, in this case) does give you a notification. 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 screen saver name is similar to MARQUEE.SCR. If the last four characters in the name are not. SCR, we simply loop around and wait for the next event to occur.
So what if the last four characters are. scr? In this case, we only care about two possibilities: Screen saver startup or screen saver end. (We don't care if someone modifies the properties of the screen saver.) To handle both of these possibilities, we set up a Select case block to check the Class of the event instance:
Select case Objeventobject.path_. Class
If Class equals __instancecreationevent, it means that a new process (that is, a new screen saver) has been created. In the first case statement, we check that the Class is equal to __InstanceCreationEvent. If we are equal, we echo back the fact that a particular screen saver (represented by the process name) starts at a specific time (using the VBScript function now):
Case "__InstanceCreationEvent"
WScript.Echo "Screensaver" & ObjEventObject.TargetInstance.Name & "Started:" & Now
That's a clear idea, isn't it? Now, suppose that the screen saver is finished, which causes the screen saver process to be removed. To handle this possibility, we check whether the __InstanceDeletionEvent class has a new instance. If an event belonging to this class occurs (indicating that the screen saver process has been deleted), we echo back the following fact-the specified screen saver stops at a specified time:
Case "__InstanceDeletionEvent"
WScript.Echo "Screensaver" & ObjEventObject.TargetInstance.Name & "ended:" & Now
At this point you have achieved your purpose. After you run this script, you will return information similar to the following:
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 exactly is Script center.scr? Download it, and then see for yourself.
There are two things we need to add. First, it's best to run this script in a command window under Cscript, that is, to start monitoring, open a command window and type a command similar to the following (depending on the script name, of course):
cscript Screensaver_monitor.vbs
Second, as we noted earlier, this script is designed to run forever. On the other hand, nothing lasts forever, does it? If you want to stop monitoring, we simply press CTRL + C, close the command window, or terminate the CScript.exe process. Remember, the Scripting Guys never let you in an infinite loop without an exit. (You know: This is a very appropriate job for us.) )

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.