Question WMI equivalents in WMI.Net

mellomel70

Member
Joined
May 10, 2010
Messages
8
Programming Experience
10+
Hi - I'm writing a script that will identify remote logons. I've done this before successfully with WMI, but I need to use WMI.Net this time around. For why, see WMI calls ignored outside of VisualStudio. Anyway, I have code that works, but I need to output some data, specifically a network user ID, and I'm not sure what I need to do in WMI.Net to accomplish this. Here's my old WMI code (objItem.Name is what I need to get at):

VB.NET:
Dim strComputer As String = "."
Dim objWMIService As Object = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Dim colSessions As Object = objWMIService.ExecQuery("Select * from Win32_LogonSession Where LogonType = 10")
Dim objSession As Object
For Each objSession In colSessions
  Dim colList As Object
  colList = objWMIService.ExecQuery("Associators of " & "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " & "Where AssocClass=Win32_LoggedOnUser Role=Dependent")
  Dim objItem As Object
  For Each objItem In colList
    If objItem.Name = Session("UserEID") Then
      'Do Something
    End If
  Next

Here's the WMI.Net version:

VB.NET:
Dim query As ObjectQuery
query = New ObjectQuery("Select * from Win32_LogonSession Where LogonType = 10")
Dim searcher As ManagementObjectSearcher
searcher = New ManagementObjectSearcher(query)
Dim queryCollection1 As ManagementObjectCollection = searcher.Get()
' Get the resulting collection and loop through it
Dim mo As ManagementBaseObject
For Each mo In queryCollection1
  query = New ObjectQuery("Associators of " & "{Win32_LogonSession.LogonId=" & mo("LogonId") & "} " & "Where AssocClass=Win32_LoggedOnUser Role=Dependent")
Next

Dim searcher2 = New ManagementObjectSearcher(query)
For Each objItem As ManagementObject In searcher2.Get()
   If objItem.Name = Session("UserEID") Then
      'Do Something
    End If
Next

In the .Net world objItem doesn't have a Name property. Where can I go to find what the properties of this object are in WMI.Net and which one I should be using to get the user's network ID?

Thanks!
 
Matt - thanks, objItem("Name") is indeed the answer. Now I've got another problem: my query is no longer returning remote users. As a matter of fact, it's returning a user who isn't logged into the server at all, and not showing users who are actually logged in remotely (me, for example). :confused: Has WQL also changed in .Net? Any ideas are welcome, or any links to WMI.Net documentation that might actually be helpful. Thanks!
 
Back
Top