c# - WMI retrieve groups that a user is member of? -
this code retrieving local computer info :
managementobjectsearcher usersearcher = new managementobjectsearcher("select * win32_computersystem"); managementobjectcollection usercollection = usersearcher.get(); string[] sep = { "\\" }; string[] username = usercollection.cast<managementbaseobject>().first()["username"].tostring().split(sep, stringsplitoptions.none); managementobjectsearcher searcher = new managementobjectsearcher("select * win32_useraccount domain = 'mydomain' , name= '" + username[1] + "'"); managementobjectcollection collection = searcher.get(); writeonlistbox("username: " + (string)collection.cast<managementbaseobject>().first()["name"]); writeonlistbox("full name: " + (string)collection.cast<managementbaseobject>().first()["fullname"]); writeonlistbox("description: " + (string)collection.cast<managementbaseobject>().first()["description"]);
but i couldnt retrieve groups i'm in , email.
after couple of hours on working on solved , answer question,
private string getgroupsforuser(string username) { managementobjectsearcher searcher = new managementobjectsearcher("select * win32_groupuser partcomponent=\"win32_useraccount.domain='mydomain',name='" + username + "'\""); stringbuilder strgroups = new stringbuilder(); foreach (managementobject mobject in searcher.get()) { managementpath path = new managementpath(mobject["groupcomponent"].tostring()); if (path.classname == "win32_group") { string[] names = path.relativepath.split(','); strgroups.append(names[1].substring(names[1].indexof("=") + 1).replace('"', ' ').trim() + ", "); } } return strgroups.tostring(); }
Comments
Post a Comment