In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use vbs to record the start time and end time of the screen saver", the content is simple and clear, hoping to help you solve your doubts, the following let the editor lead you to study and learn "how to use vbs to record the start time and end time of the screen saver" this article.
Q:
How do I record the start and end times of the screen saver?
A: you know, a scripting expert (Hey, who says "definitely Greg"? Old enough to remember the days when screen savers first appeared. At that time, such scripts were meaningless. After all, after the screen saver started, everyone was fascinated and never wanted it to end. In fact, one of the first things the scripting expert as a computer support person must do is to create shortcuts on everyone's desktop so that they can start the "small flying oven" at any time.
It was easy for people to be happy at that time.
Ah, but there's no point in living in the past, is it? In today's modern society, it is clear that you not only need to stop the screen saver, but also need to record when it stops. Once this is clear, let's take a look at the following WMI event monitoring script, which tracks every start and stop of the screen saver:
The copy code is 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: WMI event scripts always look a little complicated in terms of design. Fortunately, these scripts just look complex; as you can see, they 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 the composition of WMI events, it's not that hard to understand. If you don't already know, it's best to take a moment to look at the webcast in the second week of scripting. This webcast will provide you with all the background information you need to understand today's column.
That's a good idea! Although there may be no information to help clarify one of our columns, it at least helps to understand the meaning of the script code.
This particular script begins by connecting to the WMI service on the local computer in a time-honored way. Usually here, we execute a WMI query to return information. As you can see, we're going to do the same in this script, except that 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 used to writing, because we are calling the ExecNotificationQuery method, not ExecQuery. Why? Because to monitor WMI events, you must use the ExecNotificationQuery method. We can't explain this query in detail today, but we can say that we ask WMI to notify us as soon as any WMI events (create, delete, modify) occur. There is only one problem: we only want to be notified when TargetInstance (the project that is created, deleted, or modified) is an instance of the Win32_Process class.
Note: of course, from a technical point of view, 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 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. Suppose a service has been modified. Do we want to be notified? Do not want it, because the service is an instance of the Win32_Service class. Well, suppose a new process (such as a screen saver) starts. Do we want to be notified? Of course I do. Don't forget that the new process is an instance of the Win32_Process class. Any time we create, delete, or modify a process, we want to be notified.
But you're aware of that, right?
To get these notifications, we set up a Do loop that runs when True equals True:
Do While True
The syntax of the sentence is a little weird, but it keeps the script running and monitors the creation, deletion, and modification of the process until it terminates the script or restarts the computer. Without such a loop, the script tells us when the screen saver starts, but then the script ends. As a result, we will never be notified when the screen saver ends.
Within the loop, the first thing we need to 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 we care about occurs. In other words, the script stays on this line of code until a process is created, deleted, or modified. Assume that the process remains the same, and that we never create, delete, or modify the process. In this case, the script will stop here forever and wait patiently. Just in case.
Now, we know what you're thinking. You're thinking, "Hey, wait a minute. All we care about is the screen saver. Microsoft Word is also running in the process. If we start Microsoft Word to create a new instance of the Winword.exe process, won't it also trigger a notification?"
You are right: a notification will be triggered. The next line of code is used to solve this problem. Starting Word (or any executable file, for that matter) does issue 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 equals .scr, we assume that we are working on a screen saver because the name of the screen saver is similar to Marquee.scr. If the last four characters in the name are not .scr, we just loop once and wait for the next event to occur.
So, what if the last four characters are .scr? In this case, we are only concerned with two possibilities: the screen saver starts or the screen saver ends. (we don't care if anyone modifies the properties of the screen saver. To handle these two 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 to see if Class equals _ _ InstanceCreationEvent. If equal, we echo the fact that a specific 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
It's clear, isn't it? Now, assuming that the screen saver has ended, this will cause the screen saver process to be deleted. To handle this possibility, we check to see if there is a new instance of the _ _ InstanceDeletionEvent class. If an event that belongs to this class occurs (indicating that the screen saver process has been deleted), we echo the fact that the specified screen saver stops at a specified time:
Case "_ _ InstanceDeletionEvent"
Wscript.Echo "Screensaver" & objEventObject.TargetInstance.Name & "ended:" & Now
So far you have achieved your goal. After running 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 see for yourself.
We have two more things to add. First, it is best to run this script in the command window under Cscript, that is, to start monitoring, open the command window and type something similar to the following (depending on the name of the script, of course):
Cscript screensaver_monitor.vbs
Second, as we pointed out earlier, this script is designed to run forever. On the other hand, nothing lasts forever, does it? To stop monitoring, all we have to do is press Ctrl+C, close the command window, or terminate the CScript.exe process. Remember, experts will never let you fall into an infinite loop with no exit.
The above is all the contents of the article "how to record the start time and end time of the screen saver with vbs". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.