【问题标题】:Retrieve LoggedOnUsers on Remote Machine检索远程计算机上的 LoggedOnUsers
【发布时间】:2020-12-23 13:41:50
【问题描述】:

我正在构建一个 C# 应用程序,以使用 WMI 和 WQL 查询来监控服务器和工作站的工作负载。我正在使用 WMI,因为与 powershell 查询相比,它似乎更快。当我尝试在远程计算机上检索已登录的用户时,我的困难就开始了。我想我需要使用 Win32_LoggedOnUser 类。我尝试了以下查询:

@"SELECT * FROM Win32_LoggedOnUser"
@"SELECT Antecedent FROM Win32_LoggedOnUser"

我习惯的是像这样检索所需的值:

var cims = connection.getCimInstances(this, queryUser);

 if (cims != null)
 {
    foreach (CimInstance cim in cims)
    {
      Komponenten.User user = new Komponenten.User();
      user.Name = Convert.ToString(cim.CimInstanceProperties["Name"].Value);
                    users.Add(user);
    }
 }    

其中 queryUser 是上面的字符串之一。

在这两种情况下,我都会得到一个 Win32_Account 对象作为回报,这似乎表明 - 调试器似乎确认 - 我应该再次在返回的 Win32_Account 类上使用 CimInstanceProperties["Name"].Value。但这根本行不通。关于如何访问存储在 CimInstanceProperity 中的 Win32_Account 的 CimInstanceProperties 的任何想法?我在相应的 Windows 参考页面 (https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-loggedonuser) 上也找不到任何东西,在我广泛的谷歌搜索中也找不到任何东西。

谢谢!

【问题讨论】:

  • “但这根本行不通”你的意思是什么?当您创建连接(我猜是用户名和密码)时,您是否检查过连接是否正常?
  • 太模糊了,同意。连接没问题,我也取回了 CimInstance-Object,但 CimInstanceProperty 拥有两个新的 CimInstance-Object(Antecedent 和 Descendent),它们又以 CimInstanceProperty 作为成员。我想访问 Antecendent-Object 的属性,但不知道如何访问。

标签: c# wmi wql


【解决方案1】:

在将 Antecedent - 对象转换为字符串后,我最终使用 ManagementObject-ClassRegex 来查找用户名:

var users = new List<Komponenten.User>();
        var searcher = this.connection.makeQuery(this, "SELECT * FROM Win32_LoggedOnUser");

        if (searcher != null)
        {
            foreach (ManagementObject queryObj in searcher.Get())
            {
                Komponenten.User user = new Komponenten.User();
                var win32_account = queryObj["Antecedent"].ToString();
                string stripped = Regex.Replace(win32_account, "[^a-zA-Z=]+", "", RegexOptions.Compiled);
                int end = stripped.LastIndexOf("=");
                user.Name = stripped.Substring(end+1);
                users.Add(user);
            }

            this.users = users;

考虑到 LogonSession 的替代方法是:

var users = new List<Komponenten.User>();
        var searcher = this.connection.makeQuery(this, "SELECT LogonId  FROM Win32_LogonSession Where LogonType=2");
        var Scope = this.connection.getScope(this, this.connection.getConnection());

        if (searcher != null)
        {
            foreach (ManagementObject queryObj in searcher.Get())
            {
                ObjectQuery LQuery = new ObjectQuery("Associators of {Win32_LogonSession.LogonId=" + queryObj["LogonId"] + "} Where AssocClass=Win32_LoggedOnUser Role=Dependent");
                ManagementObjectSearcher LSearcher = new ManagementObjectSearcher(Scope, LQuery);
                foreach (ManagementObject LWmiObject in LSearcher.Get())
                {
                    Komponenten.User user = new Komponenten.User();
                    user.Name =  Convert.ToString(LWmiObject["Name"]);
                    users.Add(user);
                }
            }
            this.users = users;
        }

其中 this.connection.getConnection 是一个 ConnectionsOption 对象,具体取决于您各自的域和帐户数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-28
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多