Ask:
Hello, Scripting Guy! Is there a way to use a script to determine if the computer has a USB 2.0 port?
--RD
For:
Hello, RD. Are you familiar with the movie Crazy Friday (Freaky Friday), the movie where the mother and daughter exchanged body (and role)? Well, our problem is a bit like the crazy Friday thing. After all, in this column it should have been the Scripting Guys to help you. However, this problem actually helps a "Scripting Guy". He had been trying to test USB devices (unsuccessfully) and had almost made the job meaningless. But when you read your question, he thought to himself, "gee, yes: USB 1.1 and USB 2.0." He tried to test the device in a USB 1.1 slot (Control 2.0 slots), and the problem was solved immediately.
Hey, no one ever said this particular "Scripting Guy" was a genius. (Edit note: I certify for this.) )
We owe you a favor, RD; how can we repay you? Good idea: Maybe we can start by answering your questions. OK, here's a script that is likely to return the number of USB 2.0 ports on your computer. Let's look at the code first and then explain why we say it "probably" returns the number of ports:
Copy Code code as follows:
strcomputer = "."
i = 0
Set objwmiservice = getobject ("winmgmts:\\" & strcomputer & "\root\cimv2")
set colcontrollers = objwmiservice.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. A dot (.) is assigned to the variable strComputer, and the point (WMI notation) represents the local computer. Also, assign a value of 0 to the counter variable I and 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 an instance of all Win32_usbcontroller classes:
Set Colcontrollers = objWMIService.ExecQuery _
("SELECT * from Win32_usbcontroller")
Here we set up a For Each loop to traverse the collection (which, of course, represents all the USB ports on the computer). Unfortunately, the Win32_usbcontroller class does not contain properties that tell us about the USB version (such as version), and in fact, there is no clear way to view the properties of the USB slot and see if it is version 1.1 or 2.0. However, most of the 2.0 ports contain the word enhanced in a location in the name. We can't guarantee that all ports are the same, which is why we say this script is likely to return the number of USB 2.0 ports on your computer. However, in random tests in the office, this approach seems to work; When you find 2.0 slots, it always has a name similar to the following:
VIA USB Enhanced Host Controller
So, how does that help us? Well, that means we can use the INSTR function to see if the word enhanced appears in the Name of each slot:
If Instr (Objcontroller.name, "enhanced") Then
If InStr returns the value True (technically, if it returns a value greater than 0), it means that the word enhanced is found somewhere in the name. In this case, we'll add the I value by 1:
i = i + 1
What if InStr returns 0, indicating that the word enhanced cannot be found? Well, in this case, we go to the next loop and try the next item in the collection, and the I value stays the same.
We continue with the query, increasing the I value each time we find a USB slot with a enhanced in the name. When all is done, we simply echo the I value, which tells us the number of USB 2.0 ports on the computer.
It's not necessarily a foolproof method, but it seems to work. And, you know, it's not the same thing you do for us.
Oh, yes; we forgot something. Well, you might like chocolate?