Results 1 to 1 of 1

Thread: Suggestions on WMI

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Suggestions on WMI

    Hi.
    I'm using WMI to get remote processes on computers.
    No, I can't use process class, search yesterday posts if you have questions.

    So What I do is create the management class and then get the collection.
    Something like this:

    Code:
       Dim queryCollection1 As ManagementObjectCollection
            Dim searcher1 As ManagementObjectSearcher
    
      Dim propt As ObjectGetOptions
            propt = New ObjectGetOptions(Nothing, TimeSpan.MaxValue, True)
            Using manClass As New ManagementClass("\\testserver\root\cimv2", "Win32_PerfFormattedData_PerfProc_Process", propt)
                manClass.Scope.Options.EnablePrivileges = True
                manClass.Scope.Options.Impersonation = ImpersonationLevel.Impersonate
                manClass.Scope.Options.Username = "user"
                manClass.Scope.Options.Password = "pass"
                manClass.Scope.Options.Authority = "ntlmdomain:mydomain"
                Dim query As ObjectQuery
                query = New ObjectQuery(
                    "SELECT * FROM Win32_PerfFormattedData_PerfProc_Process")
             
                searcher1 = New ManagementObjectSearcher(manClass.Scope, query)
    
                queryCollection1 = searcher1.Get()
              
            End Using
    
    ' Testing for values...
      For xx = 0 To 6 Step +1
           queryCollection1 = searcher1.Get()
      Next
    
            queryCollection1.Dispose()
            searcher1.Dispose()
    The 2nd implementation is like so:
    Code:
      Dim queryCollection1 As ManagementObjectCollection
      Dim searcher1 As New ManagementObjectSearcher
    
    
            Dim propt As ObjectGetOptions
            propt = New ObjectGetOptions(Nothing, TimeSpan.MaxValue, True)
            Dim scopper1 As New ManagementScope
            Dim query1 As New ObjectQuery
            Using manClass As New ManagementClass("\\testserver\root\cimv2", "Win32_PerfFormattedData_PerfProc_Process", propt)
                manClass.Scope.Options.EnablePrivileges = True
                manClass.Scope.Options.Impersonation = ImpersonationLevel.Impersonate
                manClass.Scope.Options.Username = "user"
                manClass.Scope.Options.Password = "pass"
                manClass.Scope.Options.Authority = "ntlmdomain:mydomain"
                   
                scopper1 = manClass.Scope.Clone()
                                
            End Using
    
       query1.QueryString = "SELECT * FROM Win32_PerfFormattedData_PerfProc_Process"
        searcher1.Scope = scopper1
        searcher1.Query = query1
      For xx = 0 To 6 Step +1
    
            '' in or out of the loop? probably out 
            ' searcher1.Scope = scopper1
            ' searcher1.Query = query1
    
                queryCollection1 = searcher1.Get()
       Next
    
            queryCollection1.Dispose()
            searcher1.Dispose()
    Now, this seems to work both ways , haven't tested in extend but it gets the collection.

    My questions are:
    1)Is this correct? And if so, which is better? I'm putting the ManagementClass and all the options and creds in a a using and then I get the results also outside the using. I do that because this will run with a timer every x seconds,
    so it does not seem logical to get the ManagementClass again and again. I was not sure if it will work as the loop runs outside the using but seems to work, so is that correct?
    2)Does anyone know how long this will keep working? Is there a time limit? I use Timespan.Maxvalue on ObjectGetOptions but i don't know if that is the way to go as I can't understand what the MSDN documentation is saying for ObjectGetOptions timeout
    Last edited by sapator; Mar 1st, 2021 at 01:46 PM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

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