title: Weird results with WMI query for logged in users
body: <p>all!</p>
  
  <p>I'm trying to execute a pretty common WMI query for getting a list of users logged into any given machine. It is shown below (using Powershell code):</p>
  
  <pre><code> $wmi_result = Get-WmiObject -Query "SELECT LogonId FROM Win32_LogonSession WHERE LogonType=2" 
   foreach ($obj in $wmi_result) {
        $id = $obj.LogonId
        $user_list = Get-WmiObject -Query "ASSOCIATORS OF {Win32_LogonSession.LogonId=$id} WHERE AssocClass=Win32_LoggedOnUser Role=Dependent" | Select Name
   }
  </code></pre>
  
  <p>This works perfectly well on my local machine, but gives me nothing on remote machines. However, I'm able to get this information pretty readily if I manually parse the Dependent property of the associated class, as shown below:</p>
  
  <pre><code> $wmi_result = Get-WmiObject -Query "SELECT LogonId FROM Win32_LogonSession WHERE LogonType=2" -ComputerName &lt;computer&gt;
   foreach ($obj in $wmi_result) {
        $id = $obj.LogonId
        $user_list = Get-WmiObject -Query "SELECT * FROM Win32_LoggedOnUser" | where {$_.Dependent -match $id} -ComputerName &lt;computer&gt;
        foreach ($path in $user_list) {
              $user = ([wmi]$path).name
        }
   }
  </code></pre>
  
  <p>I've tried changing the impersonation and authentication levels of the WMI connection, to no avail. Running this query in WbemTest shows no results or errors as well. Finally, I get the same results regardless of whether I use PowerShell or System.Management directly. Of course, the Google fails me here.</p>
  
  <p>Can anyone give me some pointers on what I should try next?</p>
  
  <p>Thanks!</p>