Peter,
Do you need to use LDAP for this particular task; it may be easier to use WMI -- however this will only work on windows hosts. I don't know the name of the object you want to query off the top of my head, but if you can track it down, here's the rest ( I'm pulling this from an app I wrote to pull process info, but the theory is the same):
1). Create a ConnectionOptions Object
ConnectionOptions oConn = new ConnectionOptions();
if(!chkTrusted.Checked){
oConn.Username = txtUsername.Text;
oConn.Password = txtPassword.Text;
}
The default behavior is to construct an object with the currently logged in users credentials ... the if statement referes to controlls on the form that allow you to specify a different user (very usefull in a web context).
System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\" + txtComputername.Text + "\\root\\cimv2" , oConn);
System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("SELECT * FROM Win32_Process");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
try{
ManagementObjectCollection oReturnCollection = oSearcher.Get();
foreach(ManagementObject oReturn in oReturnCollection){
// name of process
txtOutput.AppendText( oReturn["Name"].ToString().ToLower() + "\n");
// arg to send with method invoke to return user and domain
string[] o = new String[2];
// Invoke the method and populate the o var with the user name and domain
oReturn.InvokeMethod("GetOwner",(object[])o);
// write out user info that was returned
txtOutput.AppendText("User: " + o[1] + "\\" + o[0]);
txtOutput.AppendText("PID: " + oReturn["ProcessId"].ToString());
// get priority
if(oReturn["Priority"] != null)
txtOutput.AppendText("Priority: " + oReturn["Priority"].ToString());
// this is the amount of memory used
if(oReturn["WorkingSetSize"] != null){
long mem = Convert.ToInt64(oReturn["WorkingSetSize"].ToString()) / 1024;
txtOutput.AppendText("Mem Usage: " + mem.ToString());
}
}
}catch (System.Management.ManagementException me){
txtOutput.AppendText(me.ToString());
}
}
All you have to do to adapt this code too your needs is to replace the WQL "SELECT * FROM Win32_Process" with one that queries users ... i.e. "SELECT * FROM <whatever_user_object>" and then, in the for loop, you would do like an:
(pseudo code; will not compile)
ArrayList lstUsersInGroup = new ArrayList();
...
if (objUser.Group == txtGroup.text) lstUsersInGroup.Add(objUser);
...
HTH
Regards,
Meredith Shaebanyan
|