User registered on the remote computer

Is there a way to determine that all users are registered on a remote computer using WMI and C #

+3
source share
1 answer

After a little research, I was able to figure this out, although I'm not sure if this is the best way.

public void GetCompDet(string ComputerName)
{
    CurrentSystem = ComputerName;
    ConnectionOptions options = new ConnectionOptions();

    ManagementScope moScope = new ManagementScope(@"\\" + ComputerName + @"\root\cimv2");
    try
    {
        moScope.Connect();
    }
    catch
    {
        return;
    }
    ObjectQuery query = new ObjectQuery("select * from Win32_Process where name='explorer.exe'");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(moScope, query);
    ManagementObjectCollection queryCollection = searcher.Get();
    foreach (ManagementObject m in queryCollection)
    {
        ManagementOperationObserver mo = new ManagementOperationObserver();
        mo.ObjectReady += new ObjectReadyEventHandler(mo_ObjectReady);
        m.InvokeMethod(mo, "GetOwner", null);
    }
}

void mo_ObjectReady(object sender, ObjectReadyEventArgs e)
{
    ManagementObject m = sender as ManagementObject;
    LoggedinUser.Enqueue(CurrentSystem + " - >" + e.NewObject.Properties["user"].Value.ToString());
    Console.WriteLine(CurrentSystem + " - >" + e.NewObject.Properties["user"].Value.ToString());
}
+2
source

Source: https://habr.com/ru/post/1793614/


All Articles