Results 1 to 4 of 4

Thread: Avoid 'For Each' when using WMIService?

  1. #1

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,663

    Question Avoid 'For Each' when using WMIService?

    Hello:

    Is there a way to use the following code, without using the For Each loop?
    Can we access a single Item from the collection, without making it in to a collection?

    Code:
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
            
    Debug.Print colItems.Caption   'Why can't we just do it like this? <<<<<<<<
            
    For Each objItem In colItems
            debug.print objitem.Caption   'Only this method works....
    Next
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  2. #2
    Fanatic Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    578

    Re: Avoid 'For Each' when using WMIService?

    Well it returns a collection even if we only return one item.

    At the moment you are asking the Query statement to return everything possible with 'Select * from'. This could be very slow depending on how many items are are available.

    As you only want the Caption, ask for it only. 'Select Caption from'. This will speed things up considerably.

  3. #3
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine
    Posts
    740

    Re: Avoid 'For Each' when using WMIService?

    some1uk03, you can't get value of default property of WMI class without iterating the collection.
    E.g., for Win32_ComputerSystem class def. prop. name is "Name", for Win32_LogicalDisk - "DeviceID" e.t.c.

    But, you can have inline solution to get value of any other property of the class if you know the value of default property, like:
    Code:
    Debug.? GetObject("winmgmts:\\.\root\cimv2").Get("Win32_ComputerSystem.Name='Alex-PC'").Properties_.Item("TotalPhysicalMemory").Value
    Be aware, it is very difficult to prevent trapping on errors in such calling model.
    It's better to use traditional enumeration or without WMI at all.
    Last edited by Dragokas; Jun 4th, 2017 at 10:59 AM.
    Malware analyst, VirusNet developer, HiJackThis+ author || my CodeBank works

  4. #4
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: Avoid 'For Each' when using WMIService?

    Well how far do you want to take things. You can access WMI through COM interfaces in the style of my projects... starting with IWbemLocator and IWbemServices

    It would be a LOT more work, but you could avoid For Each


    ..sounds like something I would do just for kicks. hmm.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width