JonBMN Posted February 21, 2014 Posted February 21, 2014 I'm trying to get the length of the WMI object I have created. I've tried using .count() and .length() to no avail. Can anyone shed some light on maybe something I'm missing? Local $oWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2") If @error Then Return SetError(@error, 0, "") Local $oItems = $oWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%)'", "WQL", 48) MsgBox(0, "", "Number of Ports: " & $oItems)
FireFox Posted February 21, 2014 Posted February 21, 2014 (edited) Hi, Maybe like this : Local $oWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2") If @error Then Return SetError(@error, 0, "") Local $oItems = $oWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%)'", "WQL", 48) Local $iCount = 0 For $oItem In $oItems $iCount += 1 Next MsgBox(0, "", "Number of Ports: " & $iCount) _ Br, FireFox. Edited February 21, 2014 by FireFox
JonBMN Posted February 21, 2014 Author Posted February 21, 2014 (edited) Hi, Maybe like this : Local $oWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2") If @error Then Return SetError(@error, 0, "") Local $oItems = $oWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%)'", "WQL", 48) Local $iCount = 0 For $oItem In $oItems $iCount += 1 Next MsgBox(0, "", "Number of Ports: " & $iCount) _ Br, FireFox. Yes, that is a solution. Not the solution I was hoping for as I was hoping there would be a method for finding the length, but thank you all the same. Edited February 21, 2014 by JonBMN
Solution kylomas Posted February 21, 2014 Solution Posted February 21, 2014 JonBMN, Try this (had to alter the script to make it run and altered the query to get results)... Local $oWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2") ;If @error Then Return SetError(@error, 0, "") ;Local $oItems = $oWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%)'", "WQL", 48) Local $oItems = $oWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity", "WQL") ; use default parms ConsoleWrite($oItems.count & @LF) See >this link for an explanation. kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
JonBMN Posted February 21, 2014 Author Posted February 21, 2014 Also, is there anyway to filter out certain comports, like bluetooth comports, when reading in through the WMI?
JonBMN Posted February 21, 2014 Author Posted February 21, 2014 JonBMN, Try this (had to alter the script to make it run and altered the query to get results)... Local $oWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2") ;If @error Then Return SetError(@error, 0, "") ;Local $oItems = $oWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%)'", "WQL", 48) Local $oItems = $oWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity", "WQL") ; use default parms ConsoleWrite($oItems.count & @LF) See >this link for an explanation. kylomas Why did you need to change the ExecQuery?
kylomas Posted February 21, 2014 Posted February 21, 2014 I don't have any "%(COM%)" qualifying entries... Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
JonBMN Posted February 21, 2014 Author Posted February 21, 2014 (edited) I don't have any "%(COM%)" qualifying entries... Ah I see, that's all I want. Also the .count() failed. Am I missing something on using this method? Edited February 21, 2014 by JonBMN
kylomas Posted February 21, 2014 Posted February 21, 2014 (edited) It's not "count()", it's "count" edit: I guess you programmer types would say it's not a method it's a property Edited February 21, 2014 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
JonBMN Posted February 21, 2014 Author Posted February 21, 2014 It's not "count()", it's "count" edit: I guess you programmer types would say it's not a method it's a property Tried that first off and it didn't work and that's why I tried to put the '()'. My bad on the method - property mistake.
kylomas Posted February 21, 2014 Posted February 21, 2014 The code that I posted runs on my machine (Vista). Make sure you remove the last parm ("48") from the query. Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
ripdad Posted February 21, 2014 Posted February 21, 2014 (edited) Try this... Local $iCount = _GetComCount() MsgBox(0, '', 'Number of Ports: ' & $iCount) Func _GetComCount($sComputer = @ComputerName) Local $objWMI = ObjGet('winmgmts:\\' & $sComputer & '\root\CIMV2') If @error Or Not IsObj($objWMI) Then Return SetError(@error, 0, '') Local $objClass = $objWMI.InstancesOf('Win32_PnPEntity WHERE Name LIKE "%(COM%)"') Return $objClass.Count EndFunc Edited February 21, 2014 by ripdad "The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward
JonBMN Posted February 21, 2014 Author Posted February 21, 2014 I'm running on windows 7 and XP. Why would you remove the '48' param? It was suggested to help read comports.
kylomas Posted February 21, 2014 Posted February 21, 2014 (edited) Read the link I pointed you to. It explains the count parameter. edit: The "48" you are using is probably several parms combined. You can manipulate them any way you want or use ripdad's example. They pretty much do the same thing. Edited February 21, 2014 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
JonBMN Posted February 21, 2014 Author Posted February 21, 2014 'it's memory foot print is reduced.' which takes out the ability to use .Count. Thanks for the help kylomas.
kylomas Posted February 21, 2014 Posted February 21, 2014 (edited) This thread has additional info. Edited February 21, 2014 by kylomas JonBMN 1 Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now