1,105,534 Community Members

How to check available USB ports in Windows?

Member Avatar
adrianandreev
Newbie Poster
3 posts since Jul 2011
Reputation Points: 13 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi guys,

What i need is a way to check the available USB ports in windows using python.

The purpose: I'm trying to run on startUp a script that wait to plug in my Token, and after that the script will log me in automatically into the system that I'm using.

Scenario:
1) Waiting the special port to be used.
2) When detect some device in it - starting the application.
3) Auto log in with the Credentials that are on the token.

I think this should be the easiest way to make this work...
Please give some example of your way implementing step 1.

Thank you in advance!

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 
Member Avatar
adrianandreev
Newbie Poster
3 posts since Jul 2011
Reputation Points: 13 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

I found a better solution of my problem:

Here it is:

import win32com.client

strComputer = "."

objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")

colItems = objSWbemServices.ExecQuery("SELECT * FROM Win32_PnPEntity")

for objItem in colItems:
    if(objItem.Name!=None and objItem.Name=='USB Token'):
        print("Name:" + ` objItem.Name`)
        print("Status:" + ` objItem.Status`)
        print("Manufacturer:" + ` objItem.Manufacturer`)
        print("DeviceID:" + ` objItem.DeviceID`)
        print("Status:" + ` objItem.Status`)

This give me exactly what I needed!

Question Answered as of 2 Years Ago by pyTony
Member Avatar
adrianandreev
Newbie Poster
3 posts since Jul 2011
Reputation Points: 13 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Here is more information about this method:

""" ...
Win32_PnPEntity
Description
The Win32_PnPEntity WMI class represents the properties of a Plug and Play device. Plug and Play entities are shown as entries in the Device Manager located in Control Panel.

import win32com.client
def WMIDateStringToDate(dtmDate):
    strDateTime = ""
    if (dtmDate[4] == 0):
        strDateTime = dtmDate[5] + '/'
    else:
        strDateTime = dtmDate[4] + dtmDate[5] + '/'
    if (dtmDate[6] == 0):
        strDateTime = strDateTime + dtmDate[7] + '/'
    else:
        strDateTime = strDateTime + dtmDate[6] + dtmDate[7] + '/'
        strDateTime = strDateTime + dtmDate[0] + dtmDate[1] + dtmDate[2] + dtmDate[3] + " " + dtmDate[8] + dtmDate[9] + ":" + dtmDate[10] + dtmDate[11] +':' + dtmDate[12] + dtmDate[13]
    return strDateTime

strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("SELECT * FROM Win32_PnPEntity")
for objItem in colItems:
    if objItem.Availability != None:
        print "Availability:" + ` objItem.Availability`
    if objItem.Caption != None:
        print "Caption:" + ` objItem.Caption`
    if objItem.ClassGuid != None:
        print "ClassGuid:" + ` objItem.ClassGuid`
    if objItem.ConfigManagerErrorCode != None:
        print "ConfigManagerErrorCode:" + ` objItem.ConfigManagerErrorCode`
    if objItem.ConfigManagerUserConfig != None:
        print "ConfigManagerUserConfig:" + ` objItem.ConfigManagerUserConfig`
    if objItem.CreationClassName != None:
        print "CreationClassName:" + ` objItem.CreationClassName`
    if objItem.Description != None:
        print "Description:" + ` objItem.Description`
    if objItem.DeviceID != None:
        print "DeviceID:" + ` objItem.DeviceID`
    if objItem.ErrorCleared != None:
        print "ErrorCleared:" + ` objItem.ErrorCleared`
    if objItem.ErrorDescription != None:
        print "ErrorDescription:" + ` objItem.ErrorDescription`
    if objItem.InstallDate != None:
        print "InstallDate:" + WMIDateStringToDate(objItem.InstallDate)
    if objItem.LastErrorCode != None:
        print "LastErrorCode:" + ` objItem.LastErrorCode`
    if objItem.Manufacturer != None:
        print "Manufacturer:" + ` objItem.Manufacturer`
    if objItem.Name != None:
        print "Name:" + ` objItem.Name`
    if objItem.PNPDeviceID != None:
        print "PNPDeviceID:" + ` objItem.PNPDeviceID`
    print "PowerManagementCapabilities:"
    strList = " "
    try :
        for objElem in objItem.PowerManagementCapabilities :
            strList = strList + `objElem` + ","
    except:
        strList = strList + 'null'
    print strList
    if objItem.PowerManagementSupported != None:
        print "PowerManagementSupported:" + ` objItem.PowerManagementSupported`
    if objItem.Service != None:
        print "Service:" + ` objItem.Service`
    if objItem.Status != None:
        print "Status:" + ` objItem.Status`
    if objItem.StatusInfo != None:
        print "StatusInfo:" + ` objItem.StatusInfo`
    if objItem.SystemCreationClassName != None:
        print "SystemCreationClassName:" + ` objItem.SystemCreationClassName`
    if objItem.SystemName != None:
        print "SystemName:" + ` objItem.SystemName`

..."""

Have fun :)

Also here is the link that I found the answer: http://www.activexperts.com/admin/scripts/wmi/python/0346/

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

For me that code looked ugly, maybe something like this simply (the funcition was not called in my computer, so that is left out for simplicity):

import win32com.client

objSWbemServices = win32com.client.Dispatch("WbemScripting.SWbemLocator").ConnectServer(".","root\cimv2")

for item in objSWbemServices.ExecQuery("SELECT * FROM Win32_PnPEntity"):
    print('-'*60)
    for name in ('Availability', 'Caption', 'ClassGuid', 'ConfigManagerUserConfig',
                 'CreationClassName', 'Description','DeviceID', 'ErrorCleared', 'ErrorDescription',
                 'InstallDate', 'LastErrorCode', 'Manufacturer', 'Name', 'PNPDeviceID', 'PowerManagementCapabilities ',
                 'PowerManagementSupported', 'Service', 'Status', 'StatusInfo', 'SystemCreationClassName', 'SystemName'):
        a = getattr(item, name, None)
        if a is not None:
            print('%s: %s' % (name, a))
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: