Hi everyone,
I am having a problem with an application that has worked great in both test and QA. It is a Windows Service that does a couple of things depending on the situation on the client. One of these situations is when there's more than a certain amount of users logged on to the client(We have Switch User enabled on these clients).
It seems like the reason because it is not working is because the WMI class Win32_LoggedOnUser seems to want to talk to the Active Directory and at our customer we have List Mode activated. The client can't talk to the user side of the AD and the user side can't talk to the Computer side. Here's some code(With debug entries just for this debugging):
private int GetUserCount()
{
_log.WriteDebugEntry("Is now located in GetUserCount()");
List<string> tempList = new List<string>();
int count = 0;
try
{
int firstQueryResult = 0;
ManagementScope Scope = new ManagementScope(String.Format(@"\\.\root\CIMV2"), null);
Scope.Connect();
ObjectQuery Query = new ObjectQuery("SELECT LogonId FROM Win32_LogonSession " +
"WHERE LogonType = 2 " +
"OR LogonType = 10 " +
"OR LogonType = 11");
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
foreach (ManagementObject WmiObject in Searcher.Get())
{
firstQueryResult++;
_log.WriteDebugEntry("Query: \nAssociators of {Win32_LogonSession.LogonId=" + WmiObject["LogonId"] + "} Where AssocClass=Win32_LoggedOnUser Role=Dependent");
ObjectQuery LQuery = new ObjectQuery("Associators of {Win32_LogonSession.LogonId=" + WmiObject["LogonId"] + "} Where AssocClass=Win32_LoggedOnUser Role=Dependent");
ManagementObjectSearcher LSearcher = new ManagementObjectSearcher(Scope, LQuery);
foreach (ManagementObject LWmiObject in LSearcher.Get())
{
tempList.Add((string)LWmiObject["Name"]);
_log.WriteDebugEntry(string.Format("Name of the found user is: {0}", (string)LWmiObject["Name"]), EventLogEntryType.Information);
}
}
_log.WriteDebugEntry(string.Format("Result of first query is: {0}", firstQueryResult), EventLogEntryType.Information);
}
catch (Exception e)
{
//EventLog
_log.WriteDebugEntry("Could not determinate user count using WMI query\n" + e.Message, EventLogEntryType.Error, e);
}
foreach (string sDistinct in tempList.Distinct())
{
count++;
}
_log.WriteDebugEntry("Distinct Usercount is: " + count);
return count;
}
The first query from Win32_LogonSession works great but the result from the Associators query is always empty. This works in our environment.
At first I just thought "Well, this sucks... guess I should just take out all explorer processes och count the distinctive owners as a backup", with the only problem: When a user is "Locked" and another user is using the desktop the owners of the Explorer processes becomes empty.
So, my question here is... do you guys know any way to get the user count of users that only has or have had an interactive desktop from System Context?
Would save my day :) Thank you!
No comments:
Post a Comment