开发者

Ask ActiveDirectory if a machine is a member of a group

开发者 https://www.devze.com 2022-12-09 20:06 出处:网络
This should be really easy but for some reason it doesn\'t seem to be.I want to ask AD if the current machine is a member of a particular group.Direct membership is fine.

This should be really easy but for some reason it doesn't seem to be. I want to ask AD if the current machine is a member of a particular group. Direct membership is fine.

Group only contains 8 PC's and is extremely unlikely to grow beyond 30.

开发者_开发技巧

C# code examples appreciated!


Here's an example method using the System.DirectoryServices namespace:

public bool BelongsToGroup(string computerName, string groupName, string domain)
{
   PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, domain);

   ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(domainContext, computerName);

   foreach (Principal result in computer.GetGroups())
   {
      if (result.Name == groupName)
      {
         return true;
      }
   }

  return false;
}

So you could call it like this:

string computerName = Environment.MachineName;
string groupName = "Group Name";
string domainName = "Domain Name";
bool test = BelongsToGroup(computerName, groupName, domainName);
0

精彩评论

暂无评论...
验证码 换一张
取 消