开发者

User logged into remote machine

开发者 https://www.devze.com 2023-02-12 03:52 出处:网络
Is there a way to determine what all users are logged into remote 开发者_运维知识库machine, using WMI and C#After little research I was able to figure it out, although not sure if this is the best way

Is there a way to determine what all users are logged into remote 开发者_运维知识库machine, using WMI and C#


After little research I was able to figure it out, although 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());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消