[英]Retrieve LoggedOnUsers on Remote Machine
I'm building a C# application to monitor server and workstation workloads with WMI and WQL queries.我正在构建一个 C# 应用程序以使用 WMI 和 WQL 查询监视服务器和工作站工作负载。 I'm using WMI because it seems to be faster in comparison to powershell queries.
我正在使用 WMI,因为与 powershell 查询相比,它似乎更快。 My hardship starts when I try to retrieve logged on users on a remote machine.
当我尝试检索远程计算机上的登录用户时,我的困难就开始了。 I figured I need to use the Win32_LoggedOnUser class. I have tried the following queries:
我想我需要使用Win32_LoggedOnUser class。我尝试了以下查询:
@"SELECT * FROM Win32_LoggedOnUser"
@"SELECT Antecedent FROM Win32_LoggedOnUser"
What I'm used to is to retrieve the desired value like this:我习惯于像这样检索所需的值:
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);
}
}
where queryUser is one of the strings from above.其中queryUser是上面的字符串之一。
In both cases, I get a Win32_Account object in return, which seems to suggest - and the debugger seems to confirm - that I should use CimInstanceProperties["Name"].Value
on the returned Win32_Account
class again.在这两种情况下,我都得到一个Win32_Account object 作为回报,这似乎表明 - 并且调试器似乎确认 - 我应该在返回的
Win32_Account
class 上再次使用CimInstanceProperties["Name"].Value
。 But that's not working at all.但这根本不起作用。 Any ideas on how to get access to the CimInstanceProperties of a Win32_Account stored in a CimInstanceProperity?
关于如何访问存储在 CimInstanceProperity 中的 Win32_Account 的 CimInstanceProperties 的任何想法? I can't find anything on the respective Windows reference page ( https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-loggedonuser ) nor during my extensive google-search.
我在相应的 Windows 参考页 ( https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-loggedonuser ) 或我广泛的谷歌搜索中找不到任何内容。
Thanks!谢谢!
I ended up using the ManagementObject-Class and Regex to find the usernames after converting the Antecedent - Object to a string:在将Antecedent - Object 转换为字符串后,我最终使用ManagementObject-Class和Regex查找用户名:
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;
An alternative which takes the LogonSession into account is:考虑到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;
}
where this.connection.getConnection is a ConnectionsOption object depending on your respective domain and account data其中this.connection.getConnection是ConnectionsOption object,具体取决于您各自的域和帐户数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.