Q:
Hello, script expert! Is there a way to use the script to determine whether the computer has a USB port 2.0?
-- RD
A:
Hello, RD. Are you familiar with the movie Freaky Friday? In the movie, the mother and daughter exchanged their bodies (and roles )? Well, we have something similar to crazy Friday. After all, the "script experts" should have helped you in this column. However, this problem actually helped a "script expert ". He has been trying to test a USB device (not successful) and has almost considered this work meaningless. But when I read your question, he thought, "Oh, yes: USB 1.1 and USB 2.0 ". He tried to test the device in the USB 1.1 slot (compared with the 2.0 slot) and the problem was solved immediately.
Hi, no one said that this special "script expert" is a genius. (Edit Note: I certify this .)
We owe you a favor, RD; how can we repay you? Good idea: Maybe we can start by answering your questions. Okay. Here is a script that probably returns the number of USB 2.0 ports on the computer. Let's look at the code first, and then explain the reason why we say it is "very likely" to return the number of ports:
Copy codeThe Code is as follows: strComputer = "."
I = 0
Set ob1_miservice = GetObject ("winmgmts: \" & strComputer & "\ root \ cimv2 ")
Set colControllers = obw.miservice. ExecQuery _
("Select * From Win32_USBController ")
For Each objController in colControllers
If Instr (objController. Name, "Enhanced") Then
I = I + 1
End If
Next
Wscript. Echo "No. of USB 2.0 Ports:" & I
This script first assigns values to two variables. Points (.) are assigned to the variable strComputer, and points (WMI Notation) represent local computers. At the same time, assign 0 to the counter variable I; we will use this variable as the active counter for all the USB 2.0 ports found.
After connecting to the WMI Service, we use this query to return all instances of the Win32_USBController class:
Set colControllers = obw.miservice. ExecQuery _
("Select * From Win32_USBController ")
Here we set a For Each loop to traverse Each item in the Set (the set represents all USB ports on the computer, of course. Unfortunately, the Win32_USBController class does not contain the USB Version attribute (such as Version). In fact, there is no clear way to view the properties of the USB slot and understand whether it is version 1.1 or 2.0. However, most port 2.0 contains the word Enhanced at a certain position in the name. We cannot guarantee that this is true for all ports, which is why we say this script may return the number of USB 2.0 ports on the computer. However, in random tests in the office, this method seems to work; it always has a name similar to the following when the 2.0 slot is found:
Via usb Enhanced Host Controller
How can this help us? Well, this means we can use the InStr function to check whether the word Enhanced appears in the Name of each slot:
If Instr (objController. Name, "Enhanced") Then
If the value returned by InStr is True (technically, if it returns a value greater than 0), Enhanced is found somewhere in the name. In this case, we will add the I value to 1:
I = I + 1
If InStr returns 0, the word Enhanced cannot be found. What should I do? Well, in this case, we enter the next loop and try the next item in the set. The I value remains unchanged.
We continue the query. Each time we find a USB slot with an Enhanced name, the I value is increased. After all, we only need to echo the I value, which will tell us the number of USB 2.0 ports on the computer.
It may not be a foolproof method, but it seems to work. And, you know, this is different from what you do for us.
Oh, by the way, we forgot one thing. Well, maybe you like chocolate?