Use VBS to determine the connection time of a removable drive _vbs

Source: Internet
Author: User
Ask:
Hello, Scripting Guy! How do I determine the connection time for a USB flash drive?
--PS
For:
Hello, PS. Yes, we admit: the Scripting Guys are really lazy. (especially in Friday, this day we have to write the Monday column.) There may be a way to specifically monitor when a USB flash drive is plugged in. However, we cannot find this method, at least not immediately find it. There is no denying that perhaps we may feel a little bit difficult. However, we decided to write a script that tells you when any removable drives are connected (or disconnected) from the computer. We hope this will provide you with some added value and flexibility.
Hey, we didn't say we did offer you added value and flexibility. We just want to be able to do it.
In fact, this is a small script that is easy to write. In addition, it has the advantage of being able to run on any version of Windows. (Initially, we wanted to use the Win32_VolumeChangeEvent class to accomplish this task, but that particular WMI class could only be found on Windows Server 2003.) )
The code for this section is as follows:
Copy Code code as follows:

StrComputer = "."
Set objWMIService = GetObject ("winmgmts:\\" & StrComputer & "\root\cimv2")
Set Colevents = Objwmiservice.execnotificationquery _
("SELECT * from __InstanceOperationEvent Within Where" _
& "TargetInstance Isa ' Win32_LogicalDisk '")
Do While True
Set objevent = colevents.nextevent
If ObjEvent.TargetInstance.DriveType = 2 Then
Select case Objevent.path_. Class
Case "__InstanceCreationEvent"
WScript.Echo "Drive" & ObjEvent.TargetInstance.DeviceId & _
"has been added."
Case "__InstanceDeletionEvent"
WScript.Echo "Drive" & ObjEvent.TargetInstance.DeviceId & _
"has been removed."
End Select
End If
Loop

Yes, it does look like the screen saver monitoring script we showed you a few days ago. We want to reuse the same script to provide you with added value and flexibility. (although this is primarily to ensure that we can save energy.) )
This script first connects to the WMI service on the local computer. We then issue the following query:
Set Colevents = Objwmiservice.execnotificationquery _
("SELECT * from __InstanceOperationEvent Within Where" _
& "TargetInstance ISA ' Win32_LogicalDisk '")
How does this query work? Well, here we're going to use the ExecNotificationQuery method to subscribe to a specific set of WMI events. What WMI event? (Man, you have too many questions, don't you?) In this case, we want to be notified every time we change an instance of the Win32_LogicalDisk class. As you can see immediately, these changes will include creating a new instance of the class (that is, adding a removable drive) and deleting an existing instance of the class (that is, removing a removable drive). Within 10 only means that every 10 seconds we will check to see if any of the Win32_LogicalDisk instances have changed.
Yes, it also means that if you insert a removable drive and then take it out in 6 seconds, then we may never know about it. If this is a problem, change the 10 to a smaller number. You can also change the 10 to a larger value. For example, if you change 10 to 60, you will check every 60 seconds instead of every 10 seconds.
Do you understand? We've even added added value and flexibility to the code itself!
Then, we build a non-stop do loop:
Do While True
Next we encountered the following line of code:
Set objevent = colevents.nextevent
As we said in the previous column, this line of code will cause the script to "break", meaning that the script will pause until the Win32_LogicalDisk class changes. This change, either by creating a new instance or deleting/modifying an existing instance, causes the script to execute the remaining lines of code in the Do loop.
Good question: What are the remaining lines of code going to do? Well, first look at whether the drive that generated the event happens to be a removable drive (at least for WMI, DriveType is 2):
If ObjEvent.TargetInstance.DriveType = 2 Then
If DriveType is not 2, then we can only recycle and wait for the next event to occur. If DriveType equals 2, then we use the Select case block to determine which type of event is occurring. We can do this by identifying the Class of the event:
Select case Objevent.path_. Class
Why did you do that? There are two reasons why: first, we are not concerned about any changes that occur in the existing instance. For example, we don't care whether the free drive space on drive C has changed. If you look at the Select case code, you'll notice that we don't bother checking __instancemodificationevent. Why not? Because we don't care about __instancemodificationevent (the type of event that is generated when an existing instance is modified in some way).
Second, we want to differentiate between __instancecreationevent (which tells us that we have created a new drive) and __InstanceDeletionEvent (which tells us that we have removed an existing drive). By determining the type of event, we can echo the different (and corresponding) messages. For example, the following is the code that determines whether a new drive was created, and if so, echo a message that indicates the result:
Case "__InstanceCreationEvent"
WScript.Echo "Drive" & ObjEvent.TargetInstance.DeviceId & _
"has been added."
Here is the code that tells us whether to delete the existing drive:
Case "__InstanceDeletionEvent"
WScript.Echo "Drive" & ObjEvent.TargetInstance.DeviceId & _
"has been removed."
After echoing the corresponding message, we loop around and wait for the next event to occur. By default, the script will run forever, or at least until you terminate the script process. Which piece of code is executed first in what case.
As we have said, this is not what you really want, but it does achieve this goal. Oh, don't forget the added value and flexibility. This should suffice to compensate for the fact that we have not really answered your question. (We hope we have answered one aspect of the problem.) But that's not your problem. )

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.